day 0912

本文介绍了C++中的类Shape,以及其派生类Circle和Rect,展示了如何定义构造函数、运算符重载和成员函数,计算并显示图形的周长和面积。
摘要由CSDN通过智能技术生成
#include <iostream>
#define PI 3.14
using namespace std;
class Shape
{
protected:
    double round;
    double area;
public:
    Shape():round(0),area(0){}
    Shape(double r,double a):round(r),area(a){}
    Shape(const Shape &shape)
    {
        this->round = shape.round;
        this->area = shape.area;
    }
    ~Shape(){}
    Shape& operator=(const Shape &shape)
    {
        this->round = shape.round;
        this->area = shape.area;
        return *this;
    }
};
class Circle:public Shape
{
private:
    double radius;
public:
    Circle(double r):radius(r)
    {
        round = 2*PI*r;
        area = r*r*PI;
    }
    Circle(Circle *c):Shape(c->round,c->area),radius(c->radius){}
    ~Circle(){}
    Circle&operator = (const Circle &c)
    {
        this->radius = c.radius;
        this->round = c.round;
        this->area =c.area;
        return *this;
    }
    double get_round()
    {
        return round;
    }
    double get_area()
    {
        return area;
    }
};
class Rect:public Shape
{
private:
    double length;
    double width;
public:
    Rect(double l,double w):length(l),width(w)
    {
        round = (l+w)*2;
        area = l*w;
    }
    Rect(Rect *c):Shape(c->round,c->area),length(c->length),width(c->width){}
    ~Rect(){}
    Rect& operator=(const Rect &c)
    {
        this->length =c.length;
        this->width =c.width;
        this->round = c.round;
        this->area = c.area;
        return *this;
    }
    double get_round()
    {
        return round;
    }
    double get_area()
    {
        return area;
    }

};

int main()
{
    Circle c1(6);
    cout<<"c1周长="<<c1.get_round()<<endl;
    cout<<"c1面积="<<c1.get_area()<<endl;

    Circle c2(c1);
    cout<<"c2周长="<<c2.get_round()<<endl;
    cout<<"c2面积="<<c2.get_area()<<endl;

    Rect r1(4,6);
    cout<<"r1周长="<<r1.get_round()<<endl;
    cout<<"r1面积="<<r1.get_area()<<endl;

    Rect r2(r1);
    cout<<"r2周长="<<r2.get_round()<<endl;
    cout<<"r2面积="<<r2.get_area()<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值