欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-15 16:59:59
...

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();
}