作业内容:
设计一个虚基类Base,然后由它派生出两个 leader和engieer类,之后再由派生出的两个类派生出一个chairman类。
个人看法:
在派生类缀上基类的构造函数时,一定要注意参数的个数是否一致。
# include <iostream>
# include <string>using namespace std;
class base{
public:
base(string name1,int age1,int exp1)
{
name=name1;
age=age1;
exp=exp1;
}
void showprint()
{
cout<<"姓名"<<" "<<name<<endl;
cout<<"年龄"<<" "<<age<<endl;
cout<<"在职年数"<<" "<<exp<<endl;
}
private:
string name;
int age;
int exp;
};
class leader:virtual public base{
public:
leader(string name1,int age1,int exp1,string post1,string work1):base(name1,age1,exp1)
{
post=post1;
work=work1;}
void print()
{
cout<<"职务"<<" "<<post<<endl;
cout<<"部门"<<" "<<work<<endl;
}
private:
string post;
string work;
};
class engieer:virtual public base{
public:
engieer(string name1,int age1,int exp1,string title1,string major1):base(name1,age1,exp1)
{
title=title1;
major=major1;
}
void print()
{
cout<<"职称"<<" "<<title<<endl;
cout<<"专业"<<" "<<major<<endl;
}
private:
string title;
string major;
};
class chairman:public leader,public engieer{
public:
chairman(string name1,int age1,int exp1,string post1,string work1,string title1,string major1)
:base(name1,age1,exp1),leader(name1, age1, exp1, post1,work1),engieer( name1, age1, exp1, title1, major1)
{}
void print()
{
base::showprint();
leader::print();
engieer::print();
}
};
int main()
{
chairman a("都敏俊",404,404,"利用超能力拯救女主","KMT184.05星球","大学讲师","生物学");
a.print();
return 0;
}