本文主要是介绍1480: 多重继承派生(4)--person、student、teacher和graduate类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1480: 多重继承派生(4)--person、student、teacher和graduate类
Description
Person类包含私有成员数据姓名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
代码:
#include <iostream>#include <string>
using namespace std;
class Person
{
public:
Person(string name,int code,int year,int month,int day):name(name),code(code),year(year),month(month),day(day)
{
cout<<"Person::Constructor Function is called."<<endl;
}
virtual void Show()
{
cout<<"Person::Show Function is called."<<endl;
cout<<"NAME:"<<name<<" Code:"<<code<<" BIRTHDAY:"<<year<<"-"<<month<<"-"<<day<<endl;
}
string GetName()
{
return name;
}
int GetCode()
{
return code;
}
int GetYear()
{
return year;
}
int GetMonth()
{
return month;
}
int GetDay()
{
return day;
}
private:
string name;
int code;
int year,month,day;
};
class Student:virtual public Person
{
public:
Student(string name,int code,int year,int month,int day,int score):Person(name,code,year,month,day),score(score)
{
cout<<"Student::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Student::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" SCORE:"<<GetScore()<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
}
int GetScore()
{
return score;
}
private:
int score;
};
class Teacher:virtual public Person
{
public:
Teacher(string name,int code,int year,int month,int day,string department):Person(name,code,year,month,day),department(department)
{
cout<<"Teacher::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Teacher::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" DEPARTMENT:"<<department<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
}
string GetDepartment()
{
return department;
}
private:
string department;
};
class Graduate:public Student,public Teacher
{
public:
Graduate(string name,int code,int year,int month,int day,int score,string department):Person(name,code,year,month,day),Student(name,code,year,month,day,score),Teacher(name,code,year,month,day,department)
{
cout<<"Graduate::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Graduate::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" Code:"<<GetCode()<<" SCORE:"<<GetScore()<<" DEPARTMENT:"<<GetDepartment()<<" BIRTHDAY:"<<GetYear()<<"-"<<GetMonth()<<"-"<<GetDay()<<endl;
}
};
void Show(Person* p)
{
cout<<"Show Function is called."<<endl;
p->Show();
}
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;
}
这篇关于1480: 多重继承派生(4)--person、student、teacher和graduate类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!