知识点:
1.多重继承:
形如class b:public a;class c:public b.这里使用的都是public权限,故所有基类public,protect成员对于派生类而言权限都不变,但是private成员不可直接访问。
2.访问方式:
如果一个派生类有多个基类,并且基类中有同名成员或者函数,则需要使用 类名::成员名 进行访问,避免报错。
3.虚基类:
如果遇到这种情况:
class b:public a
class c:public a
class d:public b,public c
则b,c有公共基类a。且不说同名访问问题麻烦,其次如果还有更多的继承,则基类a会一直生成,则会占用空间且操作过程麻烦。因此用到虚基类,即让该共同成员在内存中只有一个副本,同一个函数名只有一个映射。
使用方法:
class b:virtual public a
class c:virtual public a
则此时派生类d可以单独直接对a的成员访问,而不会因为b和c中a基类成员的歧义而报错。
例题:(WUSTOJ:1479)
1479: 多重继承派生(3)–person、account、admin和master类
Description
Person类包含私有成员数据姓名name(string)和编号code(int)。Account类包含私有成员数据姓名name(string)、编号code(int)和工资pay(int)。Admin类包含私有成员数据姓名name(string)、编号code(int)和实践经验experience(string)。Master类包含私有成员数据姓名name(string)、编号code(int)、工资pay(int)和实践经验experience(string)。
请根据给定的main函数以及运行结果设计相应的类及相互间的继承关系。
main函数已给定(如下所示),提交时只需要提交main函数外的代码部分。
int main()
{
string name,experience;
int code,pay,Cas=0;
while(cin>>name>>code>>pay>>experience)
{
Cas++;
cout<<“CASE #”<<Cas<<":"<<endl;
Person person(name,code);
Account account(name,code,pay);
Admin admin(name,code,experience);
Master master(name,code,pay,experience);
Show(&person);
Show(&account);
Show(&admin);
Show(&master);
}
return 0;
}
Input
包含多组数据(数据均正确合法)
每组测试数据1行,分别表示姓名,编号,工资和实践经验。
Output
每组测试数据输出具体格式详见Sample Output。
Sample Input 张三 201506 2541 绘画
王大夫 2012510 3654 跳舞
殷文琦 1005210 5412 写代码编程
Sample Output
CASE #1:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::Constructor Function is called.
Master::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:张三 CODE:201506
Show Function is called.
Account::Show Function is called.
NAME:张三 CODE:201506 PAY:2541
Show Function is called.
Admin::Show Function is called.
NAME:张三 CODE:201506 EXPERIENCE:绘画
Show Function is called.
Master::Show Function is called.
NAME:张三 CODE:201506 PAY:2541 EXPERIENCE:绘画
CASE #2:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::Constructor Function is called.
Master::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:王大夫 CODE:2012510
Show Function is called.
Account::Show Function is called.
NAME:王大夫 CODE:2012510 PAY:3654
Show Function is called.
Admin::Show Function is called.
NAME:王大夫 CODE:2012510 EXPERIENCE:跳舞
Show Function is called.
Master::Show Function is called.
NAME:王大夫 CODE:2012510 PAY:3654 EXPERIENCE:跳舞
CASE #3:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::C