(c++)类多重继承和派生 - person、student、teacher和graduate类

本文通过一个C++编程示例,介绍了如何设计一个涉及多重继承和派生的类结构。Person类为基础,Student、Teacher和Graduate分别派生自Person,同时Student和Graduate类具有共同属性。示例中展示了这些类如何处理姓名、编号、出生日期、分数和部门等信息,并提供了主函数的输入输出示例。
摘要由CSDN通过智能技术生成

DescriptionPerson类包含私有成员数据姓名name(string),编号code(int)和出生年月日。Student类包含私有成员数据姓名name(string),编号code(int),出生年月日和分数score(int)。Teacher类包含私有成员数据姓名name(string),编号code(int),出生年月日和所在系department(string)。Graduate类包含私有成员数据姓名name(string),编号code(int),出生年月日,分数score(int)和所在系department(string)。
请根据给定的main函数以及运行结果设计相应的类及相互间的继承关系。
main函数已给定(如下所示),提交时只需要提交main函数外的代码部分。
int main()
{
string name,department;
int Cas=0,code,year,month,day,score;

while(cin>>name>>code>>year>>month>>day>>score>>department)
{
    Cas++;
    cout<<"CASE #"<<Cas<<":"<<endl;
    Person person(name,code,year,month,day);
    Student student(name,code,year,month,day,score);
    Teacher teacher(name,code,year,month,day,department);
    Graduate graduate(name,code,year,month,day,score,department);
    Show(&person);
    Show(&student);
    Show(&teacher);
    Show(&graduate);
}
return 0;

}

Input包含多组数据(数据均正确合法)
每组测试数据1行,分别表示姓名,编号,出生日期,分数和部门。

Output每组测试数据输出具体格式详见Sample Output。
Sample Input
张三 201506 1978 6 14 85 计算机
王大夫 2012510 1984 6 7 82 机械
殷文琦 1005210 1980 2 4 86 电信
Sample Output
CASE #1:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:张三 Code:201506 BIRTHDAY:1978-6-14
Show Function is called.
Student::Show Function is called.
NAME:张三 Code:201506 SCORE:85 BIRTHDAY:1978-6-14
Show Function is called.
Teacher::Show Function is called.
NAME:张三 Code:201506 DEPARTMENT:计算机 BIRTHDAY:1978-6-14
Show Function is called.
Graduate::Show Function is called.
NAME:张三 Code:201506 SCORE:85 DEPARTMENT:计算机 BIRTHDAY:1978-6-14
CASE #2:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:王大夫 Code:2012510 BIRTHDAY:1984-6-7
Show Function is called.
Student::Show Function is called.
NAME:王大夫 Code:2012510 SCORE:82 BIRTHDAY:1984-6-7
Show Function is called.
Teacher::Show Function is called.
NAME:王大夫 Code:2012510 DEPARTMENT:机械 BIRTHDAY:1984-6-7
Show Function is called.
Graduate::Show Function is called.
NAME:王大夫 Code:2012510 SCORE:82 DEPARTMENT:机械 BIRTHDAY:1984-6-7
CASE #3:
Person::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Person::Constructor Function is called.
Teacher::Constructor Function is called.
Person::Constructor Function is called.
Student::Constructor Function is called.
Teacher::Constructor Function is called.
Graduate::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:殷文琦 Code:1005210 BIRTHDAY:1980-2-4
Show Function is called.
Student::Show Function is called.
NAME:殷文琦 Code:1005210 SCORE:86 BIRTHDAY:1980-2-4
Show Function is called.
Teacher::Show Function is called.
NAME:殷文琦 Code:1005210 DEPARTMENT:电信 BIRTHDAY:1980-2-4
Show Function is called.
Graduate::Show Function is called.
NAME:殷文琦 Code:1005210 SCORE:86 DEPARTMENT:电信 BIRTHDAY:1980-2-4

AC代码:

#include<bits/stdc++.h>
us
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值