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;
}

运行截图

思维导图

模拟面试

相关推荐
mit6.8244 小时前
[openvela] Hello World :从零开始的完整实践与问题复盘
c++·嵌入式硬件
啊阿狸不会拉杆5 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路6 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程7 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆7 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆7 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...11 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞11 小时前
Qt——文件操作
开发语言·c++·qt
十五年专注C++开发11 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
winds~12 小时前
【git】 撤销revert一次commit中的某几个文件
linux·c++