题解 | #KiKi设计类继承#

KiKi设计类继承

https://www.nowcoder.com/practice/699ba050e2704591ae3e62401a856b0e

#include <iostream>
using namespace std;
class shape{
    protected:
    double x,y;
    public:
    shape(double x,double y):x(x),y(y){}
    virtual double GetArea(){
        return 0.0;
    }
};
class Rectangle:public shape{
    protected:
        double len,wid;
    public:
        Rectangle(double x,double y,double len,double wid):shape(x,y),len(len),wid(wid){}
        double GetArea() override{
            return len*wid;
        }
};
class circle:public shape{
    private:
    double redius;
    public:
    circle(double x,double y,double redius):shape(x,y),redius(redius){}
    double GetArea() override{
        return 3.14*redius*redius;
    }
};
class squre:public Rectangle{
    protected:
        double side;
        public:
        squre(double x,double y,double side):Rectangle( x, y,  side, side){}


};
int main() {
    double len,wid,redius,side;
    cin>>len>>wid;
    cin>>redius;
    cin>>side;
    Rectangle a(0,0,len,wid);
    circle b(0,0,redius);
    squre c(0,0,side);
    cout<<a.GetArea()<<endl;
    cout<<b.GetArea()<<endl;
    cout<<c.GetArea()<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务