- #include
-
- using namespace std;
-
- class Shape
- {
- protected:
- double c;
- double s;
- public:
- //构造函数
- Shape(){
- cout<<"无参构造"<
- }
- Shape (double c,double s):c(c),s(s)
- {
- cout<<"有参构造"<
- }
-
- //析构函数
- ~Shape(){cout<<"析构函数"<
-
- //拷贝构造函数
- Shape(const Shape &other)
- {
- c=other.c;
- s=other.s;
- cout<<"拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Shape &operator=(const Shape &other)
- {
- c=other.c;
- s=other.s;
- cout<<"拷贝赋值函数"<
- return *this;
- }
-
- };
-
- class Circle:public Shape
- {
- private:
- double r;
- public:
- //构造函数
- Circle(){
- cout<<"圆的无参构造"<
- }
- Circle (double c, double s,double r):Shape(c,s),r(r)
- {
- cout<<"圆的有参构造"<
- }
- Circle(double r):r(r)
- {
- c=2*3.14*r;
- s=3.14*r*r;
- cout<<"圆的一个参数的有参构造"<
- }
-
- //析构函数
- ~Circle(){cout<<"圆的析构函数"<
-
- //拷贝构造函数
- Circle(const Circle &other)
- {
- r=other.r;
- c=other.c;
- s=other.s;
- cout<<"圆的拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Circle &operator=(const Circle &other)
- {
- r=other.r;
- c=other.c;
- s=other.s;
- cout<<"圆的拷贝赋值函数"<
- return *this;
- }
-
- //获取周长
- double get_C()
- {
- cout<<"圆的周长="<
- return c;
- }
-
- //获取面积函数
- double get_S()
- {
- cout<<"圆的面积="<
- return s;
- }
- };
-
- class Rect:public Shape
- {
- private:
- double l;
- double w;
- public:
- //构造函数
- Rect(){
- cout<<"矩形的无参构造"<
- }
- Rect (double c, double s,double l,double w):Shape(c,s),l(l),w(w)
- {
- cout<<"矩形的有参构造"<
- }
- Rect(double l,double w):l(l),w(w)
- {
- c=2*(l+w);
- s=l*w;
- cout<<"矩形的长宽参数的有参构造"<
- }
-
- //析构函数
- ~Rect(){cout<<"矩形的析构函数"<
-
- //拷贝构造函数
- Rect(const Rect &other)
- {
- l=other.l;
- w=other.w;
- c=other.c;
- s=other.s;
- cout<<"矩形的拷贝构造函数"<
- }
-
- //拷贝赋值函数
- Rect &operator=(const Rect &other)
- {
- l=other.l;
- w=other.w;
- c=other.c;
- s=other.s;
- cout<<"矩形的拷贝赋值函数"<
- return *this;
- }
-
- //获取周长
- double get_C()
- {
- cout<<"矩形的周长="<
- return c;
- }
-
- //获取面积函数
- double get_S()
- {
- cout<<"矩形面积="<
- return s;
- }
- };
-
- int main()
- {
- Shape s1(12,9); //有参构造
- Shape s2=s1; //拷贝构造函数
- Shape s3; //无参构造
- s3=s1; //拷贝赋值函数
-
- Circle c1(6*3.14,9*3.14,3); //圆的有参构造
- Circle c2=c1; //圆的拷贝构造函数
- Circle c3; //圆的无参构造
- c3=c1; //圆的拷贝赋值函数
- c3.get_C(); //获取周长
- Circle c4(4); //圆的一个参数的有参构造
- c4.get_C(); //获取周长
- c4.get_S(); //获取面积
-
- Rect r1(3,4); //矩形的长宽两个参数的有参构造
- Rect r2=r1; //矩形的拷贝构造函数
- Rect r3; //矩形的无参构造
- r3=r1; //矩形的拷贝赋值函数
- r3.get_C(); //获取矩形的周长
- r3.get_S(); //获取矩形的面积
-
- return 0;
- }
思维导图
-
相关阅读:
web3 前端dapp从redux过滤出 (我创建与别人创建)正在执行的订单 并展示在Table上
[正式学习java③]——字符串在内存中的存储方式、为什么字符串不可变、字符串的拼接原理,键盘录入的小细节。
Elasticsearch7.9.3保姆级安装教程
四 Pytorch构建分类器
Qt 实现侧边栏滑出菜单效果
为什么使用Spring Boot?
罗克韦尔AB PLC RSLogix5000中定时器指令使用方法介绍
三星泄露微软 Copilot 新功能:用自然语言操控各种功能
Java开发中的工作流程和步骤
Codeforces Round #800 (Div. 2)
-
原文地址:https://blog.csdn.net/weixin_58469613/article/details/132839563