• 9月12日作业


    作业代码

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

    运行截图

    思维导图

    模拟面试

  • 相关阅读:
    Java之接口
    矩阵运算_矩阵的协方差矩阵/两个矩阵的协方差矩阵_求解详细步骤示例
    初始化一个Vue3项目
    C语言——1.入门须知
    从理论到实践:如何用 TDengine 打造完美数据模型​
    【华为IP阶段OSPF3】--- 四类特殊区域及OSPF协议特性
    K8S的安装kubernetes-dashboard服务起来了,访问不到解决
    模板语法2
    Win系统VMware虚拟机安装配置(一)(附激活码安装包)
    haproxy实验
  • 原文地址:https://blog.csdn.net/mcslll/article/details/132839812