46 - 继承中的构造与析构

本文探讨了子类对象构造过程中的父类构造函数调用,包括默认调用和显式调用的方式,并通过实例展示了构造函数的执行顺序。同时,介绍了析构函数的调用顺序,强调其与构造函数的对称性。总结了在创建和销毁子类对象时,构造和析构的规则及流程。
摘要由CSDN通过智能技术生成

---- 整理自狄泰软件唐佐林老师课程

1. 思考

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

1.1 子类对象的构造

  • 子类中可以定义构造函数

1.1.1 子类构造函数

  • 必须对继承而来的成员进行初始化
    • 直接通过初始化列表或者赋值的方式进行初始化
    • 调用父类构造函数进行初始化

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

  • 默认调用:
    • 适用于无参构造函数和使用默认参数的构造函数
  • 显式调用:
    • 通过初始化列表进行调用
    • 适用于所有父类构造函数

1.1.3 父类构造函数的调用

在这里插入图片描述

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

#include <iostream>
#include <string>

using namespace std;

class Parent
{
public:

};

class Child : public Parent
{
public:
	Child() {
		cout << "Child()" << endl;
	}
	Child(string s) {
		cout << "Child(string s): " << s << endl; 
	}
};

int main()
{
	Child c; // 调用父类构造函数
			 // 当类中没有定义构造函数时,编译器默认提供一个无参构造函数
			 // 这里的没有表示没有构造函数,也没有拷贝构造函数
	
    return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

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;
}

在这里插入图片描述

#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) { // 隐式调用父类构造函数,默认调用无参构造函数
		cout << "Child(string s): " << s << endl; 
	}
};

int main()
{
	Child c;
	Child cc("cc");
	
    return 0;
}

在这里插入图片描述

#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("Parameter to Parent") { // 显示调用父类构造函数
		cout << "Child(string s): " << s << endl; 
	}
};

int main()
{
	Child c;
	Child cc("cc");
	
    return 0;
}

在这里插入图片描述

#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.1.5 构造规则

  • 子类对象在创建时会 首先调用父类的构造函数
  • 执行父类构造函数 执行子类的构造函数
  • 父类构造函数可以 被隐式调用(默认无参构造函数)或者显式调用

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

  • 先父母,后客人,再自己
  • 1、调用父类的构造函数
  • 2、调用成员变量的构造函数
  • 3、调用类自身的构造函数

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

#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("Default1"), mO2("Default2") {
		cout << "Child()" << endl;
	}
	Child(string s) : Parent(s), mO1(s + "1"), mO2(s + "2") {
		cout << "Child(string s): " << s << endl; 
	}
};

int main()
{
	Child c;
	
	cout << endl;
	
	Child cc("cc");
	
    return 0;
}

在这里插入图片描述

1.2 子类对象的析构

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

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

1.2.2 编程实验:对象的析构

#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 c;
    cout << endl;
	Child cc("cc");
    
    return 0;
}

在这里插入图片描述

1.3 小结

  • 子类对象在创建时需要调用父类构造函数进行初始化
  • 先执行父类构造函数然后执行成员的构造函数
  • 父类构造函数 显示调用 需要在 初始化列表 中进行
  • 子类对象在销毁时需要调用父类析构函数进行清理
  • 析构顺序与构造顺序 对称相反
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uuxiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值