类的继承与派生
设计以下类和函数
1.人员基类Person。其成员包括:
数据成员:姓名、性别、年龄
成员函数:SetPerson,设置人员数据函数;
DisplayPerson,显示人员数据函数。
2.派生类1,Teacher, 派生自Person。新增成员包括:
数据成员:职称、教研室、所授课程
成员函数:SetTeacher,设置数据
DisplayTeacher,显示数据
3.派生类2,Student,派生自Person。新增成员包括:
数据成员:专业、班级、类别
成员函数:SetStudent,设置数据
DisplayStudent, 显示数据
4.派生类3,PostDoctor,多重继承于Student和Teacher。新增成员包括:
数据成员:无
成员函数:SetPostDoctor,设置数据
DisplayPostDoctor, 显示数据
5.主函数:输入并输出一个教师、一个本科生、一个博士后数据。
要求每个类都要定义有参数的构造函数,注意虚基类的使用。
#include<iostream>
#include<string>
using namespace std;
//基类Person
class Person
{
private:
string name;//名字
string sex;//性别
string age;//年龄
public:
void SetPerson();
void DisplayPerson();
};
//继承于Person类的Teacher类
class Teacher :public Person
{
private:
string ranks;
string room;
string course;
public:
void SetTeacher();
void DisplayTeacher();
};
//继承于Person类的Student类
class Student :public Person
{
private:
string profession;
string class_num;
string class_class;
public:
void SetStudent();
void DisplayStudent();
};
//多重继承,既继承于Student类又继承于Teacher类的PostDoctor类
class PostDoctor : public Student, public Teacher
{
public:
void SetPostDoctor();
void DisplayPostDoctor();
};
void Person::SetPerson()
{
cout << "==============输入基本人员信息===============" << endl;
cout << "姓名:";
cin >> this->name;
cout << "年龄:";
cin >> this->age;
cout << "性别:";
cin >> this->sex;
}
void Teacher::SetTeacher()
{
Person::SetPerson();
cout << "==============输入教师基本信息===============" << endl;
cout << "职称:";
cin >> this->ranks;
cout << "教研室:";
cin >> this->room;
cout << "所授课程:";
cin >> this->course;
}
void Student::SetStudent()
{
Person::SetPerson();
cout << "==============输入学生基本信息===============" << endl;
cout << "专业:";
cin >> this->profession;
cout << "班级:";
cin >> this->class_num;
cout << "类别:";
cin >> this->class_class;
}
void PostDoctor::SetPostDoctor()
{
Student::SetStudent();
}
void Person::DisplayPerson()
{
cout << "===" << endl;
cout << "该人信息如下:" << endl;
cout << "姓名:" << this->name << endl;
cout << "性别:" << this->sex << endl;
cout << "年龄:" << this->age << endl;
}
void Teacher::DisplayTeacher()
{
Person::DisplayPerson();
cout << "职称:" << this->ranks << endl;
cout << "教研室:" << this->room << endl;
cout << "所授课程:" << this->course << endl;
cout << "类别:教师" << endl;
}
void Student::DisplayStudent()
{
Person::DisplayPerson();
cout << "专业:" << this->profession << endl;
cout << "班级:" << this->class_num << endl;
cout << "类别:" << this->class_class << endl;
}
void PostDoctor::DisplayPostDoctor()
{
Student::DisplayStudent();
}
int main()
{
Teacher a;
Student b;
PostDoctor c;
a.SetTeacher();
b.SetStudent();
c.SetPostDoctor();
a.DisplayTeacher();
b.DisplayStudent();
c.DisplayPostDoctor();
return 0;
}