Problem:
vector<Shape*> v;를 이용하여 간단한 그래픽 편집기를 콘솔 바탕으로 만들어보자.
생성된 도형 객체를 v에 삽입하고 관리하라. 9장 Exercise Problem 10번의 힌트를 참고하라.
Shape과 Circle, Line, Rect 클래스는 다음과 같다.

2019/11/26 - [C++/명품 C++ programming] - 명품 C++ programming Exercise Problem 9장 10번
[명품 C++ programming Exercise Problem 9장 10번
Problem: 간단한 그래픽 편집기를 콘솔 바탕으로 만들어보자. 그래픽 편집기의 기능은 "삽입", "삭제", "모두보기", "종료" 의 4가지이고, 실행 과정은 다음과 같다. Objective & Hints: 추상 클래스, 상속 종합 응용 S..
sobamemil.tistory.com](https://sobamemil.tistory.com/17)
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() |