• Day 5 C++


    1. #include
    2. //多继承
    3. using namespace std;
    4. //封装沙发 类
    5. class Sofa
    6. {
    7. private:
    8. string sitting;
    9. public:
    10. Sofa(){cout<< "父类sofa无参构造函数" << endl;}
    11. Sofa(string s):sitting(s)
    12. {
    13. cout << "父类sofa有参构造函数" << endl;
    14. }
    15. //拷贝构造函数
    16. Sofa(const Sofa &other):sitting(other.sitting)
    17. {
    18. cout << "父类Sofa拷贝构造函数" << endl;
    19. }
    20. //拷贝赋值函数
    21. Sofa &operator=(const Sofa &other)
    22. {
    23. cout << "父类Sofa拷贝赋值函数" << endl;
    24. sitting=other.sitting;
    25. return *this;
    26. }
    27. ~Sofa()
    28. {
    29. cout << "父类sofa析构函数" << endl;
    30. }
    31. void show()
    32. {
    33. cout << sitting<< endl ;
    34. }
    35. };
    36. //封装 桌子 类
    37. class Table
    38. {
    39. private:
    40. string material;
    41. int quaty;
    42. public:
    43. Table(){cout << "父类Table无参构造函数" << endl;}
    44. Table(string material,int quaty):material(material),quaty(quaty)
    45. {
    46. cout << "父类Table有参构造函数" << endl;
    47. }
    48. //拷贝构造函数
    49. Table(const Table &other):material(other.material),quaty(other.quaty)
    50. {
    51. cout << "父类Table拷贝构造函数" << endl;
    52. }
    53. //拷贝赋值函数
    54. Table &operator=(const Table &other)
    55. {
    56. cout << "父类Table拷贝赋值函数" << endl;
    57. material=other.material;
    58. quaty=other.quaty;
    59. return *this;
    60. }
    61. ~Table()
    62. {
    63. cout << "父类Table析构函数" << endl;
    64. }
    65. void show()
    66. {
    67. cout << material <
    68. cout << quaty << endl;
    69. }
    70. };
    71. //封装 家具 沙发 桌子
    72. class Furni :public Sofa,public Table
    73. {
    74. private :
    75. int num ;
    76. string color;
    77. public:
    78. Furni(){cout << "子类无参构造函数" << endl;}
    79. Furni(int n,string c,string sit,string m,int q):Sofa(sit),Table(m,q),num(n),color(c)
    80. {
    81. cout << "子类有参构造函数" << endl;
    82. }
    83. //子类拷贝构造函数
    84. Furni(const Furni &other):num(other.num),color(other.color),Sofa(other),Table(other)
    85. {
    86. cout << "子类拷贝函数" << endl;
    87. }
    88. //子类拷贝赋值函数
    89. Furni &operator=(const Furni &other)
    90. {
    91. cout << "子类拷贝赋值函数" << endl;
    92. num=other.num;
    93. color=other.color;
    94. Sofa::operator=(other);
    95. Table::operator=(other);
    96. return *this;
    97. }
    98. ~Furni()
    99. {
    100. cout << "子类析构函数" << endl;
    101. }
    102. void show()
    103. {
    104. cout << num <
    105. cout << color << endl;
    106. }
    107. };
    108. int main()
    109. {
    110. Furni s(10,"white","可坐","woolen",20);
    111. Furni s1;
    112. Furni s2(s);
    113. s1=s;
    114. s.Sofa::show();
    115. s.Table::show();
    116. s.show();
    117. return 0;
    118. }

  • 相关阅读:
    PTA 7-72 成绩分析表
    详解react生命周期和在父子组件中的执行顺序
    马斯克发布人形机器人进展,它是否“中看不中用”?
    MYSQL常用函数详解
    天翎BPM流程引擎助力打造流程服务中台
    STM32 HAL库 串口使用问题记录
    论文阅读——RetNet
    【Java EE】JUC(java.util.concurrent) 的常见类
    网工内推 | 国企、上市公司,IA/IP认证即可,有年终、绩效
    JAVA_多态(面向对象进阶)学习笔记
  • 原文地址:https://blog.csdn.net/weixin_42019010/article/details/133777161