C++实验04(03)多继承

题目描述
在习题4-3的基础上,Person类添加日期类对象作数据成员,由Person类公有派生出Student类和Teacher类,由Student类和Teacher类公有派生子类TA(助教博士生)类,添加数据成员:专业(string类型),添加构造函数、输出函数(输出一个TA对象的全部信息)。注意虚基类的使用。类之间的关系如图4-1所示(电子文档中)。
主函数中定义一个TA类对象,其各数据成员的初始值由键盘输入,输出TA类对象的全部信息;修改TA对象的出生年月日,数据由键盘输入(按年月日顺序),再输出TA类对象的全部信息。
输入描述
一个TA类对象的全部信息
修改的出生年月日信息
输出描述
修改前TA类对象的全部信息
修改后TA类对象的全部信息
输入样例
1001 刘玄德 m 四川成都 123456789
1989 8 12 87 90 85 88 讲师 3500 计算机科技与技术
1989 9 12
输出样例
编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:四川成都
联系电话:123456789
出生日期:1989年8月12日
数 学:87
物 理:90
英 语:85
程序设计:88
职 称:讲师
工 资:3500
专 业:计算机科技与技术

编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:四川成都
联系电话:123456789
出生日期:1989年9月12日
数 学:87
物 理:90
英 语:85
程序设计:88
职 称:讲师
工 资:3500
专 业:计算机科技与技术

#include <iostream>
#include <string>
using namespace std;

class Date
{
    
    
public:
	Date(int m = 0, int d = 0, int y = 0)
	{
    
    
		year = m;
		month = d;
		day = y;
	}
	void setDate(int a, int b, int c)
	{
    
    
		year = a;
		month = b;
		day = c;
	}
	void display()
	{
    
    
		cout << "出生日期:" << year << "年" << month << "月" << day << "日" << endl;
	}
protected:
	int month, day, year;

};
class Person
{
    
    
public:
	Person(string a = " ", string b = " ", char c = ' ', string d = " ", string e = " ")
	{
    
    
		Num = a;
		Name = b;
		Sex = c;
		Adr = d;
		Tel = e;
	}
	void SetNum(string a)
	{
    
    
		Num = a;
	}
	void SetName(string a)
	{
    
    
		Name = a;
	}
	void SetNum(char a)
	{
    
    
		Sex = a;
	}
	void SetAdr(string a)
	{
    
    
		Adr = a;
	}
	void SetTel(string a)
	{
    
    
		Tel = a;
	}
	void output()
	{
    
    
		cout << "编    号:" << Num << endl;
		cout << "姓    名:" << Name << endl;
		cout << "性    别:" << Sex << endl;
		cout << "家庭住址:" << Adr << endl;
		cout << "联系电话:" << Tel << endl;
	}
protected:
	string Num;
	string Name;
	char Sex;
	string Adr;
	string Tel;
	Date A;
};
class Student :virtual public Person//消除二义性
{
    
    
public:
	Student(int a = 0, int b = 0, int c = 0, int d = 0)
	{
    
    
		mScore = a;
		pScore = b;
		eScore = c;
		cScore = d;
	}
	void setScore(char tag, int score)
	{
    
    
		switch (tag)
		{
    
    
		case 'm':mScore = score; break;
		case 'p':pScore = score; break;
		case 'e':eScore = score; break;
		case 'c':cScore = score; break;
		}

	}
	void output1()
	{
    
    
		cout << "编    号:" << Num << endl;
		cout << "姓    名:" << Name << endl;
		cout << "性    别:" << Sex << endl;
		cout << "家庭住址:" << Adr << endl;
		cout << "联系电话:" << Tel << endl;
		cout << "数    学:" << mScore << endl;
		cout << "物    理:" << pScore << endl;
		cout << "英    语:" << eScore << endl;
		cout << "程序设计:" << cScore << endl;
	}
protected:
	int mScore;
	int pScore;
	int eScore;
	int cScore;
};

class Teacher :virtual public Person
{
    
    
public:
	Teacher(string a = " ", double b = 0)
	{
    
    
		ZhiCheng = a;
		GongZi = b;
	}
	void SetZhiCheng(string a)
	{
    
    
		ZhiCheng = a;
	}
	void SetGongZi(double a)
	{
    
    
		GongZi = a;
	}
	void output2()
	{
    
    
		cout << "编    号:" << Num << endl;
		cout << "姓    名:" << Name << endl;
		cout << "性    别:" << Sex << endl;
		cout << "家庭住址:" << Adr << endl;
		cout << "联系电话:" << Tel << endl;
		cout << "职    称:" << ZhiCheng << endl;
		cout << "工    资:" << GongZi << endl;
	}
protected:
	string ZhiCheng;
	double GongZi;
};

class TA :public Student, public Teacher
{
    
    
public:
	TA(string a, string b, char c, string d, string e, string j = " ", int aa = 0, int bb = 0, int cc = 0, int dd = 0, string ee = " ", double ff = 0) :Student(aa, bb, cc, dd), Teacher(ee, ff)
	{
    
    
		Num = a;
		Name = b;
		Sex = c;
		Adr = d;
		Tel = e;
		major = j;
	}
	void setDate(int a, int b, int c)
	{
    
    
		A.setDate(a, b, c);
	}
	void output3()
	{
    
    
		cout << "编    号:" << Num << endl;
		cout << "姓    名:" << Name << endl;
		cout << "性    别:" << Sex << endl;
		cout << "家庭住址:" << Adr << endl;
		cout << "联系电话:" << Tel << endl;
		A.display();
		cout << "数    学:" << mScore << endl;
		cout << "物    理:" << pScore << endl;
		cout << "英    语:" << eScore << endl;
		cout << "程序设计:" << cScore << endl;
		cout << "职    称:" << ZhiCheng << endl;
		cout << "工    资:" << GongZi << endl;
		cout << "专    业:" << major << endl;
	}
protected:
	string major;
};

int main()
{
    
    
	string Num;
	string Name;
	char Sex;
	string Adr;
	string Tel;
	int month, day, year;
	int month1, day1, year1;
	int mScore;
	int pScore;
	int eScore;
	int cScore;
	string ZhiCheng;
	double GongZi;
	string major;
	cin >> Num >> Name >> Sex >> Adr >> Tel >> year >> month >> day >> mScore >> pScore >> eScore >> cScore >> ZhiCheng >> GongZi >> major;
	cin >> year1 >> month1 >> day1;
	TA A(Num, Name, Sex, Adr, Tel, major, mScore, pScore, eScore, cScore, ZhiCheng, GongZi);
	A.setDate(year, month, day);
	A.output3();
	A.setDate(year1, month1, day1);
	cout << "\n";
	A.output3();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44179485/article/details/105890590