• C++ Day5


    实现一个图形类(Shape),包含受保护成员属性:周长、面积,
    公共成员函数:特殊成员函数书写
    定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
    公共成员函数:特殊成员函数、以及获取周长、获取面积函数
    定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
    公共成员函数:特殊成员函数、以及获取周长、获取面积函数
    在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
    1. #include
    2. using namespace std;
    3. class Shape
    4. {
    5. protected:
    6. double c;
    7. double s;
    8. public:
    9. //构造函数
    10. Shape(){
    11. cout<<"无参构造"<
    12. }
    13. Shape (double c,double s):c(c),s(s)
    14. {
    15. cout<<"有参构造"<
    16. }
    17. //析构函数
    18. ~Shape(){cout<<"析构函数"<
    19. //拷贝构造函数
    20. Shape(const Shape &other)
    21. {
    22. c=other.c;
    23. s=other.s;
    24. cout<<"拷贝构造函数"<
    25. }
    26. //拷贝赋值函数
    27. Shape &operator=(const Shape &other)
    28. {
    29. c=other.c;
    30. s=other.s;
    31. cout<<"拷贝赋值函数"<
    32. return *this;
    33. }
    34. };
    35. class Circle:public Shape
    36. {
    37. private:
    38. double r;
    39. public:
    40. //构造函数
    41. Circle(){
    42. cout<<"圆的无参构造"<
    43. }
    44. Circle (double c, double s,double r):Shape(c,s),r(r)
    45. {
    46. cout<<"圆的有参构造"<
    47. }
    48. Circle(double r):r(r)
    49. {
    50. c=2*3.14*r;
    51. s=3.14*r*r;
    52. cout<<"圆的一个参数的有参构造"<
    53. }
    54. //析构函数
    55. ~Circle(){cout<<"圆的析构函数"<
    56. //拷贝构造函数
    57. Circle(const Circle &other)
    58. {
    59. r=other.r;
    60. c=other.c;
    61. s=other.s;
    62. cout<<"圆的拷贝构造函数"<
    63. }
    64. //拷贝赋值函数
    65. Circle &operator=(const Circle &other)
    66. {
    67. r=other.r;
    68. c=other.c;
    69. s=other.s;
    70. cout<<"圆的拷贝赋值函数"<
    71. return *this;
    72. }
    73. //获取周长
    74. double get_C()
    75. {
    76. cout<<"圆的周长="<
    77. return c;
    78. }
    79. //获取面积函数
    80. double get_S()
    81. {
    82. cout<<"圆的面积="<
    83. return s;
    84. }
    85. };
    86. class Rect:public Shape
    87. {
    88. private:
    89. double l;
    90. double w;
    91. public:
    92. //构造函数
    93. Rect(){
    94. cout<<"矩形的无参构造"<
    95. }
    96. Rect (double c, double s,double l,double w):Shape(c,s),l(l),w(w)
    97. {
    98. cout<<"矩形的有参构造"<
    99. }
    100. Rect(double l,double w):l(l),w(w)
    101. {
    102. c=2*(l+w);
    103. s=l*w;
    104. cout<<"矩形的长宽参数的有参构造"<
    105. }
    106. //析构函数
    107. ~Rect(){cout<<"矩形的析构函数"<
    108. //拷贝构造函数
    109. Rect(const Rect &other)
    110. {
    111. l=other.l;
    112. w=other.w;
    113. c=other.c;
    114. s=other.s;
    115. cout<<"矩形的拷贝构造函数"<
    116. }
    117. //拷贝赋值函数
    118. Rect &operator=(const Rect &other)
    119. {
    120. l=other.l;
    121. w=other.w;
    122. c=other.c;
    123. s=other.s;
    124. cout<<"矩形的拷贝赋值函数"<
    125. return *this;
    126. }
    127. //获取周长
    128. double get_C()
    129. {
    130. cout<<"矩形的周长="<
    131. return c;
    132. }
    133. //获取面积函数
    134. double get_S()
    135. {
    136. cout<<"矩形面积="<
    137. return s;
    138. }
    139. };
    140. int main()
    141. {
    142. Shape s1(12,9); //有参构造
    143. Shape s2=s1; //拷贝构造函数
    144. Shape s3; //无参构造
    145. s3=s1; //拷贝赋值函数
    146. Circle c1(6*3.14,9*3.14,3); //圆的有参构造
    147. Circle c2=c1; //圆的拷贝构造函数
    148. Circle c3; //圆的无参构造
    149. c3=c1; //圆的拷贝赋值函数
    150. c3.get_C(); //获取周长
    151. Circle c4(4); //圆的一个参数的有参构造
    152. c4.get_C(); //获取周长
    153. c4.get_S(); //获取面积
    154. Rect r1(3,4); //矩形的长宽两个参数的有参构造
    155. Rect r2=r1; //矩形的拷贝构造函数
    156. Rect r3; //矩形的无参构造
    157. r3=r1; //矩形的拷贝赋值函数
    158. r3.get_C(); //获取矩形的周长
    159. r3.get_S(); //获取矩形的面积
    160. return 0;
    161. }

    思维导图

  • 相关阅读:
    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