• C++day5


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

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

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

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

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

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

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

    1. #include
    2. #define PI 3.14
    3. using namespace std;
    4. class Shape
    5. {
    6. protected:
    7. double cir;//周长
    8. double area;//面积
    9. public:
    10. Shape()//显性定义无参构造
    11. {
    12. cir = 0;
    13. area = 0;
    14. cout<<"无参构造函数"<
    15. }
    16. Shape(double cir, double area):cir(cir),area(area)
    17. {
    18. cout<<"有参构造函数"<
    19. }
    20. ~Shape()
    21. {
    22. cout<<"Shape::析构函数"<
    23. }
    24. Shape(const Shape &other):cir(other.cir),area(other.area)
    25. {
    26. cout<<"拷贝构造函数"<
    27. }
    28. Shape & operator=(const Shape &other)
    29. {
    30. this->cir = other.cir;
    31. this->area = other.area;
    32. cout<<"拷贝赋值函数"<
    33. return *this;
    34. }
    35. };
    36. class Circle:public Shape
    37. {
    38. private:
    39. double rad;
    40. public:
    41. Circle()//显性定义无参构造
    42. {
    43. rad = 0;
    44. cout<<"无参构造函数"<
    45. }
    46. Circle(double rad):rad(rad)
    47. {
    48. cout<<"有参构造函数"<
    49. }
    50. ~Circle()
    51. {
    52. cout<<"Circle::析构函数"<
    53. }
    54. Circle(const Circle &other):rad(other.rad)
    55. {
    56. cout<<"拷贝构造函数"<
    57. }
    58. Circle & operator=(const Circle &other)
    59. {
    60. if(this != &other)
    61. {
    62. this->rad = other.rad;
    63. }
    64. cout<<"拷贝赋值函数"<
    65. return *this;
    66. }
    67. double get_cir()//获取周长函数
    68. {
    69. this->cir = 2*PI*rad;
    70. return this->cir;
    71. }
    72. double get_area()
    73. {
    74. this->area = PI*rad*rad;
    75. return this->area;
    76. }
    77. };
    78. class Rect:public Shape
    79. {
    80. private:
    81. double len;
    82. double width;
    83. public:
    84. Rect()//显性定义无参构造
    85. {
    86. len = 0;
    87. width = 0;
    88. cout<<"无参构造函数"<
    89. }
    90. Rect(double len, double width):len(len),width(width)
    91. {
    92. cout<<"有参构造函数"<
    93. }
    94. ~Rect()
    95. {
    96. cout<<"Rect::析构函数"<
    97. }
    98. Rect(const Rect &other):len(other.len),width(other.width)
    99. {
    100. cout<<"拷贝构造函数"<
    101. }
    102. Rect & operator=(const Rect &other)
    103. {
    104. if(this != &other)
    105. {
    106. this->len = other.len;
    107. this->width = other.width;
    108. }
    109. cout<<"拷贝赋值函数"<
    110. return *this;
    111. }
    112. double get_cir()//获取周长函数
    113. {
    114. this->cir = 2*(len+width);
    115. return this->cir;
    116. }
    117. double get_area()
    118. {
    119. this->area = len*width;
    120. return this->area;
    121. }
    122. };
    123. int main()
    124. {
    125. Circle c1(1.1);
    126. cout<<"c1周长="<get_cir()<
    127. cout<<"c1面积="<get_area()<
    128. Circle c2;
    129. c2 = c1;
    130. cout<<"c2周长="<get_cir()<
    131. cout<<"c2面积="<get_area()<
    132. Rect r1(2,3);
    133. cout<<"r1周长="<get_cir()<
    134. cout<<"r1面积="<get_area()<
    135. Rect r2 = r1;
    136. cout<<"r2周长="<get_cir()<
    137. cout<<"r2面积="<get_area()<
    138. return 0;
    139. }

     

  • 相关阅读:
    Mybatis---从入门到深化
    Kubernetes部署kubernates Nginx Ingress Controller
    Bee1.17同时支持JDBC,安卓和鸿蒙;SQL Server分页,JPA支持(同步Maven)
    【机器学习】近邻类模型:KNN算法在数据科学中的实践与探索
    宠物用品做谷歌推广的三种形式
    【uniapp】小程序自定义一个通用的返回按钮组件
    罗翔:钱没了可以再挣,工作没了可以再找,朋友没了可以再交,爱情没了可以再去找。你生来就一无所有,何惧从头再来。...
    【EasyExcel】Java将不同的.csv文件数据存入同一个.xlsx文件的不同sheet当中
    知识付费免费开源项目-课程讲师教务真题
    JVM参数配置 JDK1.8
  • 原文地址:https://blog.csdn.net/qq_53478460/article/details/132839875