【C++】 46_继承中的构造与析构

发布时间:2019-06-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 46_继承中的构造与析构脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

思考:
如何初始化父类成员?
父类构造函数和子类构造函数有什么关系?

子类对象的构造

  • 子类对象可以定义构造函数
  • 子类构造函数

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

      • 直接通过初始化列表或者赋值的方式进行初始化
      • 调用父类构造函数进行初始化

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

    • 默认调用

      • 适用于无参构造函数和使用默认参数的构造函数
    • 显示调用

      • 通过初始化列表进行调用
      • 使用于所有父类构造函数

  • 父类构造函数的调用
class Child : public Parent
{
public:
    Child()                                         // 隐式调用
    {
        cout << "Child" << endl;
    }
    Child(string s) : Parent(Parameter to Parent)   // 显示调用
    {
        cout << "Parent" << s << endl;
    }
}

编程实验: 子类的构造初探

#include <iostream>

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;
}
输出:
Parent()
Child()
Parent(string s) : cc
Child(string s) : cc

子类对象中的构造

  • 构造规则

    • 子类对象在创建是会首先调用父类的构造函数
    • 先执行父类构造函数再执行子类的构造函数
    • 父类构造函数可以被隐式调用或者显示调用

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

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

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

编程实验: 子类构造深度解析

#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
{
private:
    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) : cc
Parent(string s) : cc
Object(string s) : cc1
Object(string s) : cc2
Child(string s) : cc

子类对象的析构

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

    • 执行自身的析构函数
    • 执行成员变量的析构函数
    • 执行父类的析构函数

编程实验: 对象的析构

#include <iostream>
#include <string>

using namespace std;

class Object
{
private: 
    string ms;
public:
    Object(string s)
    {
        cout << "Object(string s) : " << s << endl;
        ms = s;
    }
    ~Object()
    {
        cout << "~Object() : " << ms << endl;
    }
};

class Parent : public Object
{
private:
    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
{
private:
    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) : cc1
Object(string s) : cc2
Child(string s) : cc

~Child() : cc
~Object() : cc2
~Object() : cc1
~Parent() : cc
~Object() : cc

小结

  • 子类对象在创建时需要调用父类构造函数进行初始化
  • 先执行父类构造函数然后执行成员的构造函数
  • 父类构造函数显示调用需要在初始化列表中进行
  • 子类对象在销毁时需要调用父类析构函数进行处理

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 46_继承中的构造与析构全部内容,希望文章能够帮你解决【C++】 46_继承中的构造与析构所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。