C++笔记 第四十六课 继承中的构造与析构---狄泰学院

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42187898/article/details/84573942

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第四十六课 继承中的构造与析构

1.思考

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

2.子类对象的构造

子类中可以定义构造函数
子类构造函数
必须对继承而来的成员进行初始化
直接通过初始化列表或者赋值的方式进行初始
调用父类构造函数进行初始化
父类构造函数在子类中的调用方式
默认调用
适用于无参构造函数和使用默认参数的构造函数
显示调用
通过初始化列表进行调用
适用于所有父类析构函数
父类析构函数的调用
在这里插入图片描述

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) :" << s << endl;
    }
};
int main()
{       
    Child c; //error, no Parent constructor to call
    Child cc("cc"); 
// c output:
// "Parent()"
// Child()
// cc output:
// Parent(string s) : cc
//Child(string s): cc
    
    return 0;
}
运行结果
Parent()
Child()
Parent(string s) : cc
Child(string s) : cc

构造规则
子类对象在创建时会首先调用父类的构造函数
先执行父类构造函数再执行子类的构造函数
父类构造函数可以被隐式调用或者显式调用
对象创建时构造函数的调用顺序
1.调用父类的构造函数
2.调用成员变量的构造函数
3.调用类自身的构造函数
口诀:先父母,后客人,再自己

46-2 子类构造深度解析(**)

#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 + "1"),m02(s + "2")
    {
        cout << "Child(string s) : " << s << endl;
    }
};
int main()
{       
    Child cc("cc");
// cc output:
// Object(string s ) : cc
// Parent(string s) : cc
// Object(string s) : cc 1
// Object(string s) : cc 2
// Child(string s) : cc
    return 0;
}
运行结果
Object(string s ) : cc
Parent(string s) : cc
Object(string s ) : cc1
Object(string s ) : cc2
Child(string s) : cc

3.子类对象的析构

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

46-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 + "1"),m02(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

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

猜你喜欢

转载自blog.csdn.net/weixin_42187898/article/details/84573942