• C++&QT day 5


    实现一个图形类(Shape),包含受保护成员属性:周长、面积,

                                                 公共成员函数:特殊成员函数书写

    定义一个圆形类(Circle),继承自图形类,包含私有属性:半径

                                                 公共成员函数:特殊成员函数、以及获取周长、获取面积函数

    定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度

                                              公共成员函数:特殊成员函数、以及获取周长、获取面积函数

    在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

    1. #include
    2. #define PI 3.14//π
    3. using namespace std;
    4. //图形类
    5. class Shape
    6. {
    7. protected:
    8. double cir;//周长
    9. double area;//面积
    10. public:
    11. //无参构造
    12. Shape()
    13. {
    14. cout<<"Shape::无参构造"<
    15. }
    16. //有参构造
    17. Shape(double cir,double area):cir(cir),area(area)
    18. {
    19. cout<<"Shape::有参构造"<
    20. }
    21. //析构函数
    22. ~Shape()
    23. {
    24. cout<<"Shape::析构函数"<
    25. }
    26. //拷贝构造函数
    27. Shape(const Shape &other):cir(other.cir),area(other.area)
    28. {
    29. cout<<"Shape::拷贝构造函数"<
    30. }
    31. //拷贝赋值函数
    32. Shape & operator=(const Shape &other)
    33. {
    34. if(this!=&other)//确定不是自己给自己赋值
    35. {
    36. this->cir=other.cir;
    37. this->area=other.area;
    38. }
    39. cout<<"Shape::拷贝赋值函数"<
    40. return *this;
    41. }
    42. };
    43. class Circle:public Shape
    44. {
    45. private:
    46. double rad;//半径
    47. public:
    48. //无参构造
    49. Circle()
    50. {
    51. cout<<"Circle::无参构造"<
    52. }
    53. //有参构造
    54. Circle(double rad):rad(rad)
    55. {
    56. cout<<"Cricle::有参构造"<
    57. }
    58. //析构函数
    59. ~Circle()
    60. {
    61. cout<<"Circle::析构函数"<
    62. }
    63. //拷贝构造函数
    64. Circle(const Circle &other):rad(other.rad)
    65. {
    66. cout<<"Cricle::拷贝构造函数"<
    67. }
    68. //拷贝赋值函数
    69. Circle & operator=(const Circle &other)
    70. {
    71. if(this!=&other)//确定不是自己给自己赋值
    72. {
    73. this->rad=other.rad;
    74. }
    75. cout<<"Circle::拷贝赋值函数"<
    76. return *this;
    77. }
    78. //计算周长函数
    79. void get_cir()
    80. {
    81. cir=2*PI*rad;
    82. cout<<"cir="<
    83. }
    84. //计算面积函数
    85. void get_area()
    86. {
    87. area=PI*rad*rad;
    88. cout<<"area="<
    89. }
    90. };
    91. class Rect:public Shape
    92. {
    93. private:
    94. double length;//长度
    95. double width;//宽度
    96. public:
    97. //无参构造
    98. Rect()
    99. {
    100. cout<<"Rect::无参构造"<
    101. }
    102. //有参构造
    103. Rect(double length,double width):length(length),width(width)
    104. {
    105. cout<<"Rect::有参构造"<
    106. }
    107. //析构函数
    108. ~Rect()
    109. {
    110. cout<<"Rect::析构函数"<
    111. }
    112. //拷贝构造函数
    113. Rect(const Rect &other):length(other.length),width(other.width)
    114. {
    115. cout<<"Rect::拷贝构造函数"<
    116. }
    117. //拷贝赋值函数
    118. Rect & operator=(const Rect &other)
    119. {
    120. if(this!=&other)//确定不是自己给自己赋值
    121. {
    122. this->length=other.length;
    123. this->width=other.width;
    124. }
    125. cout<<"Rect::拷贝赋值函数"<
    126. return *this;
    127. }
    128. //计算周长函数
    129. void get_cir()
    130. {
    131. cir=(length+width)*2;
    132. cout<<"cir="<
    133. }
    134. //计算面积函数
    135. void get_area()
    136. {
    137. area=length*width;
    138. cout<<"area="<
    139. }
    140. };
    141. int main()
    142. {
    143. //实例一个圆
    144. Circle c(3);
    145. c.get_cir();
    146. c.get_area();
    147. //实例拷贝构造函数
    148. Circle c1=c;
    149. c1.get_cir();
    150. c1.get_area();
    151. //实例一个矩形
    152. Rect r(3,4);
    153. r.get_cir();
    154. r.get_area();
    155. //实例拷贝赋值函数
    156. Rect r1;
    157. r1=r;
    158. r1.get_cir();
    159. r1.get_area();
    160. return 0;
    161. }

    结果:

    思维导图

  • 相关阅读:
    Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目
    kafka知识总结
    介绍 TensorFlow 的基本概念和使用场景
    懒人方案--半天搞定一个SpringBoot单体项目
    PAT 甲级 A1072 Gas Station
    计算BMI健康指数
    Java 基础入门
    面试八股文:C++ 多态 继承 重载 虚函数
    项目2-年收入判断
    deepvariant 基因变异识别算法docker版使用
  • 原文地址:https://blog.csdn.net/m0_59031281/article/details/132839845