#include<iostream>
#include<stdio.h>
using namespace std;
/************************************************************************
* 多重继承(多层继承)
************************************************************************/
//基类CBase
class CBase
{
int a;
public:
CBase(int na)
{
a=na;
cout<<"CBase constructor! " << endl;
}
~CBase(){cout<<"CBase deconstructor! "<< endl;}
int GetA(){return a;}
};
//CBase的派生类CDerive
class CDerive : public CBase
{
int b;
public:
//将参数na传递给基类CBase
CDerive(int nb,int na):CBase(na)
{
b=nb;
cout<<"CDerive constructor! "<< endl;
}
~CDerive(){cout<<"CDerive deconstructor! "<< endl;}
int GetB(){return b;}
};
//CDerive的派生类CSubDerive
class CSubDerive : public CDerive
{
int c;
public:
//向直接基类传递参数,不负责向间接基类传递参数
CSubDerive(int nc,int nb,int na):CDerive(nb,na)
{
c=nc;
cout<<"CSubDerive constructor! "<< endl;
}
~CSubDerive(){cout<<"CSubDerive deconstructor! "<< endl;}
int GetC(){return c;}
void show()
{
//因为是公有继承,子派生类可以访问其父类和父类的父类的公有成员函数
cout<<"int the subderive class object : "<< endl;
cout<<"a = "<<GetA()<<endl;
cout<<"b = "<<GetB()<<endl;
cout<<"c = "<<GetC()<<endl;
}
};
int main()
{
CSubDerive obj(30,20,10);
cout<<"call the display function of the object: "<< endl;
obj.show();
cout<<" call the interface function of the object: "<< endl;
cout<<"a = "<<obj.GetA()<<endl;
cout<<"b = "<<obj.GetB()<<endl;
cout<<"c = "<<obj.GetC()<<endl;
return 0;
}
C++学习 (3)--多重继承类对象的构造函数参数的传递方法
最新推荐文章于 2022-09-14 10:07:16 发布