题目描述
1、建立如下的类继承结构:
1)定义一个人员类CPeople,其属性(保护类型)有:姓名、性别、年龄;
2)从CPeople类派生出学生类CStudent,添加属性:学号和入学成绩;
3)从CPeople类再派生出教师类CTeacher,添加属性:职务、部门;
4)从CStudent和CTeacher类共同派生出在职研究生类CGradOnWork,添加属性:研究方向、导师;
2、分别定义以上类的构造函数、输出函数print及其他函数(如需要)。
3、在主函数中定义各种类的对象,并测试之。
输入
第一行:姓名性别年龄,姓名的最大字符长度为20
第二行:学号成绩
第三行:职务部门,职务和部门的最大字符长度均为20
第四行:研究方向导师,研究方向和导师的最大长度均为20
输出
第一行:People:
第二行及以后各行:格式见Sample
输入样例1
wang-li m 23
2012100365 92.5
assistant computer
robot zhao-jun
输出样例1
People:
Name: wang-li
Sex: m
Age: 23
Student:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5
Teacher:
Name: wang-li
Sex: m
Age: 23
Position: assistant
Department: computer
GradOnWork:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5
Position: assistant
Department: computer
Direction: robot
Tutor: zhao-jun
AC代码
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
class CPeople
{
protected:
string name;
char sex;
int age;
public:
CPeople(string name, char sex, int age) :name(name), sex(sex), age(age) {}
void print() {
cout << "People:" << endl;
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
cout << endl;
}
};
class CStudent :virtual public CPeople
{
protected:
string idnum;
float score;
public:
CStudent(string name, char sex, int age, string idnum, float score) :CPeople(name, sex, age), idnum(idnum), score(score) {}
void print() {
cout << "Student:" << endl;
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
cout << "No.: " << idnum << endl;
cout << "Score: " << score << endl;
cout << endl;
}
};
class CTeacher :virtual public CPeople
{
protected:
string work, department;
public:
CTeacher(string name, char sex, int age, string work, string department) :CPeople(name, sex, age), work(work), department(department) {}
void print() {
cout << "Teacher:" << endl;
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
cout << "Position: " << work << endl;
cout << "Department: " << department << endl;
cout << endl;
}
};
class CGradOnWork :public CStudent, public CTeacher
{
protected:
string direction, tutor;
public:
CGradOnWork(string name, char sex, int age, string idnum, float score, string work, string department, string direction, string tutor) :CPeople(name, sex, age), CStudent(name, sex, age, idnum, score), CTeacher(name, sex, age, work, department), direction(direction), tutor(tutor) {}
void print() {
cout << "GradOnWork:" << endl;
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
cout << "No.: " << idnum << endl;
cout << "Score: " << score << endl;
cout << "Position: " << work << endl;
cout << "Department: " << department << endl;
cout << "Direction: " << direction << endl;
cout << "Tutor: " << tutor << endl;
}
};
int main()
{
string name,idnum, work, department, direction, tutor;
char sex;
int age;
float score;
cin >> name >> sex >> age >> idnum >> score >> work >> department >> direction >> tutor;
CPeople A(name, sex, age);
A.print();
CStudent B(name, sex, age, idnum, score);
B.print();
CTeacher C(name, sex, age, work, department);
C.print();
CGradOnWork D(name, sex, age, idnum, score, work, department, direction, tutor);
D.print();
}