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 문을 이용하여 합을 구한다. ...

November 21, 2019 · 3 min · Sobamemil

C++ Programming Ch.9 Exercise 2 Solution

Problem: The following is an abstract class Converter that converts units. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Converter { protected: double ratio; virtual double convert(double src)=0; // src를 다른 단위로 변환한다. virtual string getSourceString()=0; // src 단위 명칭 virtual string getDestString()=0; // dest 단위 명칭 public: Converter(double ratio) { this->ratio = ratio; } void run(){ double src; cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. "; cout << getSourceString() << "을 입력하세요>> "; cin >> src; cout << "변환 결과 : " << convert(src) << getDestString() << endl; } }; Write a km를 mile(마일)로 변환하는 KmToMile class that inherits from the Converter class. The main() function and execution result are as follows. ...

November 20, 2019 · 2 min · Sobamemil