46-继承中的构造与析构

来源:互联网 发布:腾飞投资理财源码 编辑:程序博客网 时间:2024/06/05 04:34

1、思考

这里写图片描述

2、子类对象的构造

这里写图片描述
子类对象继承父类,对父类成员进行初始化的方式,直接初始化成员变量,或者调用父类构造函数进行初始化

3、

这里写图片描述

4、

这里写图片描述

#include <iostream>#include <string>using namespace std;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)    {        cout << "Child(string s) : " << s << endl;    }};int main(){           Child c;     Child cc("cc");    return 0;}1、子类的构造函数显示调用父类的构造函数 Child(string s) : Parent(s)结果:Parent()Child()Parent(string s) : ccChild(string s) : cc2、子类的构造函数没有使用初始化列表来进行初始化,会默认调用父类的无参构造函数进行初始化,结果Parent()Child()Parent()Child(string s) : cc3、当父类没有提供无参构造函数时,子类也没有显示调用父类构造函数,子类初始化的时候,会调用父类默认无参构造函数,发现没有找到,则编译器报错

5、

这里写图片描述

6、

这里写图片描述

#include <iostream>#include <string>using namespace std;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), mO1(s + " 1"), mO2(s + " 2")    {        cout << "Child(string s) : " << s << endl;    }};int main(){           Child cc("cc");    //先父母(父类,父类如果还有父类,就继续向上找),再客人(按照成员对象先后创建的顺序进行),后自己    return 0;}Object(string s) : ccParent(string s) : ccObject(string s) : cc 1Object(string s) : cc 2Child(string s) : cc

7、子类对象的析构

这里写图片描述

#include <iostream>#include <string>using namespace std;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) : ccParent(string s) : ccObject(string s) : cc 1Object(string s) : cc 2Child(string s) : cc~Child() cc~Object() : cc 2~Object() : cc 1~Parent() : cc~Object() : cc

8、

这里写图片描述

原创粉丝点击