头图

如何初始化父类成员?

父类构造函数和子类构造函数有什么关系?

子类对象的构造

子类可以定义构造函数

子类构造函数

必须对继承而来的成员进行初始化

直接通过初始化列表或者赋值的方式进行初始化

调用父类构造函数进行初始化

父类构造函数子类中的调用方式

默认调用

适用于无参构造函数和使用默认参数的构造函数

显示调用

通过初始化列表进行调用
适用于父类构造函数
46-1子类的构造

#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):" <<endl;
        
    }
};
int main()
{
    child c;      
    child cc("cc");
    return 0;
}

输出结果:

Parent
child()
Parent(string s):cc
child()(string s):

子类对象的构造

构造规则

子类对象在创建时会首先调用 父类 的构造函数

先执行父类构造函数再执行 子类 构造函数

父类构造函数可以被 隐式调用 或者 显示调用

Slide8.PNG
子类构造深度解析
46-2.cpp

#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 m01;
    Object m02;
    public:
    child():m01("default 1"),m02("default 2")
    {
        cout<< "child()" <<endl;
        
    }
    child(string s):Parent(s),m01(s+"3"),m02(s+"4")
    {
        cout<< "child()(string s):" <<s<<endl;
        
    }
};
int main()
{
   // child c;      
    child cc("cc");
    return 0;
}

输出:

Object(string s):cc
Parent(string s):cc
Object(string s):cc3
Object(string s):cc4
child()(string s):cc

子类对象的析构

析构函数的调用顺序与构造函数相反

1.执行自身分析构函数

2.执行成员变量的析构函数

3.执行父类的析构函数

例子:36-3构造与析构

#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 m01;
    Object m02;
    string ms;
    
    public:
    child():m01("default 1"),m02("default 2")
    {
        cout<< "child()" <<endl;
        ms = "Default";
        
    }
    child(string s):Parent(s),m01(s+"3"),m02(s+"4")
    {
        cout<< "child()(string s):" <<s<<endl;
        ms = s;
        
    }
    ~child()
    {
     cout<< "~child():" <<ms<<endl;
    }
};
int main()
{
   // child c;      
    child cc("cc");
    cout <<endl;
    return 0;
}

输出:

Object(string s):cc
Parent(string s):cc
Object(string s):cc3
Object(string s):cc4
child()(string s):cc

~child():cc
~Object():cc4
~Object():cc3
~Parent():cc
~Object():cc

小结:

子类对象在创建时需要调用父类构造函数进行初始化

先执行父类构造函数然后自行成员的构造函数

父类构造函数显示调用需要再初始化列表中进行

子类对象在销毁时需要调用父类析构函数进行清理

析构顺序与构造顺序对称相反


YingLi
6 声望4 粉丝

From zero to hero.