728x90
#include<iostream>
using namespace std;
class Shape {
Shape* next;
protected:
virtual void draw() { cout << "--Shape--" << endl; }
public:
Shape() { next = NULL; }
virtual ~Shape() {}
void paint() { draw(); }
Shape* add(Shape* p) { this->next = p; return p; }
Shape* getNext() { return next; }
};
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; }
};
int main()
{
cout << "2018305065 전유정" << endl << endl;
Shape* pStart = NULL;
Shape* pLast;
pStart = new Circle();
pLast = pStart;
pLast = pLast->add(new Rect());
pLast = pLast->add(new Circle());
pLast = pLast->add(new Line());
pLast = pLast->add(new Rect());
Shape* p = pStart;
while (p != NULL)
{
p->paint();
p = p->getNext();
}
p = pStart;
while (p != NULL)
{
Shape* q = p->getNext();
delete p;
p = q;
}
}
반응형
'전공 공부 > C++' 카테고리의 다른 글
클래스 KmToMile (0) | 2021.06.16 |
---|---|
클래스 WonToDollar (0) | 2021.06.16 |
클래스 BaseArray (상속) (0) | 2021.06.16 |
클래스 Calculator (다중 상속) (0) | 2021.06.07 |
클래스 NameCircle (상속, 파생 클래스와 생성자 작성) (0) | 2021.06.07 |