hello _word
/* E2_T3 */
class Person
{
private:
string name;
int age;
public:
Person(string _name,int _age):name(_name),age(_age){}
string get_name(void) const { return name; }
int get_age(void) const { return age; };
};
class Leader :virtual /*虚基类*/ public Person
{
private:
string department;
string position;
public:
Leader(string _name,int _age,string _depart,string _posi) :\
Person(_name,_age),department(_depart),position(_posi) {}
void showInfo(void) const
{
cout<<">>Leader Information<<"<<endl;
cout<<"name: "<<get_name()<<endl; //由于 name为基类的 private 成员 在派生类不可见 解决方法:基类给出获取name数据的接口
cout<<"age: "<<get_age()<<endl;
cout<<"position: "<<position<<endl;
cout<<"department: "<<department<<endl;
cout<<endl;
}
string get_position(void) const{ return position;};
string get_department(void) const { return department;};
};
class Engineer :virtual/*虚基类*/ public Person
{
private:
string position; //职务
string speciality; //专业
public :
Engineer(string _name,int _age,string _posi,string _spec):\
Person(_name,_age),position(_posi),speciality(_spec) {}
void showInfo(void ) const
{
cout<<">>Engineer Information<<"<<endl;
cout<<"name: "<<get_name()<<endl; //由于 name为基类的 private 成员 在派生类不可见 解决方法:基类给出获取name数据的接口
cout<<"age: "<<get_age()<<endl;
cout<<"position: "<<position<<endl;
cout<<"speciality: "<<speciality<<endl;
cout<<endl;
}
string get_position(void) const{ return position;};
string get_speciality(void) const { return speciality;};
};
//虚继承 确保 在派生类当中 仅有一个 第一级 基类的 成员
class Chairman: public Leader, public Engineer
{
private:
//
public:
Chairman(string _name,int _age,string _depa,string _posi,string _spec):\
Person(_name,_age),Leader(_name,_age,_depa,_posi),\
Engineer(_name,_age,_posi,_spec)\
{} //Construction
void showInfo(void) const
{
cout<<">>Chairman Information<<"<<endl;
cout<<"name: "<<get_name()<<endl; //由于 name为基类的 private 成员 在派生类不可见 解决方法:基类给出获取name数据的接口
cout<<"age: "<<get_age()<<endl;
cout<<"department: "<<get_department()<<endl;
cout<<"position: "<<Engineer::get_position()<<endl;
cout<<"speciality: "<<get_speciality()<<endl;
cout<<endl;
}
};
//虚基类 不能实例化? 系统限定还是语意上 不建议??
int main()
{
Leader Ldr_1("ZhangWei",25,"D&R","chief inspector");
Ldr_1.showInfo();
Engineer Egr_1("Huhao",23,"developer","computer science");
Egr_1.showInfo();
Chairman ChM_1("Jony J",28,"Sales","Chairman","E-information");
ChM_1.showInfo();
return 0;
}
/* END */