๋ฐ์ํ
1. Converter ํด๋์ค๋ฅผ ์์๋ฐ์ ๋ฌ๋ฌ๋ฅผ ์ํ๋ก ํ์ฐํ๋ WonToDollar ํด๋์ค๋ฅผ ์์ฑํ๋ผ.
2. Converter ํด๋์ค๋ฅผ ์์๋ฐ์ km๋ฅผ mile๋ก ๋ณํํ๋ KmToMile ํด๋์ค๋ฅผ ์์ฑํ๋ผ.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <string> using namespace std; class Converter { protected: double ratio; virtual double convert(double src) = 0; // src๋ฅผ ๋ค๋ฅธ ๋จ์๋ก ๋ณํํ๋ค. virtual string getSourceString() = 0; // ์์ค ๋จ์ ๋ช
์นญ 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; } }; class WonToDollar : public Converter { protected: virtual string getSourceString() { return "์"; } virtual string getDestString() { return "๋ฌ๋ฌ"; } virtual double convert(double src); public: WonToDollar(double ratio) : Converter(ratio) { } }; double WonToDollar::convert(double src) { return src / ratio; } class KmToMile : public Converter { protected: virtual string getSourceString() { return "km"; } virtual string getDestString() { return "mile"; } virtual double convert(double src); public: KmToMile(double ratio) : Converter(ratio) { } }; double KmToMile::convert(double src) { return src / ratio; } int main() { WonToDollar wd(1010); // 1 ๋ฌ๋ฌ์ 1010์ wd.run(); KmToMile toMile(1.609344); // 1mile์ 1.609344 km toMile.run(); } | cs |
์คํ ๊ฒฐ๊ณผ
๋ฐ์ํ