반응형
3~4번에 쓰이는 추상 클래스 LoopAdder 입니다.
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을 출력한다.
}
문제
LoopAdder 클래스를 상속받아 다음 main() 함수와 실행 결과처럼 되도록 ForLoopAdder 클래스를 작성하라. ForLoopAdder 클래스의 calculate() 함수는 for 문을 이용하여 합을 구한다.
int main() {
ForLoopAdder forLoop("For Loop");
forLoop.run();
}
결과
While Loop:
처음 수에서 두번째 수까지 더합니다. 두 수를 입력하세요 >> 3 10
3에서 10까지의 합 = 52 입니다
소스코드
#include<iostream>
using namespace std;
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을 출력한다.
}
class ForLoopAdder : public LoopAdder {
string name;
public:
ForLoopAdder(string name = "") : LoopAdder(name) { this->name = name; }
virtual int calculate() {
int sum = 0;
for (int i = getX(); i <= getY(); i++)
sum += i;
return sum;
}
};
int main() {
ForLoopAdder forLoop("For Loop");
forLoop.run();
}
반응형
'명품C++프로그래밍' 카테고리의 다른 글
명품 C++ Programming 실습문제 9장 5번 (1) | 2021.06.01 |
---|---|
명품 C++ Programming 실습문제 9장 4번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 9장 2번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 9장 1번 (0) | 2021.06.01 |
명품 C++ Programming 실습문제 8장 9번 (1) | 2021.05.27 |