求圆,三角形,正方形的面积。运用多态

#include <iostream>
using namespace std;
class Bass
{
    public:
    virtual  int area()
    {
        return 0;
    }
    int r;
    int e;
    int h;
    int l;
};
class Circle:public Bass
{
    public:
    int area()
    {
        double r;
    cout<<"请输入圆的半径"<<endl;
    cin>>r;
        double c=3.14*r*r;
    cout<<"圆的面积为"<<c<<endl;
    return c;
    }
};
class triangle:public Bass
{
    public:
    int area()
    {
    double e;
    cout<<"请输入底边"<<endl;
    cin>>e;
    double h;
    cout<<"请输入高"<<endl;    
    cin>>h;
    double s=0.5*e*h;
    cout<<"三角形的面积为"<<s<<endl;
    return s;
    }
};
class abb:public Bass
{
    public:
    int area()
    {
    double l;
    cout<<"请输入正方形的边长"<<endl;
    cin>>l;
    double s=l*l;
    cout<<"正方形的面积为"<<s<<endl;
    return s;
    }
};
void dowork(Bass *b)
{
    b->area();
    delete b;
}
int main()
{
    {
    //Bass *b=new Circle;
    int z=0;
    while(1)
    {
            cout<<"将要输入哪总方式"<<endl;
    cout<<"1.圆"<<endl;
    cout<<"2.三角形"<<endl;
    cout<<"3.正方形"<<endl;
        cin>>z;
        switch(z)
        {
    case 1:
    dowork(new Circle);
    system("pause");
    system("cls");
    break;
    case 2:
    dowork(new triangle);
        system("pause");
    system("cls");
    break;
    case 3:
    dowork(new abb);
        system("pause");
    system("cls");
    default:break;
        }
    }
    }
    return 0;    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆向爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值