标签:
#include <iostream> using namespace std; class CFatherSum //父类Sum { public: CFatherSum(){cout<<"1,Default Constructing CFatherSum"<<endl;}//默认构造 CFatherSum(int iRec){cout<<"1,Constructing CFatherSum"<<iRec<<endl;}//构造函数成员初始化 }; class CFatherBru //父类Bru { public: CFatherBru(){cout<<"2,Default Constructing CFatherBru"<<endl;}//默认构造 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,Default 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) 非默认构造时,须派生构造传递构造参数 } CSon(const CSon &co):CFatherBru(co),CFatherSum(co),CFatherObam(co){}//传参,子父类的兼容 public: CFatherObam m_obamCopy; private: CFatherSum m_sum; CFatherBru m_bru; CFatherObam m_obam; }; //主函数 int main() { CSon son(1,2,3,4); CSon OtherSon=son; getchar(); return 0; }
结果如下:
复制次序:按派生类的数据成员的复制构造函数,复制传值具备子父类的兼容性。
思考:
CSon(const CSon &co):CFatherBru(co),CFatherSum(co),CFatherObam(co){}//传参,子父类的兼容
向父类的非默认构造函数在复制时传递有效值
标签:
原文地址:http://www.cnblogs.com/liunnis/p/4463494.html