Third practice 5

Third practice 5

任务描述

定义一个Shape基类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。

测试输入:5689

预期输出:

The area of the Circle is 78.5
The area of the Rectangle is 48
The area of the Square is 81

源代码

#include <iostream>
using namespace std;
#define PI 3.14

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

class Circle : public Shape
{
public:
    virtual float GetArea(){return PI*Redius*Redius;}
    Circle(float Redius){
        this->Redius = Redius;
    };
private:
    float Redius;
};

class Rectangle : public Shape
{
public:
    virtual float GetArea(){return this->length*this->wide;}
    Rectangle(float length,float wide){
        this->length = length;
        this->wide = wide;
    }

private:
    float length,wide;

};

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

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

}

int main(){
    float Redius,length,wide,len;
    cin>>Redius>>length>>wide>>len;

    Circle c(Redius);
    cout<<"The area of the Circle is "<<c.GetArea()<<endl;

    Rectangle r(length,wide);
    cout<<"The area of the Rectangle is "<<r.GetArea()<<endl;

    Square s(len);
    cout<<"The area of the Square is "<<s.GetArea()<<endl;
    return 0;
}

原文链接: https://www.cnblogs.com/lightice/p/12910907.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Third practice 5

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/349435

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午5:41
下一篇 2023年3月2日 上午5:41

相关推荐