목록으로 2013.04.10. 20:25
C언어

가상함수(Shape 클래스)

youtube
class Shape 
{
protected:  
    int x, y; 
public:
    Shape(int x=0, int y=0) { this->x = x; this->y = y;} 
    void setOrigin(int x, int y) { this->x = x; this->y = y; }
    virtual void draw() { cout <<"Shape Draw" << endl; }
    void printPosition() { cout << "x : " << x << ", y : " << y << endl; }
};

class Rectangle : public Shape 
{
        private: 
        int width, height;
        public: 
    Rectangle (int x=0, int y=0, int w=5, int h=10) :Shape(x,y)
        {
                width=w;
                height=h;
        }
    void setWidth(int w) 
        {
                width=w;
        }
    void setHeight(int h) 
        {
                height=h; 
        }
    void draw() 
        {
                cout<<"Rectangle draw"<<endl;
        }
    void printPosition() 
        {   
                Shape::printPosition();
                cout<<"width:"<<width<<"height:"<<height<<endl;
        }
};
class Circle : public Shape 
{
private: int radius;
public: Circle(int x=0, int y=0, int r=5):Shape(x,y)
                {
                        radius=r;
                }
                void setRadius(int r)
                {
                        radius=r;
                }
                void draw()
                {
                        cout<<"Circle draw"<<endl;
                }
                void printPosition()
                {
                        Shape::printPosition();
                        cout<<"r:"<<radius;
                }
};

int main() 
{
    Shape *ps = new Rectangle();  // 상향 형변환            
    ps->draw();        

    Shape *ps1 = new Circle();   // 상향형변환
    ps1->draw();

    delete ps;    
    delete ps1;
}

댓글 0

댓글 작성 권한이 없습니다. 로그인하기

NO
TITLE
섬네일
profile 너에게제공 | 12. 21 | 조회
16
XML
profile 너에게제공 | 12. 14 | 조회
15
XML s
XML
asf | 12. 14 | 조회
14
XML XML
profile 너에게제공 | 12. 07 | 조회
13
XML | 12. 07 | 조회
12
XML
s | 11. 30 | 조회
11
XML asd
XML
124 | 11. 23 | 조회
9
XML 124
XML
dqw | 11. 23 | 조회
8
XML 124
profile 너에게제공 | 11. 23 | 조회
7
profile 너에게제공 | 11. 23 | 조회
6
profile 너에게제공 | 11. 09 | 조회
4
XML
sdaf | 10. 26 | 조회
3
XML asdf