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


-
相关阅读:
【100个 Unity实用技能】| C#中 Add 和 AddRange 的区别 及 使用示例
RESTful API,以及如何使用它构建 web 应用程序
高性能mysql-查询性能优化
hive on spark 之通过spark web ui详解数据倾斜及思路历程
MapReduce计算流程
input空格回车输入标签
CVE-2022-22947:Spring Cloud Gateway 远程代码执行漏洞复现及修复建议
小程序中如何查看会员卡的注册时间
下一站——Java,准备好踏上新征程了?少年!!!!
思维模型 留白效应
-
原文地址:https://blog.csdn.net/qq_53478460/article/details/132839875
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU