2021-11-15

 输出

The area of the Circle is 78.5
The area of the Rectagle is 30
The area of the Square is 64
class Shape{
public:
    virtual float getArea()=0;
    virtual ~Shape(){}
};
//你的代码将被嵌在这里
#include<iostream>
using namespace std;

class Circle :public Shape
{
    float R;
public:
    Circle(float n)
    {
        R = n;
    }
    float getArea()
    {
        return R * R * 3.14;
    }
    ~Circle() {}
};
class Rectangle
{
    float wide, lenth;
public:
    Rectangle(float a, float b)
    {
        wide = a;
        lenth = b;
    }
    float getArea()
    {
        return wide * lenth;
    }
    virtual ~Rectangle()
    {}

};
class Square :public Rectangle
{
    int c;
public:
    Square(float a) :Rectangle(a, a)
    {
        c = a;
    }
    ~Square() {}

};
int main()
{  Shape *ps;
    ps=new Circle(5);
    cout<<"The area of the Circle is "<<ps->getArea()<<endl;
    delete ps;
    Rectangle *pr;
    pr=new Rectangle(5,6);
    cout<<"The area of the Rectagle is "<<pr->getArea()<<endl;
    delete pr;
    Square s(8);
    pr=&s;
    cout<<"The area of the Square is "<<pr->getArea()<<endl;
    delete pr;
    return 0;
}

delete中pr只能用虚继承来继承Rectangle 因为构造是先构造父类析构是先析构子类如果直接delete自动调用的析构Square就没了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值