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;
}
相关推荐
练习时长一年11 分钟前
LeetCode热题100(杨辉三角)
算法·leetcode·职场和发展
lzllzz2328 分钟前
bellman_ford算法
算法
栈与堆39 分钟前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
sunfove40 分钟前
麦克斯韦方程组 (Maxwell‘s Equations) 的完整推导
线性代数·算法·矩阵
Rui_Freely1 小时前
Vins-Fusion之 SFM准备篇(十二)
人工智能·算法·计算机视觉
yyy(十一月限定版)1 小时前
matlab矩阵的操作
算法·matlab·矩阵
努力学算法的蒟蒻1 小时前
day58(1.9)——leetcode面试经典150
算法·leetcode·面试
txinyu的博客2 小时前
map和unordered_map的性能对比
开发语言·数据结构·c++·算法·哈希算法·散列表
搞笑症患者2 小时前
压缩感知(Compressed Sensing, CS)
算法·最小二乘法·压缩感知·正交匹配追踪omp·迭代阈值it算法
im_AMBER2 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法