#include <iostream>
using namespace std;
class CFatherSum //父类Sum
{
public:
CFatherSum(int iRec){cout<<"1,Constructing CFatherSum"<<iRec<<endl;}//构造函数成员初始化
};
class CFatherBru //父类Bru
{
public:
CFatherBru(int iRec){cout<<"2,Constructing CFatherBru"<<iRec<<endl;}//构造函数成员初始化
};
class CFatherObam //父类Obam
{
public:
//第一行用于对比默认构造
//CFatherObam(int iRec){cout<<"3,Constructing CFatherObam"<<iRec<<endl;}//构造函数成员初始化
CFatherObam(){cout<<"3,Constructing CFatherObam*"<<endl;}//构造函数成员初始化
};
class CSon:public CFatherSum,public CFatherBru,public CFatherObam //继承父Sum,Bru,Obam
{
public:
CSon(int iFirst,int iSec,int iThir,int iFor):CFatherSum(iFirst),m_bru(iFor),m_sum(iThir),CFatherBru(iSec)
{
//CFatherObam(iFor),m_obam(iFor) 非默认构造时,须派生构造传递构造参数
} private:
CFatherSum m_sum;
CFatherBru m_bru;
CFatherObam m_obam; }; //主函数
int main()
{
CSon son(,,,); getchar();
return ; }

结果:

c++ 派生类的构造函数次序-LMLPHP

结果分析:

构造参数次序先后如下:

1,派生类定义基类的顺序:从public CFatherSum到public CFatherBru到public CFatherObam

2,派生类内成员在类中声明的顺序:

CFatherSum m_sum;
CFatherBru m_bru;
CFatherObam m_obam;

延伸:析构函数次序相反,试验自行增加代码

05-11 22:15