类的继承与派生

类的继承与派生
设计以下类和函数
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值