实现一个图形类(Shape),包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- #include
- #define PI 3.14//π
- using namespace std;
- //图形类
- class Shape
- {
- protected:
- double cir;//周长
- double area;//面积
- public:
- //无参构造
- Shape()
- {
- cout<<"Shape::无参构造"<
- }
- //有参构造
- Shape(double cir,double area):cir(cir),area(area)
- {
- cout<<"Shape::有参构造"<
- }
- //析构函数
- ~Shape()
- {
- cout<<"Shape::析构函数"<
- }
- //拷贝构造函数
- Shape(const Shape &other):cir(other.cir),area(other.area)
- {
- cout<<"Shape::拷贝构造函数"<
- }
- //拷贝赋值函数
- Shape & operator=(const Shape &other)
- {
- if(this!=&other)//确定不是自己给自己赋值
- {
- this->cir=other.cir;
- this->area=other.area;
- }
- cout<<"Shape::拷贝赋值函数"<
- return *this;
- }
- };
- class Circle:public Shape
- {
- private:
- double rad;//半径
- public:
- //无参构造
- Circle()
- {
- cout<<"Circle::无参构造"<
- }
- //有参构造
- Circle(double rad):rad(rad)
- {
- cout<<"Cricle::有参构造"<
- }
- //析构函数
- ~Circle()
- {
- cout<<"Circle::析构函数"<
- }
- //拷贝构造函数
- Circle(const Circle &other):rad(other.rad)
- {
- cout<<"Cricle::拷贝构造函数"<
- }
- //拷贝赋值函数
- Circle & operator=(const Circle &other)
- {
- if(this!=&other)//确定不是自己给自己赋值
- {
- this->rad=other.rad;
- }
- cout<<"Circle::拷贝赋值函数"<
- return *this;
- }
- //计算周长函数
- void get_cir()
- {
- cir=2*PI*rad;
- cout<<"cir="<
- }
- //计算面积函数
- void get_area()
- {
- area=PI*rad*rad;
- cout<<"area="<
- }
- };
- class Rect:public Shape
- {
- private:
- double length;//长度
- double width;//宽度
- public:
- //无参构造
- Rect()
- {
- cout<<"Rect::无参构造"<
- }
- //有参构造
- Rect(double length,double width):length(length),width(width)
- {
- cout<<"Rect::有参构造"<
- }
- //析构函数
- ~Rect()
- {
- cout<<"Rect::析构函数"<
- }
- //拷贝构造函数
- Rect(const Rect &other):length(other.length),width(other.width)
- {
- cout<<"Rect::拷贝构造函数"<
- }
- //拷贝赋值函数
- Rect & operator=(const Rect &other)
- {
- if(this!=&other)//确定不是自己给自己赋值
- {
- this->length=other.length;
- this->width=other.width;
- }
- cout<<"Rect::拷贝赋值函数"<
- return *this;
- }
- //计算周长函数
- void get_cir()
- {
- cir=(length+width)*2;
- cout<<"cir="<
- }
- //计算面积函数
- void get_area()
- {
- area=length*width;
- cout<<"area="<
- }
- };
- 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;
- }
结果:

思维导图:

-
相关阅读:
CAD Assistant - 3D模型格式转换利器
基于机器学习、遥感和Penman-Monteith方程的农田蒸散发混合模型研究_刘燕_2022
Godot2D角色导航-自动寻路教程(Godot实现角色随鼠标移动)
软航H5 PDF签章产品经nginx代理之后浏览器中PDF盖章时提示:签章失败:网络错误 的问题排查及解决办法
CSDN博客专家和万粉博主纪念章
ElasticSearch安装(Windows环境)及基本命令操作
【数据结构】树与二叉树(二):树的表示C语言:树形表示法、嵌套集合表示法、嵌套括号表示法 、凹入表示法
大地水准面 地球椭球体 大地基准面 地图投影理解
javascript
【乐鑫 Demo】|ESP32-S3-BOX-3 惊喜开箱
-
原文地址:https://blog.csdn.net/m0_59031281/article/details/132839845
-
最新文章
-
【FHE】我们如何实现同态加密推理(八):SiLU 的密文化——两条路径,和一个 8 字节的开关
SQLcl 中 c/old/new 替换变量为何失效?先定位再替换
不让模型写作文,直接从它脑子里读答案:Jev 决策在 .NET 的两条路线
Halo插件:支持GitHub的图床附件策略
电视投屏界的“一股清流”!SimpleTVCast 6.0.0-beta 抢先体验:无需TV端App,界面大换血!✨
华三VXLAN集中式网关实战指南:Spine多活网关 + 双活接入(M-LAG)
软件更好写之后口碑会更重要
大 O、大 Omega、大 Theta 的区别,O(n) 复杂度是什么意思
Java标签跳转:一个鲜为人知但却很实用的小技巧
多智能体系统论文速读