C++学习笔记:类的多重继承派生(eg:person、account、admin和master类)

这篇博客探讨了C++中的多重继承,特别是类person、account、admin和master之间的派生关系。通过示例解释了如何处理基类中的同名成员和函数,以及如何利用虚基类解决内存冗余和访问问题。同时给出了WUSTOJ 1479题目的详细说明和解决方案。
摘要由CSDN通过智能技术生成

知识点:

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

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值