继承中 子类类成员是父类对象问题 可否消除父类的父类的构造显示 代码如下
/*规划合适的类体现,设计实现对头部(Head)类型的管理,包括对眼睛(Eye),鼻子(Nose),耳朵(Ear),口(Mouth)的设计和管理。
*/
#include<iostream>
using namespace std;
class COrgan{
public:
COrgan()
{
cout<<"COrgan display!!!"<<endl;
}
};
class CEye:public virtual COrgan{
public:
CEye()
{
cout<<"CEye display!!!"<<endl;
}
};
class CNose:public virtual COrgan{
public:
CNose()
{
cout<<"CNose display!!!"<<endl;
}
};
class CMouth:public virtual COrgan{
public:
CMouth()
{
cout<<"CMouth display!!!"<<endl;
}
};
class CHead:public CEye ,public CNose ,public CMouth {
CEye eye_;
CNose nose_;
CMouth mouth_;//有他无他的区别
public:
CHead()
{
cout<<"CHead display!!!"<<endl;
}
};
void main()
{
CHead head;
}
代码运行结果:
COrgan display!!!
CEye display!!!
CNose display!!!
CMouth display!!!
COrgan display!!!//可否消除这个啊,或减少
CEye display!!!
COrgan display!!!//可否消除这个啊,或减少
CNose display!!!
COrgan display!!!//可否消除这个啊,或减少
CMouth display!!!
CHead display!!!
请按任意键继续. . .