C++&QT day 5

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

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

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

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

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

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

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

cpp 复制代码
#include <iostream>
#define PI 3.14//π
using namespace std;
//图形类
class Shape
{
protected:
    double cir;//周长
    double area;//面积
public:
    //无参构造
    Shape()
    {
        cout<<"Shape::无参构造"<<endl;
    }
    //有参构造
    Shape(double cir,double area):cir(cir),area(area)
    {
        cout<<"Shape::有参构造"<<endl;
    }
    //析构函数
    ~Shape()
    {
        cout<<"Shape::析构函数"<<endl;
    }
    //拷贝构造函数
    Shape(const Shape &other):cir(other.cir),area(other.area)
    {
        cout<<"Shape::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Shape & operator=(const Shape &other)
    {
        if(this!=&other)//确定不是自己给自己赋值
        {
            this->cir=other.cir;
            this->area=other.area;
        }
        cout<<"Shape::拷贝赋值函数"<<endl;
        return *this;
    }
};
class Circle:public Shape
{
private:
    double rad;//半径
public:
    //无参构造
    Circle()
    {
        cout<<"Circle::无参构造"<<endl;
    }
    //有参构造
    Circle(double rad):rad(rad)
    {
        cout<<"Cricle::有参构造"<<endl;
    }
    //析构函数
    ~Circle()
    {
        cout<<"Circle::析构函数"<<endl;
    }
    //拷贝构造函数
    Circle(const Circle &other):rad(other.rad)
    {
        cout<<"Cricle::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Circle & operator=(const Circle &other)
    {
        if(this!=&other)//确定不是自己给自己赋值
        {
            this->rad=other.rad;
        }
        cout<<"Circle::拷贝赋值函数"<<endl;
        return *this;
    }
    //计算周长函数
    void get_cir()
    {
        cir=2*PI*rad;
        cout<<"cir="<<cir<<endl;
    }
    //计算面积函数
    void get_area()
    {
        area=PI*rad*rad;
        cout<<"area="<<area<<endl;
    }
};
class Rect:public Shape
{
private:
    double length;//长度
    double width;//宽度
public:
    //无参构造
    Rect()
    {
        cout<<"Rect::无参构造"<<endl;
    }
    //有参构造
    Rect(double length,double width):length(length),width(width)
    {
        cout<<"Rect::有参构造"<<endl;
    }
    //析构函数
    ~Rect()
    {
        cout<<"Rect::析构函数"<<endl;
    }
    //拷贝构造函数
    Rect(const Rect &other):length(other.length),width(other.width)
    {
        cout<<"Rect::拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Rect & operator=(const Rect &other)
    {
        if(this!=&other)//确定不是自己给自己赋值
        {
            this->length=other.length;
            this->width=other.width;
        }
        cout<<"Rect::拷贝赋值函数"<<endl;
        return *this;
    }
    //计算周长函数
    void get_cir()
    {
        cir=(length+width)*2;
        cout<<"cir="<<cir<<endl;
    }
    //计算面积函数
    void get_area()
    {
        area=length*width;
        cout<<"area="<<area<<endl;
    }
};
int main()
{
    //实例一个圆
    Circle c(3);
    c.get_cir();
    c.get_area();
    //实例拷贝构造函数
    Circle c1=c;
    c1.get_cir();
    c1.get_area();
    //实例一个矩形
    Rect r(3,4);
    r.get_cir();
    r.get_area();
    //实例拷贝赋值函数
    Rect r1;
    r1=r;
    r1.get_cir();
    r1.get_area();
    return 0;
}

结果:

思维导图:

相关推荐
还债大湿兄1 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang5 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc5 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程