πŸ’» Dev & CSC++ Programming

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 ν΄λž˜μŠ€μ— 멀버듀을 λͺ¨λ‘ κ΅¬ν˜„ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 5, 2020 Β· 5 min Β· Sobamemil