深圳大学计软《面向对象的程序设计》实验11 多继承

A. 在职研究生(多重继承)

题目描述

1、建立如下的类继承结构:

1)定义一个人员类CPeople,其属性(保护类型)有:姓名、性别、年龄;

2)从CPeople类派生出学生类CStudent,添加属性:学号和入学成绩;

3)从CPeople类再派生出教师类CTeacher,添加属性:职务、部门;

4)从CStudent和CTeacher类共同派生出在职研究生类CGradOnWork,添加属性:研究方向、导师;

2、分别定义以上类的构造函数、输出函数print及其他函数(如需要)。

3、在主函数中定义各种类的对象,并测试之。

输入

第一行:姓名性别年龄

第二行:学号成绩

第三行:职务部门

第四行:研究方向导师

输出

第一行:People:

第二行及以后各行:格式见Sample

输入样例1

wang-li m 23
2012100365 92.5
assistant computer
robot zhao-jun

输出样例1

People:
Name: wang-li
Sex: m
Age: 23

Student:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5

Teacher:
Name: wang-li
Sex: m
Age: 23
Position: assistant
Department: computer

GradOnWork:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5
Position: assistant
Department: computer
Direction: robot
Tutor: zhao-jun

AC代码

#include
using namespace std;

class CPeople {
protected:
	string name;
	string sex;
	int age;
public:
	CPeople(string n, string s, int a) :name(n), sex(s), age(a) {}
	void print() {
		cout << "People:" << endl;
		cout << "Name: " << name << endl;
		cout << "Sex: " << sex << endl;
		cout << "Age: " << age << endl;
		cout << endl;
	}
};

class CStudent :virtual public CPeople {
protected:
	string id;
	double grade;
public:
	CStudent(string n, string s, int a, string id, double g) :CPeople(n, s, a), id(id), grade(g) {}
	void print() {
		cout << "Student:" << endl;
		cout << "Name: " << name << endl;
		cout << "Sex: " << sex << endl;
		cout << "Age: " << age << endl;
		cout << "No.: " << id << endl;
		cout << "Score: " << grade << endl;
		cout << endl;
	}
};


class CTeacher :virtual public CPeople {
protected:
	string position, department;
public:
	CTeacher(string n, string s, int a, string p, string d) :CPeople(n, s, a), position(p), department(d) {}
	void print() {
		cout << "Teacher:" << endl;
		cout << "Name: " << name << endl;
		cout << "Sex: " << sex << endl;
		cout << "Age: " << age << endl;
		cout << "Position: " << position << endl;
		cout << "Department: " << department << endl;
		cout << endl;
	}
};


class CGradOnWork :public CStudent, public CTeacher {
protected:
	string direction, tutor;
public:
	CGradOnWork(string n, string s, int a, string id, double g, string p, string d, string dir, string tu) :
		CPeople(n, s, a),
		CStudent(n, s, a, id, g),
		CTeacher(n, s, a, p, d),
		direction(dir),
		tutor(tu)
	{

	}

	void print() {
		cout << "GradOnWork:" << endl;
		cout << "Name: " << name << endl;
		cout << "Sex: " << sex << endl;
		cout << "Age: " << age << endl;
		cout << "No.: " << id << endl;
		cout << "Score: " << grade << endl;
		cout << "Position: " << position << endl;
		cout << "Department: " << department << endl;
		cout << "Direction: " << direction << endl;
		cout << "Tutor: " << tutor << endl;
		cout << endl;
	}

};


int main() {
	string s1, s2;
	int s3;
	string s4;
	double s5;
	string s6, s7, s8, s9;
	cin >> s1 >> s2 >> s3 >> s4 >> s5 >> s6 >> s7 >> s8 >> s9;
	CPeople(s1, s2, s3).print();
	CStudent(s1, s2, s3, s4, s5).print();
	CTeacher(s1, s2, s3, s6, s7).print();
	CGradOnWork c(s1, s2, s3, s4, s5, s6, s7, s8, s9);
	c.print();
	return 0;
}

B. 交通工具(多重继承)

题目描述

1、建立如下的类继承结构:

1)一个车类CVehicle作为基类,具有max_speed、speed、weight等数据成员,display()等成员函数

2)从CVehicle类派生出自行车类CBicycle,添加属性:高度height

3)从CVehicle类派生出汽车类CMotocar,添加属性:座位数seat_num

4)从CBicycle和CMotocar派生出摩托车类CMotocycle

2、分别定义以上类的构造函数、输出函数display及其他函数(如需要)。

3、在主函数中定义各种类的对象,并测试之,通过对象调用display函数产生输出。

