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

运行截图

思维导图

模拟面试

相关推荐
HugoStudio_SWAN1 小时前
洛谷 P1420 / P1179 / B4262 最长连号、数字统计与词频统计——统计的三种面孔
c++·学习·程序人生·算法
raindayinrain2 小时前
c++并发
c++
j7~2 小时前
【C++】智能指针的使用及其原理--详解
c++·c++11·内存泄漏·智能指针·raii·boost智能指针
Persistent的粽子!4 小时前
C++:类与对象(一)
开发语言·c++·经验分享·笔记
库玛西4 小时前
深入浅出Linux select网络模型:从底层位图原理到C++面向对象高级封装
linux·服务器·网络·c++·ubuntu
2402_882893864 小时前
深入浅出 unordered_map 与 unordered_set——从使用到底层差异
c++·哈希·unordered_map·unordered_set
今天要早睡_5 小时前
C++ 核心语法速过:命名空间、引用、函数重载与 nullptr 深度解析
android·java·c++
汉克老师5 小时前
CSP-J 初赛(以满分为目标):第二十七课 《图的遍历——BFS广度优先搜索——像“水波纹”一样,一层一层地搜索》
c++·csp-j·小学生·学c++编程
闻缺陷则喜何志丹5 小时前
【动态规划】P3609 [USACO17JAN] Hoof, Paper, Scissor G
c++·算法·动态规划·洛谷
汉克老师6 小时前
CSP-J 初赛(以满分为目标):第二十六课 《图的遍历——DFS深度优先搜索——从“树的先序遍历”走进真正的图世界》
c++·csp-j·小学生·学c++编程