c++day5

实现一个图形类(Shape),包含受保护成员属性:周长、面积,

公共成员函数:特殊成员函数书写

定义一个圆形类(Circle),继承自图形类,包含私有属性:半径

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

cpp 复制代码
#include <iostream>

using namespace std;

class Shape
{
protected:
    float perimeter;  //周长
    float area;//面积

public:
    //无参构造函数
    Shape()
    {
        cout<<"Shape:无参构造"<<endl;
    }
    //有参构造函数
    Shape(float a,float b):perimeter(a),area(b)
    {
        cout<<"Shape:有参构造"<<endl;
    }
    //析构函数
    ~Shape()
    {
        cout<<"Shape:析构函数"<<endl;
    }
    //拷贝构造函数
    Shape(const Shape &other):perimeter(other.perimeter),area(other.area)
    {
        cout<<"Shape:拷贝构造函数"<<endl;
    }
    //定义拷贝赋值函数
    Shape & operator=(const Shape &other)
    {
        if(this != &other)          //确定不是自己给自己赋值
        {
            this->perimeter = other.perimeter;
            this->area = other.area;
        }
        cout<<"Shape: 拷贝赋值函数"<<endl;
        return  *this;             //返回自身引用
    }

    void show()
    {
        cout<<"*******************"<<endl;
        cout<<"该图形的周长为"<<perimeter<<endl;
        cout<<"该图形的面积为"<<area<<endl;
        cout<<"*******************"<<endl;
    }

};
class Circle:public Shape
{
private:
    int radius;
public:
    //无参构造函数
    Circle()
    {
        cout<<"Circle:无参构造"<<endl;
    }
    //有参构造函数
    Circle(int r):radius(r)
    {
        cout<<"Circle:有参构造"<<endl;
    }
    //拷贝构造函数
    Circle(const Circle &other):radius(other.radius)
    {
        cout<<"Circle:拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Circle & operator=(const Circle &other)
    {
        if(this != &other)          //确定不是自己给自己赋值
        {
            this->radius = other.radius;
        }
        cout<<"Circle: 拷贝赋值函数"<<endl;
        return  *this;             //返回自身引用
    }
    //析构函数
    ~Circle()
    {
        cout<<"Circle:析构函数"<<endl;
    }
    //获取周长
    using Shape::perimeter;
    float perimeter_get()
    {
        this->perimeter=2*3.14*radius;
        return perimeter;
    }
    //获取面积函数
    using Shape::area;
    float area_get()
    {
        this->area=3.14*radius*radius;
        return area;
    }
};
class Rect:public Shape
{
private:
    int length;
    int hight;
public:
    //无参构造函数
    Rect()
    {
        cout<<"Rect:无参构造"<<endl;
    }
    //有参构造函数
    Rect(int l,int h):length(l),hight(h)
    {
        cout<<"Rect:有参构造"<<endl;
    }
    //拷贝构造函数
    Rect(const Rect &other):length(other.length),hight(other.hight)
    {
        cout<<"Rect:拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Rect & operator=(const Rect &other)
    {
        if(this != &other)          //确定不是自己给自己赋值
        {
            this->length = other.length;
            this->hight = other.hight;
        }
        cout<<"Rect: 拷贝赋值函数"<<endl;
        return  *this;             //返回自身引用
    }
    //析构函数
    ~Rect()
    {
        cout<<"Rect:析构函数"<<endl;
    }
    //获取周长
    float perimeter_get()
    {
        this->perimeter=2*(length+hight);
        return this->perimeter;
    }
    //获取面积函数
    float area_get()
    {
        this->area=length*hight;
        return this->area;
    }
};
int main()
{
    Circle c1(5);
    c1.perimeter_get();//获取圆形周长
    c1.area_get();//获取圆形面积
    c1.show();//打印周长面积
    Rect r1(4,5);
    r1.perimeter_get();//获取矩形周长
    r1.area_get();//获取矩形面积
    r1.show();//打印周长面积
    return 0;
}
相关推荐
No0d1es4 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
大阳1236 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
weixin_307779137 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
学行库小秘8 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归
没落之殇8 小时前
基于C语言实现的HRV分析方法 —— 与Kubios和MATLAB对比
算法
秋难降8 小时前
线段树的深度解析(最长递增子序列类解题步骤)
数据结构·python·算法
楚韵天工8 小时前
基于GIS的无人机模拟飞行控制系统设计与实现
深度学习·算法·深度优先·无人机·广度优先·迭代加深·图搜索算法
你也向往长安城吗9 小时前
推荐一个三维导航库:three-pathfinding-3d
javascript·算法
百度智能云10 小时前
VectorDB+FastGPT一站式构建:智能知识库与企业级对话系统实战
算法
AI小白的Python之路11 小时前
数据结构与算法-排序
数据结构·算法·排序算法