
实现一个图形类(Shape),包含受保护成员属性:周长、面积,公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径,公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度,公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
- #include
-
- #define PI 3.14
-
- using namespace std;
-
- class Shape{
- protected:
- double area; //面积
- double round; //周长
- public:
- //无参构造
- Shape(){}
- //有参构造
- Shape(double a,double rd):area(a),round(rd){}
- //拷贝构造函数
- Shape(const Shape &other):area(other.area),round(other.round){}
- //析构函数
- ~Shape(){}
- void show(){
- cout<<"*******************"<
- cout<<"该图形的周长为"<
- cout<<"该图形的面积为"<
- }
- };
-
- class Circle:public Shape{
- private:
- int radius;
- public:
- //无参构造
- Circle(){}
- //有参构造
- Circle(int rs):radius(rs){}
- //拷贝构造函数
- Circle(const Circle &other):Shape(Shape(other.area,other.round)),radius(other.radius){}
- //析构函数
- ~Circle(){}
- //获取周长函数
- void get_round(){
- round=2*PI*radius;
- }
- //获取面积函数
- void get_area(){
- area=PI*radius*radius;
- }
- };
-
- class Rect:public Shape{
- private:
- int length; //长度
- int width; //宽度
- public:
- //无参构造
- Rect(){}
- //有参构造
- Rect(int l,int w):length(l),width(w){}
- //拷贝构造函数
- Rect(const Rect &other):Shape(Shape(other.area,other.round)),length(other.length),width(other.width){}
- //析构函数
- ~Rect(){}
- //获取周长函数
- void get_round(){
- round=2*(length+width);
- }
- //获取面积函数
- void get_area(){
- area=length*width;
- }
- };
-
- int main()
- {
- Circle c(5);
- c.get_round();
- c.get_area();
- c.show();
- Rect r(4,7);
- r.get_area();
- r.get_round();
- r.show();
- return 0;
- }
-
相关阅读:
阿尔茨海默病中的人类连接组及它与生物标记物和遗传学的关系
安卓玩机-----反编译apk 修改apk 去广告 去弹窗等操作中的一些常识
[题] 分解质因数 #质数(素数)
103.(cesium之家)cesium蜂巢图(正方形)
利用大模型MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7实现零样本分类
写读后感的时候,可以适当地引用书中的内容吗?
自定义node版本,实现node多版本控制
自然语言生成技术现状调查:核心任务、应用和评估(2)
文心一言 vs GPT-4 —— 全面横向比较
【Linux网络】UdpSocket
-
原文地址:https://blog.csdn.net/qq_53268516/article/details/132839175