2. 继承

#include  <iostream>
using  namespace  std;

class  Shape
{
public:
        Shape()  {}
        ~Shape()  {}
        virtual  float  GetArea()  {  return  -1;  }
};

class  Circle  :  public  Shape
{
public : 
    Circle(float r):r1(r) { }
    ~Circle() {}
    virtual float GetArea() { return 3.14 * r1 * r1; }
private : 
    float r1;

};

class  Rectangle  :  public  Shape
{
public : 
    Rectangle (float len ,float width):len (len) , width (width) {}
    ~Rectangle() { } 
    virtual float GetArea() { return len * width;  }
private : 
    float len, width; 

};

class  Square  :  public  Rectangle
{
public:
        Square(float  len);
        ~Square()  {}
};

Square::Square(float  len)  :Rectangle(len,  len)
{
}

int  main()
{
        Shape*  sp;

        sp  =  new  Circle(5);
        cout  <<  "The  area  of  the  Circle  is  "  <<  sp->GetArea()  <<  endl;
        delete  sp;
        sp  =  new  Rectangle(4,  6);
        cout  <<  "The  area  of  the  Rectangle  is  "  <<  sp->GetArea()  <<  endl;
        delete  sp;
        sp  =  new  Square(5);
        cout  <<  "The  area  of  the  Square  is  "  <<  sp->GetArea()  <<  endl;
        delete  sp;
}


凉心哥哥的小宝藏 文章被收录于专栏

c++

全部评论

相关推荐

小鸡蛋吃布丁:上岸编制,考个偏远的四五线小县城的话那确实难度不高,工资三四千的,但是考发达地区的纯看实力和运气了
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务