【C++】day5学习成果:继承和六个特殊成员函数的代码应用、静态成员 继承 类的关系模型 类的继承步骤的思维导图。

1.代码题:实现一个图形类(Shape),包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

#include <iostream>

using namespace std;

class Shape
{
protected:
    double perimite;    //周长
    double area;        //面积
public:
    Shape()
    {
        cout<<"Shape::无参构造函数"<<endl;
    }
    Shape(double p,double a):perimite(p),area(a)
    {
        cout<<"Shape::有参构造函数"<<endl;
    }
    ~Shape()
    {
        cout<<"Shape::析构函数"<<endl;
    }
    //拷贝构造函数
    Shape(const Shape &other):perimite(other.perimite),area(other.area)
    {
        cout<<"Shape::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Shape & operator=(const Shape &other)
    {
        cout<<"Shape::拷贝赋值函数"<<endl;
        if(this!=&other)
        {
            this->perimite=other.perimite;
            this->area=other.area;
        }
        return *this;
    }
    //移动赋值函数
    Shape &operator=(Shape &&other)
    {
        cout<<"Shape::移动赋值函数"<<endl;
        this->perimite=other.perimite;
        this->area=other.area;
        return *this;
    }

};

class Circle:public Shape
{
private:
    double radius;
public:
    Circle()
    {
        cout<<"Circle::无参构造函数"<<endl;
    }
    Circle(double r):radius(r)
    {
        cout<<"Circle::有参构造函数"<<endl;
    }
    ~Circle()
    {
        cout<<"Circle::析构函数"<<endl;
    }
    //拷贝构造函数
    Circle(const Circle &other):
        Shape(other.perimite,other.area),radius(other.radius)
    {
        cout<<"Circle::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Circle & operator=(const Circle &other)
    {
        cout<<"Circle::拷贝赋值函数"<<endl;
        if(this!=&other)
        {
            this->radius=other.radius;
            perimite=other.perimite;
            area=other.area;
        }
        return *this;
    }
    //移动赋值函数
    Circle &operator=(Circle &&other)
    {
        cout<<"Circle::移动赋值函数"<<endl;
        this->radius=other.radius;
        perimite=other.perimite;
        area=other.area;
        return *this;
    }
    //周长的获取
    double get_perimite()
    {
        perimite=this->radius*2*3.14;
        return perimite;
    }
    //面积的获取
    double get_area()
    {
        area=this->radius*this->radius*3.14;
        return area;
    }
};

class Rect:public Shape
{
private:
    double width;       //长
    double height;      //宽
public:
    Rect()
    {
        cout<<"Rect::无参构造函数"<<endl;
    }
    Rect(double w,double h):width(w),height(h)
    {
        cout<<"Rect::有参构造函数"<<endl;
    }
    ~Rect()
    {
        cout<<"Rect::析构函数"<<endl;
    }
    //拷贝构造函数
    Rect(const Rect &other):
        Shape(other.perimite,other.area),
        width(other.width),height(other.height)
    {
        cout<<"Rect::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Rect & operator=(const Rect &other)
    {
        cout<<"Rect::拷贝赋值函数"<<endl;
        if(this!=&other)
        {
            this->width=other.width;
            this->height=other.height;
            perimite=other.perimite;
            area=other.area;
        }
        return *this;
    }
    //移动赋值函数
    Rect &operator=(Rect &&other)
    {
        cout<<"Rect::移动赋值函数"<<endl;
        this->width=other.width;
        this->height=other.height;
        perimite=other.perimite;
        area=other.area;
        return *this;
    }
    //周长的获取
    double get_perimite()
    {
        perimite=(this->width+this->height)*2;
        return perimite;
    }
    //面积的获取
    double get_area()
    {
        area=(this->width)*(this->height);
        return area;
    }
};

int main()
{
    Circle c1(5);
    cout << "c1.perimite="<<c1.get_perimite() << endl;
    cout << "c1.area="<<c1.get_area() << endl;
    Circle c2=c1;
    cout << "c2.perimite="<<c2.get_perimite() << endl;
    cout << "c2.area="<<c2.get_area() << endl;
    Circle c3;
//    cout << "c3.perimite="<<c3.get_perimite() << endl;
//    cout << "c3.area="<<c3.get_area() << endl;
    c3=c1;
    cout << "c3.perimite="<<c3.get_perimite() << endl;
    cout << "c3.area="<<c3.get_area() << endl;
    cout << "*************************************"<<endl;
    Rect r1(10,5);
    cout << "r1.perimite="<<r1.get_perimite() << endl;
    cout << "r1.area="<<r1.get_area() << endl;
    Rect r2=r1;
    cout << "r2.perimite="<<r2.get_perimite() << endl;
    cout << "r2.area="<<r2.get_area() << endl;
    Rect r3;
    r3=r1;
    cout << "r3.perimite="<<r3.get_perimite() << endl;
    cout << "r3.area="<<r3.get_area() << endl;

    return 0;
}

运行结果:
运行结果
2.学习思维导图

c++day5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值