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;
}
相关推荐
phoenix@Capricornus1 分钟前
反向传播算法——矩阵形式递推公式——ReLU传递函数
算法·机器学习·矩阵
Inverse16222 分钟前
C语言_动态内存管理
c语言·数据结构·算法
数据与人工智能律师24 分钟前
虚拟主播肖像权保护,数字时代的法律博弈
大数据·网络·人工智能·算法·区块链
wuqingshun3141591 小时前
蓝桥杯 16. 外卖店优先级
c++·算法·职场和发展·蓝桥杯·深度优先
YouQian7722 小时前
2025春训第十九场
算法
CodeJourney.2 小时前
基于MATLAB的生物量数据拟合模型研究
人工智能·爬虫·算法·matlab·信息可视化
Epiphany.5562 小时前
素数筛(欧拉筛算法)
c++·算法·图论
爱吃涮毛肚的肥肥(暂时吃不了版)2 小时前
项目班——0510——JSON网络封装
c++·算法·json
liang_20262 小时前
【HT周赛】T3.二维平面 题解(分块:矩形chkmax,求矩形和)
数据结构·笔记·学习·算法·平面·总结
緈福的街口2 小时前
【leetcode】2900. 最长相邻不相等子序列 I
算法·leetcode·职场和发展