实现一个图形类(Shape),包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- /*
- 实现一个图形类(Shape),包含受保护成员属性:周长、面积,
- 公共成员函数:特殊成员函数书写
- 在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- */
-
- #include
-
- #define pi 3.14
-
- using namespace std;
-
-
- //定义一个图形类
- class Shape
- {
- protected:
- double perimeter; //周长
- double area; //面积
- public:
- //构造函数
- Shape(){cout<<"Shape:无参构造"<
//无参构造 - Shape(double p, double a):perimeter(p),area(a) //有参构造
- {
- cout<<"Shape:有参构造"<
- }
-
- //拷贝构造函数
- Shape(const Shape &other):perimeter(other.perimeter),area(other.area)
- {
- cout<<"Shape:拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Shape &operator=(const Shape &other)
- {
- if(this != &other)
- {
- this->perimeter = other.perimeter;
- this->area = other.area;
-
- }
- cout<<"Shape:拷贝赋值函数"<
- return *this;
- }
-
- //移动赋值函数
- Shape &operator=(Shape &&other)
- {
- this->perimeter = move(other.perimeter);
- this->area = move(other.area);
-
- return *this;
- }
- };
-
-
- /*
- 定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
- 公共成员函数:特殊成员函数、以及获取周长、获取面积函数
- */
- class Circle:public Shape
- {
- private:
- double radius; //半径
- public:
- //构造函数
- Circle(){cout<<"Shape:无参构造"<
//无参构造 - Circle(double r):radius(r) //有参构造
- {
- cout<<"Shape:有参构造"<
- }
-
- //拷贝构造函数
- Circle(const Circle &other):radius(other.radius)
- {
- cout<<"Shape:拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Circle &operator=(const Circle &other)
- {
- if(this != &other)
- {
- this->radius = other.radius;
-
- }
- cout<<"Shape:拷贝赋值函数"<
- return *this;
- }
-
- //移动赋值函数
- Circle &operator=(Circle &&other)
- {
- this->radius = move(other.radius);
-
- return *this;
- }
-
- //求周长函数
- int perimeter(double r)
- {
- double D = 0;
- D = 2*r*pi;
-
- cout<<"周长 = "<
-
- return 0;
- }
-
- //求面积函数
- int area(double r)
- {
- double C = 0;
- C = r*r*pi;
-
- cout<<"面积 = "<
-
- return 0;
- }
-
- };
-
- /*
- 定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
- 公共成员函数:特殊成员函数、以及获取周长、获取面积函数
- */
- class Rect:public Shape
- {
- private:
- double length;
- double width;
- public:
- //构造函数
- Rect(){cout<<"Shape:无参构造"<
//无参构造 - Rect(double l, double w):length(l),width(w) //有参构造
- {
- cout<<"Shape:有参构造"<
- }
-
- //拷贝构造函数
- Rect(const Rect &other):length(other.length),width(other.width)
- {
- cout<<"Shape:拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Rect &operator=(const Rect &other)
- {
- if(this != &other)
- {
- this->length = other.length;
- this->width = other.width;
- }
- cout<<"Shape:拷贝赋值函数"<
- return *this;
- }
-
- //移动赋值函数
- Rect &operator=(Rect &&other)
- {
- this->length = move(other.length);
- this->width = move(other.width);
-
- return *this;
- }
-
- //求周长函数
- int perimeter(double l, double w)
- {
- double D = 0;
- D = 2*(l+w);
-
- cout<<"周长 = "<
-
- return 0;
- }
-
- //求面积函数
- int area(double l, double w)
- {
- double C = 0;
- C = l*w;
-
- cout<<"面积 = "<
-
- return 0;
- }
- };
-
- int main()
- {
- Circle c1;
- c1.perimeter(5);
- c1.area(3);
-
- Rect r1;
- r1.perimeter(4,6);
- r1.area(2.5,6);
-
-
- return 0;
- }
-
相关阅读:
【计算机网络】Tomcat和Servlet基础知识汇总
敏捷思维&敏捷管理工具
性能测试中故障排查及解决方法
ctfshow-web5(md5弱比较)
Keycloak之Gerrit安装与集成-yellowcong
【Matlab】智能优化算法_麻雀搜索算法SSA
java毕业设计创新创业竞赛管理系统2021Mybatis+系统+数据库+调试部署
vue3-基础知识(0) - vue2和vue3基本比较
【C++】:日期类实现
Jmeter —— 自动录制脚本
-
原文地址:https://blog.csdn.net/weixin_65188498/article/details/132839800