输入

第一行:最高速度 速度 重量 第二行:高度 第三行:座位数

输出

第一行:Vehicle: 第二行及以后各行:格式见Sample

输入样例1

100 60 20
28
2

输出样例1

Vehicle:
max_speed:100
speed:60
weight:20

Bicycle:
max_speed:100
speed:60
weight:20
height:28

Motocar:
max_speed:100
speed:60
weight:20
seat_num:2

Motocycle:
max_speed:100
speed:60
weight:20
height:28
seat_num:2

AC代码

#include
using namespace std;

class CVehicle {
protected:
	int max_speed, speed, weight;
public:
	CVehicle(int ms, int sp, int we) :max_speed(ms), speed(sp), weight(we) {}


	void display() {
		cout << "Vehicle:" << endl;
		cout << "max_speed:" << max_speed << endl;
		cout << "speed:" << speed << endl;
		cout << "weight:" << weight << endl;
		cout << endl;
	}
};

class CBicycle :virtual public CVehicle {
protected:
	int height;
public:
	CBicycle(int ms, int sp, int we, int h) :
		CVehicle(ms, sp, we),
		height(h) {}



	void display() {
		cout << "Bicycle:" << endl;
		cout << "max_speed:" << max_speed << endl;
		cout << "speed:" << speed << endl;
		cout << "weight:" << weight << endl;
		cout << "height:" << height << endl;
		cout << endl;
	}
};

class CMotocar :virtual public CVehicle {
protected:
	int seat_num;
public:
	CMotocar(int ms, int sp, int we, int sn) :
		CVehicle(ms, sp, we),
		seat_num(sn) {}

	void display() {
		cout << "Motocar:" << endl;
		cout << "max_speed:" << max_speed << endl;
		cout << "speed:" << speed << endl;
		cout << "weight:" << weight << endl;
		cout << "seat_num:" << seat_num << endl;
		cout << endl;
	}
};

class CMotocycle :public CBicycle, public CMotocar {
public:
	CMotocycle(int ms, int sp, int we, int h, int sn) :
		CVehicle(ms, sp, we),
		CBicycle(ms, sp, we, h),
		CMotocar(ms, sp, we, sn) {}

	void display() {
		cout << "Motocycle:" << endl;
		cout << "max_speed:" << max_speed << endl;
		cout << "speed:" << speed << endl;
		cout << "weight:" << weight << endl;
		cout << "height:" << height << endl;
		cout << "seat_num:" << seat_num << endl;
		cout << endl;
	}
};

int main() {
	int a, b, c, d, e;
	cin >> a >> b >> c >> d >> e;
	CVehicle(a, b, c).display();
	CBicycle(a, b, c, d).display();
	CMotocar(a, b, c, e).display();
	CMotocycle(a,b,c,d,e).display();

	return 0;
}

C. 商旅信用卡(多重继承)

题目描述

某旅游网站(假设旅程网)和某银行推出旅游综合服务联名卡—旅程信用卡,兼具旅程会员卡和银行信用卡功能。
旅程会员卡,有会员卡号(int)、旅程积分(int),通过会员卡下订单,按订单金额累计旅程积分。
信用卡,有卡号(int)、姓名(string)、额度(int)、账单金额(float)、信用卡积分(int)。------请注意数据类型。
信用卡消费金额m,若加已有账单金额超额度,则不做操作;否则,账单金额+m,信用卡积分按消费金额累加。
信用卡退款m,账单金额-m,信用卡积分减去退款金额。
通过旅程信用卡在旅程网下单,旅程积分和信用卡积分双重积分(即旅程积分和信用卡积分同时增加)。
旅程信用卡可以按旅程积分:信用卡积分= 1:2 的比例将信用卡积分兑换为旅程积分。
初始假设信用卡积分、旅程积分、账单金额为0。
根据上述内容,定义旅程会员卡类、信用卡类,从两者派生出旅程信用卡类并定义三个类的构造函数和其它所需函数。
生成旅程信用卡对象,输入卡信息,调用对象成员函数完成旅程网下单、信用卡刷卡、信用卡退款、信用卡积分兑换为旅程积分等操作。

输入

第一行:输入旅程会员卡号 信用卡号 姓名 额度
第二行:测试次数n
第三行到第n+2行,每行:操作 金额或积分
o m(旅程网下订单,订单金额m)
c m(信用卡消费,消费金额m)
q m (信用卡退款,退款金额m)
t m(积分兑换,m信用卡积分兑换为旅程积分)

输出

