Problem:
vector<Shape*> v;๋ฅผ ์ด์ฉํ์ฌ ๊ฐ๋จํ ๊ทธ๋ํฝ ํธ์ง๊ธฐ๋ฅผ ์ฝ์ ๋ฐํ์ผ๋ก ๋ง๋ค์ด๋ณด์.
์์ฑ๋ ๋ํ ๊ฐ์ฒด๋ฅผ v์ ์ฝ์
ํ๊ณ ๊ด๋ฆฌํ๋ผ. 9์ฅ Exercise Problem 10๋ฒ์ ํํธ๋ฅผ ์ฐธ๊ณ ํ๋ผ.
Shape๊ณผ Circle, Line, Rect ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ๋ค.
Execution Result:
Objective & Hints:
vector๋ฅผ ํ์ฉํ๋ ์ข
ํฉ ์์ฉ
Code:
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 #include #include using namespace std; class Shape { protected: virtual void draw() = 0; public: void paint() { draw(); } }; class Circle : public Shape { protected: virtual void draw(){ cout << "Circle" << endl; } }; class Rect : public Shape { protected: virtual void draw() { cout << "Rectangle" << endl; } }; class Line : public Shape { protected: virtual void draw() { cout << "Line" << endl; } }; class UI { public: static int seleteMenu() { int n; cout << "์ฝ์
:1, ์ญ์ :2, ๋ชจ๋๋ณด๊ธฐ:3, ์ข
๋ฃ:4 >> "; cin >> n; return n; } static int seleteShape() { int n; cout << "์ :1, ์:2, ์ฌ๊ฐํ:3 >> "; cin >> n; return n; } static int seleteDelIndex() { int n; cout << "์ญ์ ํ๊ณ ์ ํ๋ ๋ํ์ ์ธ๋ฑ์ค >> "; cin >> n; return n; } static void showAll(vector<Shape*> &v, vector<Shape*>::iterator &it) { int i=0; for(it = v.begin();it!=v.end(); it++, i++){ // vector v์ ์ฒซ ์์๋ถํฐ ๋ ์์๊น์ง ํ์ ๋ฐ ์ถ๋ ฅ cout << i << ": "; v.at(i)->paint(); } } }; class GraphicEditor { vector<Shape*> v; vector<Shape*>::iterator it; public: GraphicEditor() { cout << "๊ทธ๋ํฝ ์๋ํฐ์
๋๋ค.\n"; start(); } void start() { while(true){ int n; n = UI::seleteMenu(); switch(n){ case 1: //์ฝ์
์ ์ ํํ ๊ฒฝ์ฐ n = UI::seleteShape(); switch(n){ case 1: //์ ์ ์ ํํ ๊ฒฝ์ฐ v.push_back(new Line()); break; case 2: //์์ ์ ํํ ๊ฒฝ์ฐ v.push_back(new Circle()); break; case 3: //์ฌ๊ฐํ์ ์ ํํ ๊ฒฝ์ฐ v.push_back(new Rect()); break; default: cout << "์๋ชป ์ ํํ์
จ์ต๋๋ค.\n"; break; } break; case 2:{ //์ญ์ ๋ฅผ ์ ํํ ๊ฒฝ์ฐ n = UI::seleteDelIndex(); if(n >= v.size()