C++實驗(04)

一.Time、 Date類及其子類

題目描述
已有類 Time和Date,要求設計一個派生類Birthtime,它是Time和Date的公有派生類,新增一個數據成員childName用於表示小孩的名字,設計主程序顯示一個小孩的名字和出生日期時間。數據通過鍵盤輸入,需要判斷輸入的年月日時分秒是否有效。Time與Date的成員見教材上習題4.21。 
輸入描述
Birthtime類對象的數據 
輸出描述
Birthtime類對象--小孩的姓名及出生日期時間 
輸入樣例
趙無忌
2017 8 35
2017 8 25
25 45 68(順序:時分秒)
23 45 23 
輸出樣例
日期輸入錯誤!請重新輸入數據!(中文標點)
時間輸入錯誤!請重新輸入數據!
姓名:趙無忌
出生年月:2017年8月25日
出生時間:23時45分23秒
#include <iostream>
#include <string>
using namespace std;

class Time
{
public:
	Time(int h=0, int m = 0, int s = 0)
	{
		hours = h;
		minutes = m;
		seconds = s;
	};
	void display()
	{
		cout << "出生時間:" << hours << "時" << minutes << "分" << seconds << "秒" << endl;

	}

protected:
	int hours, minutes, seconds;

};

class Date
{
public:
	Date(int m = 0, int d = 0, int y = 0)
	{
		year = m;
		month = d;
		day = y;
	}

	void display()
	{
		cout << "出生年月:" << year << "年" << month << "月" << day << "日" << endl;
	}
protected:
	int month,day,year;

};


class Birthtime :public Time, public Date
{
public:
	Birthtime(string a=" ", int b = 0, int c = 0, int d = 0, int e = 0, int f = 0, int g = 0):Time(e,f,g),Date(b,c,d)
	{
		childName = a;
	}
	void output()
	{
		cout << "姓名:" << childName <<endl;
		Date::display();
		Time::display();
	}
protected:
	string  childName;
};

int main()
{

	int year, month, day, hours, minutes, seconds;
	string childName;

	cin >> childName;
here1:
	cin >> year >> month >> day;
	if (month < 0 || month > 12 || day < 0 || day > 31)
	{
		cout << "日期輸入錯誤!請重新輸入數據!"<<endl;
		goto here1;
	}
	if (month == 2 || month == 4 || month == 6 || month == 9 || month == 11 )
	{
		if (day > 30)
		{
			cout << "日期輸入錯誤!請重新輸入數據!" << endl;
			goto here1;
		}
	}
	if (!(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
	{
		if(month == 2)
			if (day > 28)
			{
				cout << "日期輸入錯誤!請重新輸入數據!" << endl;
				goto here1;
			}
	}
here2:
	cin >> hours >> minutes >> seconds;
	if (hours < 0 || hours > 24 || minutes < 0 || minutes > 60|| seconds < 0|| seconds > 60)
	{
		cout << "時間輸入錯誤!請重新輸入數據!" << endl;
		goto here2;
	}
	Birthtime A(childName,year, month, day, hours, minutes, seconds);
	A.output();

	return 0;
}

二.Shape類及其子類

題目描述
定義一個基類Shape,有成員函數:calArea(),但什麼都不做,返回0。在此基礎上公有派生出Rectangle(矩形)類和Circle類,添加相應的數據成員(成員均爲double型),重新定義calArea()計算對象的面積。
主函數中,分別定義一個Rectangle和Circle類對象,初始值由鍵盤輸入。(1)通過對象調用相應的calArea(),輸出結果;(2)定義Shape類對象指針,分別賦以Rectangle和Circle類的對象地址,通過指針調用calArea(),輸出結果;(3)定義Shape類對象引用,以Rectangle和Circle類的對象初始化,通過引用調用calArea(),輸出結果。
PI: 3.1415926 
輸入描述
兩行
Rectangle類對象的初值
Circle類對象的初值 
輸出描述
通過基類對象、基類指針和基類對象引用調用之後的結果 
輸入樣例
3 6
3 
輸出樣例
通過Rectangle類對象調用calArea():18
通過Circle類對象調用calArea():28.2743
通過基類指針調用Rectangle類的calArea():0
通過基類指針調用Circle類的calArea():0
通過基類對象引用調用Rectangle類的calArea():0
通過基類對象引用調用Circle類的calArea():0(中文冒號)
#include <iostream>
#define PI 3.1415926
using namespace std;

class Shape
{
public:

	int calArea()
	{
		return 0;
	}

};


class Rectangle :public Shape
{
public:
	Rectangle(double a, double b)
	{
		h = a;
		w = b;
	}
	double calArea()
	{
		return h*w;
	}
	void output()
	{
		cout << "通過Rectangle類對象調用calArea():" << calArea()<<endl;
	}
protected:
	double h, w;
};

class Circle:public Shape
{
public:
	Circle(double a)
	{
		r = a;
	}
	double calArea()
	{
		return r*r*PI;
	}
	void output()
	{
		cout << "通過Circle類對象調用calArea():" << calArea() << endl;
	}
protected:
	double r;
};



int main()
{
	double h, w, r;
	cin >> h >> w >> r;
	Rectangle A(h,w);
	Circle B(r);
	Shape *p1, *p2;
	Shape &q1= A, &q2=B;
	p1 = &A;
	p2 = &B;
	A.output();
	B.output();
	cout << "通過基類指針調用Rectangle類的calArea():" << (*p1).calArea() << endl;
	cout << "通過基類指針調用Circle類的calArea():" << (*p2).calArea() << endl;
	cout << "通過基類對象引用調用Rectangle類的calArea():" << (q1).calArea() << endl;
	cout << "通過基類對象引用調用Circle類的calArea():" << (q2).calArea() << endl;
	return 0;
}

三.多繼承

題目描述
在習題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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章