• c++day5


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

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

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

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

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

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

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

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

  • 相关阅读:
    苹果跌落全球市值第一宝座;联想一员工侵占公司工时费近1000万;PHP 8.1.6 发布|极客头条
    【嵌入式——QT】线程同步
    给定n个结点m条边的简单无向图,判断该图是否存在鱼形状的子图:有一个环,其中有一个结点有另外两条边,连向不在环内的两个结点。若有,输出子图的连边
    Ansible任务控制loop循环、when和block条件判断介绍演示
    使用Puppeteer构建博客内容的自动标签生成器
    数据结构中的判定转状态+扫描线:P1502
    python读取execel表格(xls等格式)转换为csv,并且加载到hive表中
    字节一面:说说HTTP 常见的状态码有哪些,适用场景?
    【SwitchyOmega】SwitchyOmega 安装及使用
    CSS 实现跳动的方块动画
  • 原文地址:https://blog.csdn.net/wdc857/article/details/132839548