输出所有操作后旅程信用卡的信息:
旅程号 旅程积分
信用卡号 姓名 账单金额 信用卡积分

输入样例1

1000 2002 lili 3000
4
o 212.5
c 300
q 117.4
t 200

输出样例1

1000 312
2002 lili 395.1 195

AC代码

#include
using namespace std;

class Member {
protected:
	int id;
	int score;
public:
	Member(int i, int s = 0) :id(i), score(s) {}
	void display() {
		cout << id << " " << score << endl;
	}
};


class Credit {
protected:
	int id;
	string name;
	int balance;//额度
	float value;//账单金额
	int score;
public:
	Credit(int i, string n, int b) :
		id(i),
		name(n),
		balance(b),
		value(0),
		score(0) {}

	void display() {
		cout << id << " " << name << " " << value << " " << score << endl;
	}
};


class MyClass :
	public Member,
	public Credit
{
public:
	MyClass(int mid, int cid, string name, int ba) :
		Member(mid),
		Credit(cid, name, ba) {}

	void o() {
		float m;
		cin >> m;
		if (m + value > balance)
			return;
		value += m;
		Member::score += (int)m;
		Credit::score += (int)m;
	}

	void c() {
		float m;
		cin >> m;
		if (m + value > balance)
			return;
		value += m;
		Credit::score += (int)m;
	}

	void q() {
		float m;
		cin >> m;
		Credit::score -= (int)m;
		value -= m;
	}

	void t() {
		float m;
		cin >> m;
		Credit::score -= m;
		Member::score += m / 2.0;
	}

	void display() {
		Member::display();
		Credit::display();
	}

};


int main() {
	int s1, s2, s4;
	string s3;
	cin >> s1 >> s2 >> s3 >> s4;
	MyClass mm(s1, s2, s3, s4);
	int n;
	cin >> n;
	while (n--) {
		char c;
		cin >> c;
		switch (c)
		{
		case 'o':
			mm.o();
			break;
		case 'c':
			mm.c();
			break;
		case 'q':
			mm.q();
			break;
		case 't':
			mm.t();
		default:
			break;
		}
	}
	mm.display();
}

D. 日程安排(多继承+友元函数)

题目描述

已有一个日期类Date,包括三个protected成员数据year,month,day;

另有一个时间类Time,包括三个protected成员数据hour,minute,second,12小时制;

现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:

int ID;//日程的ID

定义友元函数bool before(const Schedule & s1,const Schedule & s2);//判断日程s1时间是否早于日程s2。

编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程(日期和时间相等时,输出较早建立的日程),并输出该日程对象的信息。

输入

测试输入包含若干日程,每个日程占一行(日程ID日程日期日程时间)。

当读入0时输入结束,相应的结果不要输出。

输出

时间最靠前的日程

输入样例1

1 2019 6 27 8 0 1
2 2019 6 28 8 0 1
3 2020 1 1 8 0 0
0

输出样例1

The urgent schedule is No.1: 2019/06/27 08:00:01

AC代码

#include
using namespace std;

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

	void print()const {
		cout << setfill('0') << setw(2) << year << "/" << setfill('0') << setw(2) << month << "/" << setfill('0') << setw(2) << day;
	}
};

class Time {
protected:
	int hour, minute, second;
public:
	Time(int h,int m,int s) {
		hour = h;
		minute = m;
		second = s;
	}

	

