• c++&qt day4


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

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

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

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

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

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

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

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

  • 相关阅读:
    【计算机网络】Tomcat和Servlet基础知识汇总
    敏捷思维&敏捷管理工具
    性能测试中故障排查及解决方法
    ctfshow-web5(md5弱比较)
    Keycloak之Gerrit安装与集成-yellowcong
    【Matlab】智能优化算法_麻雀搜索算法SSA
    java毕业设计创新创业竞赛管理系统2021Mybatis+系统+数据库+调试部署
    vue3-基础知识(0) - vue2和vue3基本比较
    【C++】:日期类实现
    Jmeter —— 自动录制脚本
  • 原文地址:https://blog.csdn.net/weixin_65188498/article/details/132839800