问题:
如何初始化父类成员?
父类构造函数和子类构造函数的有什么关系?
- 子类中可以定义构造函数
- 子类构造函数必须对继承而来的成员进行初始化
- 直接通过初始化列表或者赋值的方式进行初始化
- 调用父类的构造函数进行初始化
父类构造函数在子类中的调用方式
- 默认调用
- 适用于无参构造函数和使用默认参数的构造函数
- 显示调用
- 通过初始化列表进行调用
- 适用于所有父类的构造函数
父类构造函数的调用
class Child : public Parent
{
public: Child() /*隐式调用*/{cout << "Child()" << endl;}Child(string s) /*显示调用*/ : Parent("Parameter to Parent"){cout << "Child()" << s << endl;}
};
示例:
class Parent
{
public:Parent(string s){cout << "Parent(string s) " << s << endl;}
};class Child : public Parent
{
public:Child(){cout << "Child()" << endl;}Child(string s){cout << "Child(string s) " << s << endl;}
};int main()
{Child c;return 0;
}
编译结果
Test.cpp:250:2: error: no matching function for call to ‘Parent::Parent()’
由于父类没有提供 默认的构造函数,所有会报错。
修改:
class Parent
{
public:Parent(){cout << "Parent() " << endl;}Parent(string s){cout << "Parent(string s) " << s << endl;}
};class Child : public Parent
{
public:Child(){cout << "Child()" << endl;}Child(string s) : Parent(s)//Child(string s) : Parent(){cout << "Child(string s) " << s << endl;}
};int main()
{Child c("hello");return 0;
}
输出:
// Child(string s) : Parent(s) 输出结果
Parent(string s) hello
Child(string s) hello//Child(string s) : Parent() 输出结果
Parent()
Child(string s) hello
构造规则
- 子类对象在创建时会首先调用父类的构造函数
- 先执行父类构造函数在执行子类的构造函数
- 父类的构造函数可以隐式调用或者显示调用
子类不显示的调用父类,则调用的是父类的默认的构造函数。
对象创建是构造函数的调用顺序
- 创建父类的构造函数
- 调用成员变量的构造函数
- 调用类自身的构造函数
口诀:
先父母,后客人,最后自己
实验:
class Object
{
public:Object(string s){cout << "Object(string s) : " << s << endl;}
};class Parent : public Object
{
public:Parent() : Object("Default"){cout << "Parent()" << endl;}Parent(string s) : Object(s){cout << "Parent(string s) : " << s << endl;}
};class Child : public Parent
{// 成员内部(客人)Object mO1;Object mO2;
public:Child() : mO1("Default 1"), mO2("Default 2"){cout << "Child()" << endl;}Child(string s) : Parent(s), mO2(s + " 2"), mO1(s + " 1"){cout << "Child(string s) : " << s << endl;}
};int main()
{ Child cc("cc");return 0;
}
输出:
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
析构顺序和上述顺序相反
class Object
{string ms;
public:Object(string s){cout << "Object(string s) : " << s << endl;ms = s;}~Object(){cout << "~Object() : " << ms << endl;}
};class Parent : public Object
{string ms;
public:Parent() : Object("Default"){cout << "Parent()" << endl;ms = "Default";}Parent(string s) : Object(s){cout << "Parent(string s) : " << s << endl;ms = s;}~Parent(){cout << "~Parent() : " << ms << endl;}
};class Child : public Parent
{Object mO1;Object mO2;string ms;
public:Child() : mO1("Default 1"), mO2("Default 2"){cout << "Child()" << endl;ms = "Default";}Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2"){cout << "Child(string s) : " << s << endl;ms = s;}~Child(){cout << "~Child() " << ms << endl;}
};int main()
{ Child cc("cc");cout << endl;return 0;
}
输出
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc~Child() cc
~Object() : cc 2
~Object() : cc 1
~Parent() : cc
~Object() : cc
小结
- 子类对象在创建时需要调用父类构造函数进行初始化
- 先执行父类构造函数后执行成员的构造函数
- 父类构造函数显示调用需要在初始化列表中进行
- 子类对象在销毁时需要调用父类中的析构函数进行清理
- 析构顺序与构造顺序对称相反