【C++基础入门】40.C++中继承的构造与析构

一、思考

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

二、子类对象的构造

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

         下面来初探一下子类的构造:

#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; //error, no Parent constructor to call
    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;
    return 0;
}

        这样就可以正常编译运行了

         如果加上 Child cc("cc"); 这行代码,会打印出什么呢?

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

        输出结果如下: 

这些示例说明了默认调用适用于无参构造函数使用默认参数的构造函数

         下面尝试一下显示地调用父类中带一个参数的构造函数,就是把 Child(string s) 改成  Child(string s) : Parent(s)

#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. 调用父类的构造函数
  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
{
	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 //后客人,m01先定义
	//Object(string s) : cc 2 //后客人
	//child(string s) : cc  //再自己
	
	return 0;
}

        输出结果如下,跟上面分析的一样: 

三、子类对象的析构

  • 析构函数的调用顺序与构造函数相反
  1. 执行自身的析构函数
  2. 执行成员变量的析构函数
  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;
}

        输出结果如下:

 

四、小结

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清风自在 流水潺潺

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

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

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

打赏作者

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

抵扣说明:

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

余额充值