C++ Programming Ch.9 Exercise 3 Solution
Problem: λ€μ μΆμ ν΄λμ€ LoopAdderκ° μλ€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class LoopAdder { // μΆμ ν΄λμ€ string name; // 루νμ μ΄λ¦ int x, y, sum; // xμμ yκΉμ§μ ν©μ sum void read(); // x, y κ°μ μ½μ΄ λ€μ΄λ ν¨μ void write(); // sumμ μΆλ ₯νλ ν¨μ protected: LoopAdder(string name="") { // 루νμ μ΄λ¦μ λ°λλ€. μ΄κΉκ°μ "" this->name = name; } int getX() { return x; } int getY() { return y; } virtual int calculate() = 0; // μμ κ°μ ν¨μ. 루νλ₯Ό λλ©° ν©μ ꡬνλ ν¨μ public: void run(); // μ°μ°μ μ§ννλ ν¨μ }; void LoopAdder::read() { // x, y μ λ ₯ cout << name << ":" << endl; cout << "μ²μ μμμ λλ²μ§Έ μκΉμ§ λνλ€. λ μλ₯Ό μ λ ₯νμΈμ >> "; cin >> x >> y; } void LoopAdder::write() { // κ²°κ³Ό sum μΆλ ₯ cout << x << "μμ " << y << "κΉμ§μ ν© = " << sum << " μ λλ€" << endl; } void LoopAdder::run() { read(); // x, yλ₯Ό μ½λλ€ sum = calculate(); // 루νλ₯Ό λλ©΄μ κ³μ°νλ€. write(); // κ²°κ³Ό sumμ μΆλ ₯νλ€. } Write a λ€μ main() ν¨μμ Execution Resultμ²λΌ λλλ‘ ForLoopAdder class that inherits from the LoopAdder class. ForLoopAdder ν΄λμ€μ calculate() ν¨μλ for λ¬Έμ μ΄μ©νμ¬ ν©μ ꡬνλ€. ...