1.如何初始化父类成员?父类构造函数与子类构造函数有什么关系?

2.子类对象的构造

—-子类中可以定义构造函数
—-子类构造函数
必须对继承而来的成员进行初始化
直接通过初始化列表或者赋值的方式进行初始化
调用父类的构造函数进行初始化

父类构造函数在子类中的调用方式
—-默认调用
适用于无参构造函数和使用默认参数的构造函数,找不到 就直接报错
—-显示调用
通过初始化列表调用
适用于所有父类构造函数

父类构造函数的调用

  1. class Child : public Parent
  2. {
  3. public:
  4. Child() //隐式调用
  5. {
  6. cout << "Child()" << endl;
  7. }
  8. Child(string s)
  9. : Parent("Parameter to Parent") //显示调用
  10. {
  11. cout << "Child() : " << s << endl;
  12. }
  13. };
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. class Parent
  6. {
  7. public:
  8. Parent()
  9. {
  10. cout << "Parent() "<< endl;
  11. }
  12. Parent(string s)
  13. {
  14. cout << "Parent(string s) : " << s << endl;
  15. }
  16. };
  17. class Child : public Parent
  18. {
  19. public:
  20. Child()
  21. {
  22. cout << "Child()" << endl;
  23. }
  24. Child(string s)
  25. : Parent(s) //显示调用,如果没有,则会隐式调用
  26. {
  27. cout << "Child(string s)" << endl;
  28. }
  29. };
  30. int main()
  31. {
  32. Child c;
  33. Child cc("cc");
  34. return 0;
  35. }
  36. delphi@delphi-vm:~$ ./a.out
  37. Parent()
  38. Child()
  39. Parent()
  40. Child(string s)
  41. delphi@delphi-vm:~$ g++ test.cpp
  42. test.cpp: In constructor ‘Child::Child(std::string)’:
  43. test.cpp:29: error: expected primary-expression before ‘s’
  44. delphi@delphi-vm:~$ g++ test.cpp
  45. delphi@delphi-vm:~$ ./a.out
  46. Parent()
  47. Child()
  48. Parent(string s) : cc
  49. Child(string s)

构造规则
—-子类对象在创建时会首先调用父类的构造函数
—-先执行父类构造函数再执行子类的构造函数
—-父类构造函数可以被隐式调用(无参或使用了默认参数值的构造函数)或者显示调用(通过初始化列表调用任何构造函数)

3.对象创建时构造函数的调用顺序

1.调用父类的构造函数
2.调用成员变量的构造函数
3.调用类自身的构造函数

口诀:先父母,后客人,再自己

例程:子类构造深度剖析

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. class Object
  6. {
  7. public:
  8. Object(string s)
  9. {
  10. cout << "Object(string s) : " << s << endl;
  11. }
  12. };
  13. class Parent : public Object
  14. {
  15. public:
  16. Parent() : Object("Default")
  17. {
  18. cout << "Parent() "<< endl;
  19. }
  20. Parent(string s) : Object(s)
  21. {
  22. cout << "Parent(string s) : " << s << endl;
  23. }
  24. };
  25. class Child : public Parent
  26. {
  27. Object mO1;
  28. Object mO2; //child类有两种关系,组合和继承,先父母没问题,但是初始化列表要指定
  29. public:
  30. Child() : mO1("Default 1"), mO2("Default 2")
  31. {
  32. cout << "Child()" << endl;
  33. }
  34. Child(string s) : Parent(s),mO1(s + " 1"),mO2(s + " 2") //显示调用,如果没有,则会隐式调用
  35. {
  36. cout << "Child(string s) : " << s << endl;
  37. }
  38. };
  39. int main()
  40. {
  41. Child cc("cc");
  42. //Object(string s) : cc
  43. //Parent(string s) : cc
  44. //Object(string s) : cc1
  45. //Object(string s) : cc2
  46. //Child(string s) : cc
  47. return 0;
  48. }
  49. delphi@delphi-vm:~$ ./a.out
  50. Object(string s) : cc
  51. Parent(string s) : cc
  52. Object(string s) : cc 1
  53. Object(string s) : cc 2
  54. Child(string s) : cc

4.子类对象的析构

析构函数的调用顺序与构造函数相反
1.执行自身的析构函数
2.执行成员变量的析构函数
3.执行父类的析构函数

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. class Object
  6. {
  7. string ms;
  8. public:
  9. Object(string s)
  10. {
  11. cout << "Object(string s) : " << s << endl;
  12. ms = s;
  13. }
  14. ~Object()
  15. {
  16. cout << "~Object() : " << ms << endl;
  17. }
  18. };
  19. class Parent : public Object
  20. {
  21. string ms;
  22. public:
  23. Parent() : Object("Default")
  24. {
  25. cout << "Parent() "<< endl;
  26. ms = "Default";
  27. }
  28. Parent(string s) : Object(s)
  29. {
  30. cout << "Parent(string s) : " << s << endl;
  31. ms = s;
  32. }
  33. ~Parent()
  34. {
  35. cout << "~Parent() : " << ms << endl;
  36. }
  37. };
  38. class Child : public Parent
  39. {
  40. Object mO1;
  41. Object mO2; //child类有两种关系,组合和继承,先父母没问题,但是初始化列表要指定
  42. string ms;
  43. public:
  44. Child() : mO1("Default 1"), mO2("Default 2")
  45. {
  46. cout << "Child()" << endl;
  47. ms = "Default";
  48. }
  49. Child(string s) : Parent(s),mO1(s + " 1"),mO2(s + " 2") //显示调用,如果没有,则会隐式调用
  50. {
  51. cout << "Child(string s) : " << s << endl;
  52. ms = s;
  53. }
  54. ~Child()
  55. {
  56. cout << "~Child() : " << ms << endl;
  57. }
  58. };
  59. int main()
  60. {
  61. Child cc("cc");
  62. cout << endl;
  63. return 0;
  64. }
  65. delphi@delphi-vm:~$ ./a.out
  66. Object(string s) : cc
  67. Parent(string s) : cc
  68. Object(string s) : cc 1
  69. Object(string s) : cc 2
  70. Child(string s) : cc
  71. ~Child() : cc
  72. ~Object() : cc 2
  73. ~Object() : cc 1
  74. ~Parent() : cc
  75. ~Object() : cc

小结:

1.子类对象在创建时需要调用父类构造函数进行初始化
2.先执行父类构造函数然后执行成员的构造函数
3.父类构造函数显示调用需要在初始化列表中进行
4.子类对象在销毁时需要调用父类析构函数进行清理
5.析构顺序与构造顺序对称相反