#include <iostream>
using namespace std;
/*---------------------------------
11多重继承
---------------------------------*/
class father
{
private:
int tall;
public:
void setA(int a)
{tall =a;}
void printA()
{cout<<"身高="<<tall<<endl;}
};
class mother
{
private:
int weight;
public:
void setB(int a)
{weight =a;}
void printB()
{cout<<"体重="<<weight<<endl;}
};
//class son:public father,public mother //多重继承语法格式 mother公有派生
class son:public father,private mother //多重继承语法格式 mother私有派生
{
private:
int age;
public:
void setC(int a)
{age =a;}
void setb(int a) //mother私有派生的情况下只能间接调用setB
{setB(a);}
void printC()
{printA();printB();cout<<"年龄="<<age<<endl;}
};
int main()
{
son a;
a.setA(140);
// a.setB(110); //mother私有派生的情况下只能间接调用setB
a.setb(110); //mother私有派生的情况下只能间接调用setB
a.setC(24);
a.printC();
return 0;
}
运行结果:
身高=140
体重=110
年龄=24
Press any key to continue
C++ 继承 4--多重继承
最新推荐文章于 2020-05-03 13:20:57 发布