C++ Programming Ch.5 Exercise 12 Solution
Problem: ๋ค์์ ํ๊ณผ๋ฅผ ๋ํ๋ด๋ Dept ํด๋์ค์ ์ด๋ฅผ ํ์ฉํ๋ main()์ ๋ณด์ฌ ์ค๋ค. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Dept { int size; // scores ๋ฐฐ์ด์ ํฌ๊ธฐ int* scores; // ๋์ ํ ๋น ๋ฐ์ ์ ์ ๋ฐฐ์ด์ ์ฃผ์ public: Dept(int size) { // ์์ฑ์ this->size = size; scores = new int[size]; } Dept(const Dept& dept); // ๋ณต์ฌ ์์ฑ์ ~Dept(); // ์๋ฉธ์ int getSize() { return size; } void read(); // size ๋งํผ ํค๋ณด๋์์ ์ ์๋ฅผ ์ฝ์ด scores ๋ฐฐ์ด์ ์ ์ฅ bool isOver60(int index); // index์ ํ์์ ์ฑ์ ์ด 60๋ณด๋ค ํฌ๋ฉด true ๋ฆฌํด }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int countPass(Dept dept) { // dept ํ๊ณผ์ 60์ ์ด์์ผ๋ก ํต๊ณผํ๋ ํ์์ ์ ๋ฆฌํด int count = 0; for (int i = 0; i < dept.getSize(); i++) { if (dept.isOver60(i)) count++; } return count; } int main() { Dept com(10); // ์ด 10๋ช ์ด ์๋ ํ๊ณผ com com.read(); // ์ด 10๋ช ์ ํ์๋ค์ ์ฑ์ ์ ํค๋ณด๋๋ก๋ถํฐ ์ฝ์ด scores ๋ฐฐ์ด์ ์ ์ฅ int n = countPass(com); // com ํ๊ณผ์ 60์ ์ด์์ผ๋ก ํต๊ณผํ ํ์์ ์๋ฅผ ๋ฆฌํด cout << "60์ ์ด์์ " << n << "๋ช "; } (1) main()์ Execution Result๊ฐ ๋ค์๊ณผ ๊ฐ์ด ๋๋๋ก Dept ํด๋์ค์ ๋ฉค๋ฒ๋ค์ ๋ชจ๋ ๊ตฌํํ๊ณ , ์ ์ฒด ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. ...