实现一个图形类(Shape) ,包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle) ,继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- #include
- #define pai 3.14159
- using namespace std;
- //图形类
- class Shape
- {
- protected:
- double cir; //周长
- double area; //面积
- public:
- //无参构造函数
- Shape(){}
- //有参构造函数
- Shape(double a,double b):cir(a),area(b)
- {
- cout<<"Shape有参构造函数"<
- }
- //析构函数
- ~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;
- return *this;
- }
- //移动赋值函数
- Shape &operator=(Shape &&other)
- {
- this->cir =other.cir;
- this->area =other.area;
- return *this;
- }
-
- };
-
- //圆形类,继承图形类
- class Circle:public Shape
- {
- private:
- double r; //半径
- public:
- //无参构造函数
- Circle(){}
- //有参构造函数
- Circle(double c):r(c)
- {
- cout<<"Circle有参构造函数"<
- }
- //析构函数
- ~Circle()
- {
- cout<<"Circle析构函数"<
- }
- //拷贝构造函数
- Circle(const Circle &other):r(other.r)
- {
- cout<<"拷贝构造函数"<
- }
- //定义拷贝赋值函数
- Circle &operator=(const Circle &other)
- {
- this->r =other.r;
-
- return *this;
- }
- //移动赋值函数
- Circle &operator=(Circle &&other)
- {
- this->r =other.r;
- return *this;
- }
-
- //获取周长
- double get_len()
- {
- cir = 2 * r * pai;
-
- return cir;
- }
- //获取面积
- double get_area()
- {
- area = pai * r * r;
- return area;
- }
-
- };
-
- //定义一个矩形类,继承自图形类
- class Rect:public Shape
- {
- private:
- double lenth; //长度
- double width; //宽度
- public:
- //无参构造函数
- Rect(){}
- //有参构造函数
- Rect(double l,double w):lenth(l),width(w)
- {
- cout<<"Rect有参构造函数"<
- }
- //析构函数
- ~Rect()
- {
- cout<<"Rect析构函数"<
- }
- //拷贝构造函数
- Rect(const Rect &other):lenth(other.lenth),width(other.width)
- {
- cout<<"拷贝构造函数"<
- }
- //定义拷贝赋值函数
- Rect &operator=(const Rect &other)
- {
- this->width =other.width;
- this->lenth = other.lenth;
-
- return *this;
- }
- //移动赋值函数
- Rect &operator=(Rect &&other)
- {
- this->width =other.width;
- this->lenth = other.lenth;
- return *this;
- }
- //获取周长
- double get_len()
- {
- cir = (width+lenth)*2;
- return cir;
- }
- //获取面积
- double get_area()
- {
- area = lenth * width;
- return area;
- }
-
-
-
-
-
- };
-
- int main()
- {
- Circle c1(5.3);
- cout<<"周长是"<
get_len()<<" 面积是:"<get_area()< - Circle c2(c1);
- cout<<"周长是"<
get_len()<<" 面积是:"<get_area()< - Circle c3 = (2);
- cout<<"周长是"<
get_len()<<" 面积是:"<get_area()< - cout<<"*********************************************************"<
- Rect r1(2.4,9.9);
- cout<<"Rect周长是"<
get_len()<<" Rect面积是:"<get_area()< - Rect r2(r1);
- cout<<"Rect周长是"<
get_len()<<" Rect面积是:"<get_area()< - Rect r3(1.2,3.4);
- cout<<"Rect周长是"<
get_len()<<" Rect面积是:"<get_area()< - Rect r4 = r3;
- cout<<"Rect周长是"<
get_len()<<" Rect面积是:"<get_area()< - return 0;
- }


-
相关阅读:
Leaflet 加载高德地图
C/C++使用Windows的API实现共享内存以及同步
tiup cluster upgrade
ES6——知识点记录
SpringCloud链路追踪SkyWalking-第四章-自定义链路追踪
企业员工内部培训系统开发
JAVASE(复习)——方法重载、数组篇
nvm:轻松管理多个 Node 版本 | 开源日报 No.80
2021-05-13 Redis面试题 为什么redis需要把所有数据放到内存中?
用户前台页面设计与实现——基于SpringBoot和Vue的后台管理系统项目系列博客(十九)
-
原文地址:https://blog.csdn.net/cscssacd/article/details/132839924