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>
using namespace std;
class Person
{public:string name;int code,year,month,day;Person(string &n,int c,int y,int m,int d);virtual void show();//虚函数,便于基类指针访问各派生类的函数
};
Person::Person(string &n,int c,int y,int m,int d):name(n),code(c),year(y),month(m),day(d){cout<<"Person::Constructor Function is called."<<endl;
}
void Person::show(){cout<<"Person::Show Function is called."<<endl;cout<<"NAME:"<<name<<" Code:"<<code<<" BIRTHDAY:"<<year<<'-'<<month<<'-'<<day<<endl;
}
class Student:virtual public Person
{public:int score;Student(string &n,int c,int y,int m,int d,int s);void show();
};
Student::Student(string &n,int c,int y,int m,int d,int s):Person(n,c,y,m,d),score(s){cout<<"Student::Constructor Function is called."<<endl;
}
void Student::show(){cout<<"Student::Show Function is called."<<endl;cout<<"NAME:"<<name<<" Code:"<<code<<" SCORE:"<<score<<" BIRTHDAY:"<<year<<'-'<<month<<'-'<<day<<endl;
}
class Teacher:virtual public Person//虚基类,只生成一个副本,避免二义性且提高效率
{public:string department;Teacher(string &n,int c,int y,int m,int d,string &depar);void show();
};
Teacher::Teacher(string &n,int c,int y,int m,int d,string &depar):Person(n,c,y,m,d),department(depar){cout<<"Teacher::Constructor Function is called."<<endl;
}
void Teacher::show(){cout<<"Teacher::Show Function is called."<<endl;cout<<"NAME:"<<name<<" Code:"<<code<<" DEPARTMENT:"<<department<<" BIRTHDAY:"<<year<<'-'<<month<<'-'<<day<<endl;
}
class Graduate: public Student,public Teacher
{public:Graduate(string &n,int c,int y,int m,int d,int s,string &depar);void show();
};
Graduate::Graduate(string &n,int c,int y,int m,int d,int s,string &depar):Person(n,c,y,m,d),Student(n,c,y,m,d,s),Teacher(n,c,y,m,d,depar){cout<<"Graduate::Constructor Function is called."<<endl;
}//由于是单独建立的副本,故需要单独对这个基类初始化;还有,基类成员不属于派生类,需要用构造函数对基类成员初始化
void Graduate::show(){cout<<"Graduate::Show Function is called."<<endl;cout<<"NAME:"<<name<<" Code:"<<code<<" SCORE:"<<score<<" DEPARTMENT:"<<department<<" BIRTHDAY:"<<year<<'-'<<month<<'-'<<day<<endl;
}
void Show(Person* p)
{cout<<"Show Function is called."<<endl;p->show();
}

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

  1. (C++实例)实现people类、student类,teacher类、graduate类、助教类继承和派生并测试

    1.问题描述 共有以下 4小点 的类继承.派生关系: (1)从people(人员)类派生出student(学生)类,添加属性:班号char classNo[7]; (2)从people类派生出teac ...

  2. 定义一个Teacher(教师)类,和一个Student(学生)类

    定义一个Teacher(教师)类,和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别).编写程序,将一个Student对象 转换为Teac ...

  3. 定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleMana

    定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleMana ...

  4. cpp课程设计实验题:定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生

    ``定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleMa ...

  5. Uninformed Students: Student–Teacher Anomaly Detection with Discriminative Latent Embeddings(翻译)

    未知学生:学生-教师异常检测与鉴别潜在嵌入 原文:https://export.arxiv.org/pdf/1911.02357 Uninformed Students: Student–Teache ...

  6. C++改变基类成员在派生类中的访问属性

    使用using声明可以改变基类成员在派生类中的访问属性.我们知道基类的公有成员经过公有继承,在派生类中其属性为public的,但是通过using 声明,我们可以将其改为private或protecte ...

  7. Java黑皮书课后题第11章:11.2(Person Student Employee Faculty Staff类)设计一个名为Person的类及其两个名为Student和Employee的子类

    Java黑皮书课后题第11章:11.2(Person Student Employee Faculty Staff类) 题目 缺陷 UML图 代码 Test02_MyDate.java:用于参考的My ...

  8. php 派生类 构造,C++派生类的构造函数和析构函数

    派生类对象中包含基类对象,因此派生类对象在创建时,除了要调用自身的构造函数进行初始化外,还要调用基类的构造函数初始化其包含的基类对象.因此,程序中任何能够生成派生类对象的语句,都要说明其包含的基类对象 ...

  9. 基类数组存放派生类_永远不要将派生类数组赋值给基类类型指针

    C.152: Never assign a pointer to an array of derived class objects to a pointer to its base C.152:永远 ...

  10. C++基类指针指向派生类(指针)

    我们常用基类指针指向派生类对象来实现多态性. 私有继承不允许基类指针指向派生类 基类指针只能访问到基类中含有的公有成员. 当用基类指针指向派生类对象在动态分配堆上内存的时候,析构函数必须是虚函数! 成 ...

最新文章

  1. Redis-12Redis 流水线( pipeline )
  2. python代码生成可执行程序_Python—脚本程序生成exe可执行程序(pyinstaller)
  3. 轮换html有虚宽出现,乒乓球理论考试复习资料
  4. 大学生应当趁早谋划未来(二)--给表弟的建议
  5. PHP和MySQL Web开发从新手到高手,第7天-创建author管理页面
  6. 陈式心意混元太极拳功要
  7. 开源的 Snort 入侵检测系统中存在高危漏洞
  8. MUI Picker选择器 自定义省市地址三级联动
  9. Linux Fedora 15 安装 Atheros AR9285 无线网卡驱动
  10. libiconv_百度百科
  11. Java Web项目中使用Freemarker生成Word文档
  12. python实现王者荣耀游戏框架
  13. 树莓派触摸屏翻转显示以及触摸翻转
  14. java c语言与人工智能_C语言与LISP语言的区别
  15. 点,线,面,透视(手绘课)
  16. rtx2060相当于gtx多少 rtx2060属于什么档次的显卡 rtx2060显卡怎么样
  17. 主流CA吊销俄罗斯数字证书启示:升级国密算法SSL证书,助力我国网络安全自主可控
  18. java采购管理系统设计_Java毕业设计——采购管理系统的设计参考
  19. python开发mbus程序_程序不知道怎么调,那个MBUS
  20. 2023华为od机试真题B卷【跳房子2】Java 实现

热门文章

  1. 01背包、完全背包问题几种变式总结,以及多重背包、组合背包模板
  2. Linux ss命令 报错,linux下SS 网络命令详解
  3. 英文翻译用什么软件好?快看这三款
  4. LinuxDay1——计算机基础
  5. 乌班图18.04安装MySQL
  6. Stata空间计量:STAR-时空自回归模型
  7. 1786. 韩信点兵
  8. 500个计算机毕业设计项目推荐(源码+论文+PPT)
  9. 神经网络入门经典书籍,最简单的神经网络算法
  10. 访问网络-------开源-Volley(Google亲儿子)