描述
定义以下类:
1个CPerson类,其属性有:姓名、性别和年龄。
2、从CPerson类派生出CStudent类,增加属性:学号、入学时间和入学成绩;
3、从CPerson类派生出CTeacher类,添加属性:职务、部门和工作时间;
4、由CStudent类派生出CGraduate类,添加属性:研究方向和导师;
5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWork;
6、CDate为日期类。
每个类定义一个Print函数,输出其属性值,输出见样例。
主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码。
int main() { CGradOnWork s1("crq",40, "Male", "teacher", "sxxy", CDate(2002,9,1),"15", CDate(2003, 9, 1), 400, "tm", "cs"); s1.Print(); s1.CTeacher::Print(); s1.CGraduate::Print(); s1.CStudent::Print(); s1.CPerson::Print(); return 0; }
输入
无
输出
输出样例信息
样例输入
无
样例输出
crq 40 Male teacher sxxy 2002-9-1 15 2003-9-1 400 tm cs
crq 40 Male teacher sxxy 2002-9-1
crq 40 Male 15 2003-9-1 400 tm cs
crq 40 Male 15 2003-9-1 400
crq 40 Male
#include<iostream> #include<string> using namespace std; class CDate{ public: int y,m,d; CDate(); CDate(int y,int m,int d):y(y),m(m),d(d){} }; class CPerson{ public: string name,sex; int age; CPerson(); CPerson(string name,int age,string sex):name(name),age(age),sex(sex){} void Print() { cout<<name<<" "<<age<<" "<<sex<<endl; } }; class CStudent:virtual public CPerson{ public: string id; CDate school; int grade; CStudent(); CStudent(string name,int age,string sex,string id,CDate school,int grade): CPerson(name,age,sex),id(id),school(school),grade(grade){} void Print() { cout<<name<<" "<<age<<" "<<sex<<" "<<id<<" "<<school.y<<"-"<<school.m<<"-"<<school.d<<" "<<grade<<endl; } }; class CTeacher:virtual public CPerson{ public: string duty,depart; CDate work; CTeacher(); CTeacher(string name,int age,string sex,CDate work,string duty,string depart): CPerson(name,age,sex),duty(duty),depart(depart),work(work){} void Print() { cout<<name<<" "<<age<<" "<<sex<<" "<<duty<<" "<<depart<<" "<<work.y<<"-"<<work.m<<"-"<<work.d<<endl; } }; class CGraduate:public CStudent{ public: string study,tutor; CGraduate(); CGraduate(string name,int age,string sex,string id,CDate school,int grade,string study,string tutor): CStudent(name,age,sex,id,school,grade),CPerson(name,age,sex),study(study),tutor(tutor){} void Print() { cout<<name<<" "<<age<<" "<<sex<<" "<<id<<" "<<school.y<<"-"<<school.m<<"-"<<school.d<<" "<<grade<<" "<<study<<" "<<tutor<<endl; } }; class CGradOnWork:public CGraduate,public CTeacher{ public: string name,sex; int age; CGradOnWork(); CGradOnWork(string name,int age,string sex,string duty,string depart,CDate work,string id,CDate school,int grade,string study,string tutor): CTeacher(name,age,sex,work,duty,depart),CGraduate(name,age,sex,id,school,grade,study,tutor),CPerson(name,age,sex),name(name),age(age),sex(sex){} void Print() { cout<<name<<" "<<age<<" "<<sex<<" "<<duty<<" "<<depart<<" "<<work.y<<"-"<<work.m<<"-"<<work.d<<" "<<id<<" "<<school.y<<"-"<<school.m<<"-"<<school.d<<" "<<grade<<" "<<study<<" "<<tutor<<endl; } }; int main() { CGradOnWork s1("crq",40, "Male", "teacher", "sxxy", CDate(2002,9,1),"15", CDate(2003, 9, 1), 400, "tm", "cs"); s1.Print(); s1.CTeacher::Print(); s1.CGraduate::Print(); s1.CStudent::Print(); s1.CPerson::Print(); return 0; }
原文地址:https://www.cnblogs.com/kannyi/p/8999224.html
时间: 2024-10-08 21:06:37