문제 :
다음은 단위를 변환하는 추상 클래스 Converter이다.
| 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; } }; |
Converter 클래스를 상속받아 km를 mile(마일)로 변환하는 KmToMile 클래스를 작성하라. main() 함수와 실행 결과는 다음과 같다.
| 1 2 3 4 | int main() { KmToMile toMile(1.609344); // 1마일은 1.609344 KM toMile.run(); } |

목적 및 힌트 :
추상 클래스를 상속받는 파생 클래스 만들기
코드 :
| 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 32 33 | #include |