⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_3_multiple_inheritance_constructor_destrctor_2.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//*********************************************************
// 派生类的构造函数和析构函数的举例(多继承、含有内嵌对象)
//*********************************************************

#include <iostream.h>

class B1 //基类B1定义
{
public:
	B1(int i) {cout<<"constructing B1 "<<i<<endl;} //B1的构造函数
	~B1() {cout<<"destructing B1 "<<endl;}         //B1的析构函数
};

class B2 //基类B2定义
{
public:
	B2(int j) {cout<<"constructing B2 "<<j<<endl;} //B2的构造函数
	~B2() {cout<<"destructing B2 "<<endl;}         //B2的析构函数
};

class B3 //基类B3定义
{
public:
	B3(){cout<<"constructing B3 *"<<endl;} //B3的构造函数
	~B3() {cout<<"destructing B3 "<<endl;} //B3的析构函数
};

class C:public B2,public B1,public B3  //派生类C定义
{
public:
	C(int a, int b, int c, int d):           //派生类构造函数定义
	  B1(a),memberB2(d),memberB1(c),B2(b){}	
private:
	B1 memberB1;
	B2 memberB2;
	B3 memberB3;
};

void main()
{
	C obj(1,2,3,4); //创建派生类对象
}

/*
constructing B2 2
constructing B1 1
constructing B3 *
constructing B1 3
constructing B2 4
constructing B3 *
destructing B3
destructing B2
destructing B1
destructing B3
destructing B1
destructing B2
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -