基本概念

有成员对象的类叫做封闭类

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class tyre
  4. {
  5. private:
  6. int radius, width;
  7. public:
  8. tyre(int, int);
  9. ~tyre(void);
  10. void PutTyre(void);
  11. };
  12. class engine
  13. {
  14. private:
  15. int power;
  16. public:
  17. engine(int);
  18. ~engine(void);
  19. void PutEngine(void);
  20. };
  21. class car
  22. {
  23. private:
  24. int price;
  25. tyre Ty;
  26. engine En;
  27. public:
  28. car(int, int, int, int);
  29. ~car(void);
  30. void PutCar(void);
  31. };
  32. tyre::tyre(int r, int w)
  33. {
  34. radius = r;
  35. width = w;
  36. }
  37. tyre::~tyre(void)
  38. {}
  39. void tyre::PutTyre(void)
  40. {
  41. cout << radius << endl;
  42. cout << width << endl;
  43. }
  44. engine::engine(int p)
  45. {
  46. power = p;
  47. }
  48. engine::~engine(void)
  49. {}
  50. void engine::PutEngine(void)
  51. {
  52. cout << power << endl;
  53. }
  54. car::car(int p, int Tr, int Tw, int Ep):price(p),Ty(Tr,Tw),En(Ep){}
  55. //构造函数的另一种写法:初始化列表
  56. car::~car(void)
  57. {}
  58. void car::PutCar(void)
  59. {
  60. cout << price << endl;
  61. Ty.PutTyre();
  62. En.PutEngine();
  63. }
  64. int main(void)
  65. {
  66. car C(20000, 25, 227, 5000);
  67. C.PutCar();//20000 25 227 5000
  68. return 0;
  69. }

7.PNG
备注:原网课的课件里engine类没有自定义构造函数,而我这里自己写了一个,就当“car.engine的初始化没问题”这句话不存在,因为在我这里如果不写构造函数这两个对象如何初始化编译器都不会知道
**

封闭类构造函数和析构函数的执行顺序

8.PNG

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class tyre
  4. {
  5. private:
  6. int radius, width;
  7. public:
  8. tyre(int, int);
  9. ~tyre(void);
  10. void PutTyre(void);
  11. };
  12. class engine
  13. {
  14. private:
  15. int power;
  16. public:
  17. engine(int);
  18. ~engine(void);
  19. void PutEngine(void);
  20. };
  21. class car
  22. {
  23. private:
  24. int price;
  25. tyre Ty;
  26. engine En;
  27. public:
  28. car(int, int, int, int);
  29. ~car(void);
  30. void PutCar(void);
  31. };
  32. tyre::tyre(int r, int w)
  33. {
  34. radius = r;
  35. width = w;
  36. cout << "Tyre Constructed" << endl;
  37. }
  38. tyre::~tyre(void)
  39. {
  40. cout << "Tyre Destructed" << endl;
  41. }
  42. void tyre::PutTyre(void)
  43. {
  44. cout << radius << endl;
  45. cout << width << endl;
  46. }
  47. engine::engine(int p)
  48. {
  49. power = p;
  50. cout << "Engine Constructed" << endl;
  51. }
  52. engine::~engine(void)
  53. {
  54. cout << "Engine Destructed" << endl;
  55. }
  56. void engine::PutEngine(void)
  57. {
  58. cout << power << endl;
  59. }
  60. car::car(int p, int Tr, int Tw, int Ep) : price(p), En(Ep), Ty(Tr, Tw)
  61. {
  62. cout << "Car Constructed" << endl;
  63. }//初始化列表顺序和成员对象声明顺序不一致,构造函数的调用根据成员对象声明顺序来
  64. car::~car(void)
  65. {
  66. cout << "Car Destructed" << endl;
  67. }
  68. void car::PutCar(void)
  69. {
  70. cout << price << endl;
  71. Ty.PutTyre();
  72. En.PutEngine();
  73. }
  74. int main(void)
  75. {
  76. car C(20000, 25, 227, 5000);
  77. C.PutCar();
  78. return 0;
  79. }
  80. /*
  81. Tyre Constructed
  82. Engine Constructed
  83. Car Constructed //构造函数由内而外调用
  84. 20000
  85. 25
  86. 227
  87. 5000
  88. Car Destructed
  89. Engine Destructed
  90. Tyre Destructed //析构函数由外而内调用
  91. */

封闭类的复制构造函数

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. A(void) { cout << "Default" << endl; }
  7. A(const A &a) { cout << "Copy" << endl; }
  8. };
  9. class B
  10. {
  11. public:
  12. A a;
  13. };
  14. int main(void)
  15. {
  16. B b1;//Default
  17. B b2(b1);//Copy
  18. return 0;
  19. }
  • 特殊:调用类B的复制构造函数(编译器自带的)初始化b2时不是简单地把b1拷贝过来,而是会调用A类中的复制构造函数初始化b2的对象a,调用函数时的实参就是b1的对象a
  • 一般:调用封闭类的复制构造函数进行初始化时,封闭类中的成员对象的复制构造函数也会被调用来完成封闭类的复制构造