2021-06-01

 类的继承与派生
设计以下类和函数
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;
class Person
{
public:
	Person(string n, string x, int a)
	{
		xm = n;
		xb = x;
		nl = a;
	}
	void SetPerson()
	{
		cout << "---------------请输入基本信息----------------" << endl;
		cout << "姓名:";
		cin >>   xm;
		cout << "性别:";
		cin >>   xb;
		cout << "年龄:";
		cin >>   nl;
	}
	void DisplayPerson()
	{
		cout << "姓名:" << xm << endl;
		cout << "性别:" << xb << endl;
		cout << "年龄:" << nl << endl;
	}
protected:
	string xm;//姓名
	string xb;//性别
	int nl;//年龄
};
class Teacher :virtual  public Person
{
public:
	Teacher(string n, string x, int a, string t, string j, string s) :Person(n, x, a)
	{
		zc = t;
		jys = j;
		sskc = s;
	}
	void SetTeacher()
	{
		cout << "---------------请输入教师信息----------------" << endl;
		cout << "姓名:";
		cin >>   xm;
		cout << "性别:";
		cin >>   xb;
		cout << "年龄:";
		cin >>   nl;
		cout << "职称:";
		cin >>   zc;
		cout << "教研室:";
		cin >>   jys;
		cout << "所授课程:";
		cin >>   sskc;
	}
	void DisplayTeacher()
	{
		cout << "==========教师基本信息为:============" << endl;
		cout << "姓名:" << xm << endl;
		cout << "性别:" << xb << endl;
		cout << "年龄:" << nl << endl;
		cout << "职称:" << zc << endl;
		cout << "教研室:" << jys << endl;
		cout << "所授课程:" << sskc << endl;
	}
protected:
	string zc;//职称
	string jys;//教研室
	string sskc;//所授课程
};
class Student :virtual public Person
{
public:
	Student(string n, string x, int a, string z, string b, string l) :Person(n, x, a)
	{
		zy = z;
		bj = b;
		lb = l;
	}
	void Setstudent()
	{
		cout << "---------------请输入学生信息----------------" << endl;
		cout << "姓名:";
		cin >>   xm;
		cout << "性别:";
		cin >>   xb;
		cout << "年龄:";
		cin >>   nl;
		cout << "专业:";
		cin >>   zy;
		cout << "班级:";
		cin >>   bj;
		cout << "类别:";
		cin >>   lb;
	}
	void DisplayStudent()
	{
		cout << "==========学生基本信息为:============" << endl;
		cout << "姓名:" << xm << endl;
		cout << "性别:" << xb << endl;
		cout << "年龄:" << nl << endl;
		cout << "专业:" << zy << endl;
		cout << "班级:" << bj << endl;
		cout << "类别:" << lb << endl;
	}
protected:
	string zy;//专业
	string bj;//班级
	string lb;//类别
};
class PostDoctor :public Student, public Teacher
{
public:
	PostDoctor(string n, string x, int a, string z, string b, string l, string t, string j, string s) :
		Person(n, x, a), Student(n, x, a, z, b, l), Teacher(n, x, a, t, j, s) {}
	void SetPostDoctor()
	{
		cout << "---------------请输入博士后信息----------------" << endl;
		cout << "姓名:";
		cin >>   xm;
		cout << "性别:";
		cin >>   xb;
		cout << "年龄:";
		cin >>   nl;
		cout << "专业:";
		cin >>   zy;
		cout << "班级:";
		cin >>   bj;
		cout << "类别:";
		cin >>   lb;
		cout << "职称:";
		cin >>   zc;
		cout << "教研室:";
		cin >>   jys;
		cout << "所授课程:";
		cin >>   sskc;
	}
	void DisplayPostDoctor()
	{
		cout << "==========博士后基本信息为:============" << endl;
		cout << "姓名:" << xm << endl;
		cout << "性别:" << xb << endl;
		cout << "年龄:" << nl << endl;
		cout << "专业:" << zy << endl;
		cout << "班级:" << bj << endl;
		cout << "类别:" << lb << endl;
		cout << "职称:" << zc << endl;
		cout << "教研室:" << jys << endl;
		cout << "所授课程:" << sskc << endl;
	}
};
int main()
{
	Teacher teacher("张三", "女", 48, "主任", "152", "数据系统概论");
	Student student("李四", "男", 20, "软件工程", "2005213", "本科生");
	PostDoctor postdoctor("王二狗", "男", 30, "计算机", "1949032", "博士后", "教师", "1001", "工程制图");
	/*
	teacher.DisplayTeacher();
	cout << "\n" << endl;
	student.DisplayStudent();
	cout << "\n" << endl;
	postdoctor.DisplayPostDoctor();
	*/
	teacher.SetTeacher();
	teacher.DisplayTeacher();
	student.Setstudent();
	student.DisplayStudent();
	postdoctor.SetPostDoctor();
	postdoctor.DisplayPostDoctor();
	return 0;
}

 

上一篇:直线段的代码裁剪算法


下一篇:【物理应用】基于matlab二维边界单元法计算腐蚀电位【含Matlab源码 523期】