C ++実験04(03)多重継承

トピックの説明
は演習4-3に基づいています。Personクラスはデータメンバーとして日付オブジェクトを追加します。PersonクラスはStudentクラスとTeacherクラスを公開し、StudentクラスとTeacherはサブクラスTA(Assistant Doctoral Student)を公開します。データメンバー:professional(文字列タイプ)、コンストラクターの追加、出力関数(TAオブジェクトのすべての情報を出力)。仮想基本クラスの使用に注意してください。クラス間の関係を図4-1(電子文書)に示します。
メイン関数でTAクラスオブジェクトを定義し、キーボードで各データメンバーの初期値を入力し、TAクラスオブジェクトのすべての情報を出力し、TAオブジェクトの生年月日を変更し、データをキーボードで入力(年、月、日の順)し、TAオブジェクトのすべての情報を出力します。
入力の説明
TAオブジェクトのすべての情報
変更された生年月日情報
出力の説明
変更前
のTAオブジェクトのすべての情報変更後のTAオブジェクトのすべての情報
入力サンプル
1001Liu Xuandem Chengdu、Sichuan 123456789
1989 8 12 87 90 8588講師3500コンピュータサイエンスおよびTechnology
1989 9 12
出力サンプル
番号:1001
名前:Liu Xuande
性別:m
自宅住所:四川省成都
Tel:123456789
生年月日:1989年8月12日
数学:87
物理学:90
英語:85
プログラミング:88
役職:講師
給与: 3500
専攻:コンピュータサイエンスとテクノロジー

番号:1001
名前:Liu Xuande
性別:m
自宅住所:四川省成都
Tel: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