전공 공부/C++

클래스 Shape 가상 함수

jeonnew 2021. 6. 16. 19:18
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;
    }
}

반응형