实现一个图形类(Shape),包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- #include
-
- using namespace std;
-
- class Shape
- {
- protected:
- float perimeter; //周长
- float area;//面积
-
- public:
- //无参构造函数
- Shape()
- {
- cout<<"Shape:无参构造"<
- }
- //有参构造函数
- Shape(float a,float b):perimeter(a),area(b)
- {
- cout<<"Shape:有参构造"<
- }
- //析构函数
- ~Shape()
- {
- 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; //返回自身引用
- }
-
- void show()
- {
- cout<<"*******************"<
- cout<<"该图形的周长为"<
- cout<<"该图形的面积为"<
- cout<<"*******************"<
- }
-
- };
- class Circle:public Shape
- {
- private:
- int radius;
- public:
- //无参构造函数
- Circle()
- {
- cout<<"Circle:无参构造"<
- }
- //有参构造函数
- Circle(int r):radius(r)
- {
- cout<<"Circle:有参构造"<
- }
- //拷贝构造函数
- Circle(const Circle &other):radius(other.radius)
- {
- cout<<"Circle:拷贝构造函数"<
- }
- //拷贝赋值函数
- Circle & operator=(const Circle &other)
- {
- if(this != &other) //确定不是自己给自己赋值
- {
- this->radius = other.radius;
- }
- cout<<"Circle: 拷贝赋值函数"<
- return *this; //返回自身引用
- }
- //析构函数
- ~Circle()
- {
- cout<<"Circle:析构函数"<
- }
- //获取周长
- using Shape::perimeter;
- float perimeter_get()
- {
- this->perimeter=2*3.14*radius;
- return perimeter;
- }
- //获取面积函数
- using Shape::area;
- float area_get()
- {
- this->area=3.14*radius*radius;
- return area;
- }
- };
- class Rect:public Shape
- {
- private:
- int length;
- int hight;
- public:
- //无参构造函数
- Rect()
- {
- cout<<"Rect:无参构造"<
- }
- //有参构造函数
- Rect(int l,int h):length(l),hight(h)
- {
- cout<<"Rect:有参构造"<
- }
- //拷贝构造函数
- Rect(const Rect &other):length(other.length),hight(other.hight)
- {
- cout<<"Rect:拷贝构造函数"<
- }
- //拷贝赋值函数
- Rect & operator=(const Rect &other)
- {
- if(this != &other) //确定不是自己给自己赋值
- {
- this->length = other.length;
- this->hight = other.hight;
- }
- cout<<"Rect: 拷贝赋值函数"<
- return *this; //返回自身引用
- }
- //析构函数
- ~Rect()
- {
- cout<<"Rect:析构函数"<
- }
- //获取周长
- float perimeter_get()
- {
- this->perimeter=2*(length+hight);
- return this->perimeter;
- }
- //获取面积函数
- float area_get()
- {
- this->area=length*hight;
- return this->area;
- }
- };
- int main()
- {
- Circle c1(5);
- c1.perimeter_get();//获取圆形周长
- c1.area_get();//获取圆形面积
- c1.show();//打印周长面积
- Rect r1(4,5);
- r1.perimeter_get();//获取矩形周长
- r1.area_get();//获取矩形面积
- r1.show();//打印周长面积
- return 0;
- }
-
相关阅读:
苹果跌落全球市值第一宝座;联想一员工侵占公司工时费近1000万;PHP 8.1.6 发布|极客头条
【嵌入式——QT】线程同步
给定n个结点m条边的简单无向图,判断该图是否存在鱼形状的子图:有一个环,其中有一个结点有另外两条边,连向不在环内的两个结点。若有,输出子图的连边
Ansible任务控制loop循环、when和block条件判断介绍演示
使用Puppeteer构建博客内容的自动标签生成器
数据结构中的判定转状态+扫描线:P1502
python读取execel表格(xls等格式)转换为csv,并且加载到hive表中
字节一面:说说HTTP 常见的状态码有哪些,适用场景?
【SwitchyOmega】SwitchyOmega 安装及使用
CSS 实现跳动的方块动画
-
原文地址:https://blog.csdn.net/wdc857/article/details/132839548