• 10.11 作业


    fun.h

    1. #ifndef __FUN_H__
    2. #define __FUN_H__
    3. #include
    4. using namespace std;
    5. // 定义一个Sofa类
    6. class Sofa
    7. {
    8. private:
    9. string sitting;
    10. public:
    11. Sofa(); // 无参构造函数
    12. Sofa(string s); // 有参构造函数
    13. ~Sofa(); // 析构函数
    14. Sofa(const Sofa &s); // 拷贝构造函数
    15. Sofa &operator=(const Sofa &s); // 拷贝赋值函数
    16. };
    17. // 定义一个Bed类
    18. class Bed
    19. {
    20. private:
    21. string sleeping;
    22. public:
    23. Bed(); // 无参构造函数
    24. Bed(string s); // 有参构造函数
    25. ~Bed(); // 析构函数
    26. Bed(const Bed &b); // 拷贝构造函数
    27. Bed &operator=(const Bed &b); // 拷贝赋值函数
    28. };
    29. // 定义一个Sofa_Bed类
    30. class Sofa_Bed : public Sofa, public Bed
    31. {
    32. private:
    33. string color;
    34. public:
    35. Sofa_Bed(); // 无参构造函数
    36. Sofa_Bed(string s, string ss, string c); // 有参构造函数
    37. ~Sofa_Bed(); // 析构函数
    38. Sofa_Bed(const Sofa_Bed &ss); // 拷贝构造函数
    39. Sofa_Bed &operator=(const Sofa_Bed &ss); // 拷贝赋值函数
    40. };
    41. #endif

    main.cpp 

    1. // 多继承实现沙发床
    2. #include "03_fun.h"
    3. int main()
    4. {
    5. Sofa_Bed s1; // 自动调用无参构造函数
    6. cout << "--------------------" << endl;
    7. Sofa_Bed s2("可坐", "可躺", "红色"); // 自动调用有参构造函数
    8. cout << "--------------------" << endl;
    9. Sofa_Bed s3(s2); // 自动调用拷贝构造函数
    10. cout << "--------------------" << endl;
    11. s1 = s2; // 自动调用拷贝赋值函数
    12. cout << "--------------------" << endl;
    13. return 0;
    14. // 自动调用析构函数
    15. }

    fun.cpp

    1. #include "03_fun.h"
    2. // Sofa::无参构造函数
    3. Sofa::Sofa()
    4. {
    5. cout << "Sofa::无参构造函数" << endl;
    6. }
    7. // Sofa::有参构造函数
    8. Sofa::Sofa(string s) : sitting(s)
    9. {
    10. cout << "Sofa::有参构造函数" << endl;
    11. }
    12. // Sofa::析构函数
    13. Sofa::~Sofa()
    14. {
    15. cout << "Sofa::析构函数" << endl;
    16. }
    17. // Sofa::拷贝构造函数
    18. Sofa::Sofa(const Sofa &s) : sitting(s.sitting)
    19. {
    20. cout << "Sofa::拷贝构造函数" << endl;
    21. }
    22. // Sofa::拷贝赋值函数
    23. Sofa &Sofa::operator=(const Sofa &s)
    24. {
    25. cout << "Sofa::拷贝赋值函数" << endl;
    26. sitting = s.sitting;
    27. return *this;
    28. }
    29. // Bed::无参构造函数
    30. Bed::Bed()
    31. {
    32. cout << "Bed::无参构造函数" << endl;
    33. }
    34. // Bed::有参构造函数
    35. Bed::Bed(string s) : sleeping(s)
    36. {
    37. cout << "Bed::有参构造函数" << endl;
    38. }
    39. // Bed::析构函数
    40. Bed::~Bed()
    41. {
    42. cout << "Bed::析构函数" << endl;
    43. }
    44. // Bed::拷贝构造函数
    45. Bed::Bed(const Bed &b) : sleeping(b.sleeping)
    46. {
    47. cout << "Bed::拷贝构造函数" << endl;
    48. }
    49. // Bed::拷贝赋值函数
    50. Bed &Bed::operator=(const Bed &b)
    51. {
    52. cout << "Bed::拷贝赋值函数" << endl;
    53. sleeping = b.sleeping;
    54. return *this;
    55. }
    56. // Sofa_Bed::无参构造函数
    57. Sofa_Bed::Sofa_Bed()
    58. {
    59. cout << "Sofa_Bed::无参构造函数" << endl;
    60. }
    61. // Sofa_Bed::有参构造函数
    62. Sofa_Bed::Sofa_Bed(string s, string ss, string c) : Sofa(s), Bed(ss), color(c)
    63. {
    64. cout << "Sofa_Bed::有参构造函数" << endl;
    65. }
    66. // Sofa_Bed::析构函数
    67. Sofa_Bed::~Sofa_Bed()
    68. {
    69. cout << "Sofa_Bed::析构函数" << endl;
    70. }
    71. // Sofa_Bed::拷贝构造函数
    72. Sofa_Bed::Sofa_Bed(const Sofa_Bed &ss) : Sofa(ss), Bed(ss), color(ss.color)
    73. {
    74. cout << "Sofa_Bed::拷贝构造函数" << endl;
    75. }
    76. // Sofa_Bed::拷贝赋值函数
    77. Sofa_Bed &Sofa_Bed::operator=(const Sofa_Bed &ss)
    78. {
    79. cout << "Sofa_Bed::拷贝赋值函数" << endl;
    80. Sofa::operator=(ss);
    81. Bed::operator=(ss);
    82. color = ss.color;
    83. return *this;
    84. }

    思维导图

  • 相关阅读:
    Nginx之 location 详解
    “花式提涨薪,结果被套路”,不懂怎么跟老板提加薪?这个方法真的很好用
    c语言练习87:合并两个有序数组
    贪心算法java实现
    Linux操作系统之进程间通信
    类加载流程
    JSR303和拦截器
    数据预处理之基于聚类的TOD异常值检测#matlab
    数据库三大范式
    基于PHP+MySQL蚕豆酱厂管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/qq_43499207/article/details/133777030