	void print()const {
		cout << setfill('0') << setw(2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second;
	}
};


class Schedule :
	public Date,
	public Time
{
	int id;



public:
	Schedule(int id,int y,int m,int d,int h,int min,int s):
		Date(y,m,d),
		Time(h,min,s)
	{
		this->id = id;
	}

	void print()const {
		cout << "The urgent schedule is No." << id << ": ";
		Date::print();
		cout << " ";
		Time::print();
		cout << endl;
	}


	bool operator < (const Schedule& s) const {
		if (year != s.year)
			return year < s.year;
		if (month != s.month)
			return month < s.month;
		if (day != s.day)
			return day < s.day;
		if (hour != s.hour)
			return hour < s.hour;
		if (minute != s.minute)
			return minute < s.minute;
		if (second != s.second)
			return second < s.second;
	}

	friend bool before(const Schedule& s1,const Schedule& s2) {
		return s1 < s2;
	}
};



int main() {
	string s;
	set<Schedule>ss;
	while (getline(cin, s)) {
		if (s[0] == '0')
			break;
		istringstream fin(s);
		int a, b, c, d, e, f, g;
		fin >> a >> b >> c >> d >> e >> f >> g;
		Schedule sss(a, b, c, d, e, f, g);
		ss.insert(sss);
	}

	(*ss.begin()).print();
	return 0;
}

E. 计算宝宝帐户收益(多重继承)

题目描述

定义一个类CPeople,具有身份号码(id,char[20])和姓名(name,char[10])两个数据成员,从CPeople类中再派生出CInternetUser类和CBankCustomer类,然后再从CInternetUser和CBankCustomer多重继承派生出CInternetBankCustomer类。

CInternetUser类有登录密码(password,char[20])属性和注册register(设置id, name和password),登录login(判断输入的id与password是否与对象注册的相同)成员函数。

CBankCustomer类有余额(balance,double)属性和开户openAccount(设置客户姓名和id),存款deposit,取款withdraw以及缺省的构造函数。

CInternetBankCustomer类包括有余额, 前一日余额, 当日收益,今日万元收益和上一日万元收益等5个数据成员,成员函数有缺省构造函数,存款和取款,设置万元收益,计算当日收益,登陆login(判断输入的id和密码是否与互联网用户的相同,同时从CBankCustomer继承的用户姓名和id要与从CInternetUser继承的相同)。CInternetBankCustomer类对象当日存款不计算收益,第2天开始才能计算收益,当日取款部分无收益。

可参照如下所示的主函数来测试并设计输入数据:

void main()
{
int t, no_of_days, i;
char i_xm[20], i_id[20], i_mm[20], b_xm[20], b_id[20], ib_id[20], ib_mm[20];
double money, interest;
char op_code;

//输入测试案例数t
cin >> t;
while (t–)
{
//输入互联网用户注册时的用户名,id,登陆密码
cin >> i_xm >> i_id >> i_mm;

//输入银行开户用户名,id
cin >> b_xm >> b_id;

//输入互联网用户登陆时的id,登陆密码
cin >> ib_id >> ib_mm;

CInternetBankCustomer ib_user;

ib_user.registerUser(i_xm, i_id, i_mm);
ib_user.openAccount(b_xm, b_id);

if (ib_user.login(ib_id, ib_mm) == 0) //互联网用户登陆,若id与密码不符;以及银行开户姓名和id与互联网开户姓名和id不同
{
cout << “Password or ID incorrect” << endl;
continue;
}

//输入天数
cin >> no_of_days;
for (i=0; i < no_of_days; i++)
{
//输入操作代码, 金额, 当日万元收益
cin >> op_code >> money >> interest;
switch (op_code)
{
case ‘S’: //从银行向互联网金融帐户存入
case ‘s’:
if (ib_user.deposit(money) == 0)
{
cout << “Bank balance not enough” << endl;
continue;
}
break;
case ‘T’: //从互联网金融转入银行帐户
case ‘t’:
if (ib_user.withdraw(money) == 0)
{
cout << “Internet bank balance not enough” << endl;
continue;
}
break;
case ‘D’: //直接向银行帐户存款
case ‘d’:
ib_user.CBankCustomer::deposit(money);
break;
case ‘W’: //直接从银行帐户取款
case ‘w’:
if (ib_user.CBankCustomer::withdraw(money) == 0)
{
cout << “Bank balance not enough” << endl;
continue;
}
break;
default:
cout << “Illegal input” << endl;
continue;
}
ib_user.setInterest(interest);
ib_user.calculateProfit();
//输出用户名,id
//输出银行余额
//输出互联网金融账户余额
ib_user.print();
}
}
}

输入

输入用户例数

输入第1个互联网用户注册时的用户名,id,登陆密码

输入第1个用户银行开户用户名,id

输入第1个互联网用户登陆时的id,登陆密码

输入第1个用户操作天数

循环输入操作代码(S,T,D,W) 金额 当日万元收益

输出

输出第1个用户名,id
输出第1个用户银行余额
输出第1个互联网金融账户余额

输入样例1

2
zhangsan 1234567890 222222
zhangsan 1234567890
1234567890 222222
4
D 15000 0
s 8000 1.5
T 3000 1.55
w 2000 0
lisi 2014150000 abcdef
lisi 2014150000
2014150000 123456

输出样例1

Name: zhangsan ID: 1234567890
Bank balance: 15000
Internet bank balance: 0

Name: zhangsan ID: 1234567890
Bank balance: 7000
Internet bank balance: 8000

Name: zhangsan ID: 1234567890
Bank balance: 10000
Internet bank balance: 5001.2

Name: zhangsan ID: 1234567890
Bank balance: 8000
Internet bank balance: 5001.98

Password or ID incorrect

AC代码

#include
using namespace std;

class CPeople {
protected:
	string id, name;
public:
	CPeople() {}
	CPeople(string id, string name) :
		id(id),
		name(name)
	{}

};


class CInternetUser :
	virtual public CPeople
{
protected:
	string password;
public:
	CInternetUser() {}
	CInternetUser(string name, string id, string password) :
		CPeople(id, name),
		password(password)
	{}

	bool login(string id, string passward) {
		return this->id == id && this->password == passward;
	}

	void registerUser(string name, string id, string password) {
		this->name = name;
		this->id = id;
		this->password = password;
	}


};

class CBankCustomer :
	virtual public CPeople
{
protected:
	double balance;
public:
	CBankCustomer() {
		balance = 0;
	}
	CBankCustomer(string name, string id, double balance) :
		CPeople(name, id),
		balance(balance)
	{}

	void openAccount(string name, string id) {
		this->name = name;
		this->id = id;
	}


	void deposit(double v) {
		balance += v;
	}

	bool withdraw(double v) {
		//这里需要判断?
		if (v > balance)
			return false;
		balance -= v;
		return true;
	}

};


class CInternetBankCustomer :
	public CInternetUser,
	public CBankCustomer
{
	double _balance;
	double yesBalance;//前一日余额
	double profit;//当日收益
	double profitW;//今日万元收益
	double yesprofitW;//上一日万元收益
	double vain;
public:
	CInternetBankCustomer() {
		vain = profit = yesBalance = _balance = 0;
	}

	//设置万元收益
	void setInterest(double interest) {
		profitW = interest / 10000.0;
	}

	void calculateProfit() {
		profit = yesBalance * yesprofitW;
		_balance += profit;
	}


	void print() {
		cout << "Name: " << name << " ID: " << id << endl;
		cout << "Bank balance: " << balance << endl;
		cout << "Internet bank balance: " << _balance << endl;
		cout << endl;
		yesBalance = _balance;
		yesprofitW = profitW;
	}

	// 银行 -> 互联网
	bool deposit(double v) {
		if (v > balance)
			return false;
		balance -= v;
		_balance += v;
		return true;
	}

	//互联网 -> 银行
	bool withdraw(double v) {
		if (v > _balance)
			return false;
		_balance -= v;
		balance += v;
		return true;
	}


};

int main()
{
	int t, no_of_days, i;
	string i_xm, i_id, i_mm, b_xm, b_id, ib_id, ib_mm;
	double money, interest;
	char op_code;

	//输入测试案例数t
	cin >> t;
	while (t--)
	{
		//输入互联网用户注册时的用户名,id,登陆密码
		cin >> i_xm >> i_id >> i_mm;

		//输入银行开户用户名,id
		cin >> b_xm >> b_id;

		//输入互联网用户登陆时的id,登陆密码
		cin >> ib_id >> ib_mm;

		CInternetBankCustomer ib_user;

		ib_user.registerUser(i_xm, i_id, i_mm);
		ib_user.openAccount(b_xm, b_id);

		if (ib_user.login(ib_id, ib_mm) == 0)  //互联网用户登陆,若id与密码不符;以及银行开户姓名和id与互联网开户姓名和id不同
		{
			cout << "Password or ID incorrect" << endl;
			continue;
		}

		//输入天数
		cin >> no_of_days;
		for (i = 0; i < no_of_days; i++)
		{
			//输入操作代码, 金额, 当日万元收益
			cin >> op_code >> money >> interest;
			switch (op_code)
			{
			case 'S':  //从银行向互联网金融帐户存入
			case 's':
				if (ib_user.deposit(money) == 0)
				{
					cout << "Bank balance not enough" << endl;
					continue;
				}
				break;
			case 'T':  //从互联网金融转入银行帐户
			case 't':
				if (ib_user.withdraw(money) == 0)
				{
					cout << "Internet bank balance not enough" << endl;
					continue;
				}
				break;
			case 'D':  //直接向银行帐户存款
			case 'd':
				ib_user.CBankCustomer::deposit(money);
				break;
			case 'W':  //直接从银行帐户取款
			case 'w':
				if (ib_user.CBankCustomer::withdraw(money) == 0)
				{
					cout << "Bank balance not enough" << endl;
					continue;
				}
				break;
			default:
				cout << "Illegal input" << endl;
				continue;
			}
			ib_user.setInterest(interest);
			ib_user.calculateProfit();
			//输出用户名,id
			//输出银行余额
			//输出互联网金融账户余额
			ib_user.print();
		}
	}
}

你可能感兴趣的:(C++程序设计,深圳大学,c++,算法,数据结构,OJ,开发语言)