C++ Programming Ch.10 Exercise 16 Solution
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()