9月12日作业

作业代码

cpp 复制代码
#include <iostream>

using namespace std;

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

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

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

int main()
{
    Circle c(3);
    cout<<"c_cir = "<<c.get_cir()<<endl;
    cout<<"c_area = "<<c.get_area()<<endl;
    Rect r(3.5, 5.5);
    cout<<"r_cir = "<<r.get_cir()<<endl;
    cout<<"r_area = "<<r.get_area()<<endl;

    return 0;
}

运行截图

思维导图

模拟面试

相关推荐
李匠202420 分钟前
C++GO语言微服务之图片、短信验证码生成及存储
开发语言·c++·微服务·golang
ll7788115 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
我不想当小卡拉米5 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++
炎芯随笔6 小时前
【C++】【设计模式】生产者-消费者模型
开发语言·c++·设计模式
乌鸦9446 小时前
《类和对象(下)》
开发语言·c++·类和对象+
逐光沧海7 小时前
数据结构基础--蓝桥杯备考
数据结构·c++·算法·蓝桥杯
前进的程序员7 小时前
嵌入式开发中 C++ 跨平台开发经验与解决方案
开发语言·c++
菜一头包7 小时前
c++ std库中的文件操作学习笔记
c++·笔记·学习
吃个早饭9 小时前
2025年第十六届蓝桥杯大赛软件赛C/C++大学B组题解
c语言·c++·蓝桥杯
阿沁QWQ10 小时前
单例模式的两种设计
开发语言·c++·单例模式