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

题目描述

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

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

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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<cstring>
using namespace std;

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

class CStudent : virtual public CPeople
{
protected:
string number;
double grade;
public:
CStudent(){}
CStudent(CPeople p,string n,double g):CPeople(p),number(n),grade(g){}
void printstudent()
{
cout << "No.: " << number << endl;
cout << "Score: " << grade << endl;
}
void print()
{
cout << "Student:" << endl;
printpeople();
printstudent();
cout << endl;
}
};

class CTeacher : virtual public CPeople
{
protected:
string position;
string department;
public:
CTeacher(){}
CTeacher(CPeople p,string po,string de):CPeople(p),position(po),department(de){}
void printteacher()
{
cout << "Position: " << position << endl;
cout << "Department: " << department << endl;
}
void print()
{
cout << "Teacher:" << endl;
printpeople();
printteacher();
cout << endl;
}
};

class CGradOnWork :public CStudent, public CTeacher
{
protected:
string direction;
string tutor;
public:
CGradOnWork(){}
CGradOnWork(CPeople p, CStudent s, CTeacher t, string di, string tu) :CPeople(p), CStudent(s), CTeacher(t),direction(di),tutor(tu){}
void print()
{
cout << "GradOnWork:" << endl;
printpeople();
printstudent();
printteacher();
cout << "Direction: " << direction << endl;
cout << "Tutor: " << tutor << endl;
}

};

int main()
{
string name;
int age;
char sex;
string number;
double grade;
string position, department, direction, tutor;

cin >> name >> sex >>age>>number>>grade>>position>>department>>direction>>tutor;
CPeople p(name, sex, age);
p.print();
CStudent s(p, number, grade);
s.print();
CTeacher t(p, position, department);
t.print();
CGradOnWork g(p, s, t, direction, tutor);
g.print();

return 0;
}

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

题目描述

某旅游网站(假设旅程网)和某银行推出旅游综合服务联名卡—旅程信用卡,兼具旅程会员卡和银行信用卡功能。

旅程会员卡,有会员卡号(int)、旅程积分(int),通过会员卡下订单,按订单金额累计旅程积分。

信用卡,有卡号(int)、姓名(string)、额度(int)、账单金额(float)、信用卡积分(int)。------请注意数据类型

信用卡消费金额m,若超额度,则不做操作;否则,账单金额+m,额度-m,信用卡积分按消费金额累加。

信用卡退款m,账单金额-m,信用卡积分减去退款金额。

通过旅程信用卡在旅程网下单,旅程积分和信用卡积分双重积分(即旅程积分和信用卡积分同时增加)。

旅程信用卡可以按      旅程积分:信用卡积分= 1:2    的比例将信用卡积分兑换为旅程积分。

初始假设信用卡积分、旅程积分、账单金额为0。

根据上述内容,定义旅程会员卡类、信用卡类,从两者派生出旅程信用卡类并定义三个类的构造函数和其它所需函数。

生成旅程信用卡对象,输入卡信息,调用对象成员函数完成旅程网下单、信用卡刷卡、信用卡退款、信用卡积分兑换为旅程积分等操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<string>
using namespace std;

class Vip
{
protected:
int Vnumber;
static int Vpoints;
public:
Vip(int n1, int p1)
{
Vnumber = n1;
Vpoints = p1;
}
int add(float a)
{
Vpoints += a;
return Vpoints;
}
};

int Vip::Vpoints = 0;

class Credit
{
protected:
int Cnumber;
string name;
int limit;
float amount;
static int Cpoints;
public:
Credit(int n2, string n, int l, float a, int p2)
{
Cnumber = n2;
name = n;
limit = l;
amount = a;
Cpoints = p2;
}
void consume(float a)
{
if (a + amount <= limit)
{
amount += a;
Cpoints +=(int) a;
}
}
void refund(float a)
{
amount -= a;
Cpoints -= (int)a;
}
int add(float a)
{
Cpoints += (int)a;
return Cpoints;
}

};

int Credit::Cpoints = 0;

class Journey :public Vip, public Credit
{
public:
Journey(int vm,int cm,string n,int s,float a,int vp,int cp):Credit(cm,n,s,a,cp),Vip(vm,vp){}
void order(float a)
{
int p1, p2, p3;
p1 = Vip::add(a);
p2 = Credit::add(a);
amount += a;
}
void change(float a)
{
int p1, p2, p3;
p2 = a / 2;
Vpoints += p2;
Cpoints -= (int)a;
}
void show()
{
cout << Vnumber << " " << Vpoints << endl;
cout << Cnumber << " " << name << " " << amount << " " << Cpoints << endl;
}
};

int main()
{
int vm, cm;
string name;
float a;
int limit;
char ch;
int t;
cin >> vm >> cm >> name >> limit;
Journey j(vm, cm, name, limit, 0, 0, 0);
cin >> t;
while (t--)
{
cin >> ch >> a;
if (ch == 'o')
{
j.order(a);
}
else if (ch == 'c')
{
j.consume(a);
}
else if (ch == 'q')
{
j.refund(a);
}
else if (ch == 't')
{
j.change(a);
}
}
j.show();

return 0;
}

C.图形面积(虚函数与多态)

题目描述

编写一个程序,定义抽象基类Shape,在Shape类中定义虚函数area();由它派生出3个派生类:Circle(圆形)、Square(正方形)、Rectangle(矩形)。用虚函数分别计算几种图形面积。

1、要求输出结果保留两位小数。

2、要求用基类指针数组,使它每一个元素指向每一个派生类对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class Shape
{
public:
virtual double area()
{
return 0;

}
};

class Circle :public Shape
{
protected:
double r;
public:
Circle(double r1) :r(r1) {}
virtual double area()
{
return r * r * 3.14;
}
};

class Square :public Shape
{
protected:
double d;
public:
Square(double d1) :d(d1) {}
virtual double area()
{
return d * d;
}
};

class Rectangele :public Shape
{
protected:
double a, b;
public:
Rectangele(double a1, double b1) :a(a1), b(b1) {}
virtual double area()
{
return a * b;
}
};

void print(Shape* p)
{
cout << fixed << setprecision(2) << p->area() << endl;
}

int main()
{
int t;
cin >> t;
double r, d, a, b;
Shape* p;
while (t--)
{
cin >> r;
Circle c(r);
p = &c;
print(p);
cin >> d;
Square s(d);
p = &s;
print(p);
cin >> a >> b;
Rectangele r(a, b);
p = &r;
print(p);
}

return 0;
}

D.汽车收费(虚函数和多态)

题目描述

现在要开发一个系统,实现对多种汽车的收费工作。 汽车基类框架如下所示:

1
2
3
4
5
6
7
class Vehicle 
{
protected:
string no; //编号
public:
virtual void display()=0; //应收费用
}

以Vehicle为基类,构建出Car、Truck和Bus三个类。

Car的收费公式为: 载客数*8+重量*2

Truck的收费公式为:重量*5

Bus的收费公式为: 载客数*30

生成上述类并编写主函数,要求主函数中有一个基类指针Vehicle *pv;用来做测试用。

主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
using namespace std;

class Vehicle
{
protected:
string No; //编号
public:
Vehicle(string n) :No(n) {}
virtual void show() = 0; //应收费用
};

class car :public Vehicle
{
protected:
int num, weight;
public:
car(string no,int n,int w):Vehicle(no),num(n),weight(w){}
void show()
{
int sum;
sum = num * 8 + weight * 2;
cout << No << " " << sum << endl;
}
};

class truck :public Vehicle
{
protected:
int weight;
public:
truck(string no,int w):Vehicle(no),weight(w){}
void show()
{
int sum;
sum = weight * 5;
cout << No << " " << sum << endl;
}
};

class bus :public Vehicle
{
protected:
int num;
public:
bus(string no,int n):Vehicle(no),num(n){}
void show()
{
int sum;
sum = num * 30;
cout << No << " " << sum << endl;
}

};

int main()
{
int t,tp;
string no;
int weight, num;

cin >> t;
while (t--)
{
Vehicle* pv;

cin >> tp;
if (tp == 1)
{
cin >> no >> weight >> num;
car c(no, weight, num);
pv = &c;
pv->show();
}
else if (tp == 2)
{
cin >> no >> weight;
truck t(no, weight);
pv = &t;
pv->show();
}
else if (tp == 3)
{
cin >> no >> num;
bus b(no, num);
pv = &b;
pv->show();
}
}

return 0;
}

E.支票账户(虚函数与多态)

题目描述

某银行的支票账户分为两类,一类为基本支票账户BaseAccount,另一类为具有透支保护特性的BasePlus支票账户。

BaseAccount支票账户的信息包括:客户姓名(name)、账户(account)、当前结余(balance);BaseAccount支票账户可以执行的操作包括:存款(deposit)、取款(withdraw)、显示账户信息(display)。注意:取款金额不能透支,否则显示出错信息“insufficient”。

BasePlus支票账户除包含BaseAccount的所有信息外,还包括以下信息:透支上限(默认为5000),当前可透支额度(limitSum);BasePlus支票账户可执行的操作与BaseAccount相同,但有两种操作的实现不同:(1)对于取款操作,可以在透支上限范围内透支,超过则显示出错信息“insufficient”;(2)对于显示操作,必须显示BasePlus的其他信息。

请实现BaseAccount类和BasePlus类,其中BasePlus类继承于BaseAccount类,注意BaseAccount账户名称以BA开头,BasePlus账户名称以BP开头。

要求只使用一个基类指针,指向所建立的对象,然后使用指针调用类中的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstring>
using namespace std;
class BaseAccount {
protected:
string name;
string account;
int balance;
public:
BaseAccount(string n, string a, int b) :balance(b),name(n),account(a){}

void deposit(int d) {
balance += d;
}
virtual void withdraw(int w) {
if (balance - w >= 0)
balance -= w;
else
cout << "insufficient" << endl;
}
virtual void print() {
cout << name << " " << account << " Balance:" << balance << endl;
}

};
class BasePlus :public BaseAccount {
protected:
int limit;
int limit_sum;
public:
BasePlus(string n, string a, int b) :BaseAccount(n, a, b) {
limit = 5000;
limit_sum = 0;
}
virtual void withdraw(int w) {
if (balance - w >= -5000) {
balance -= w;
if (balance < 0) {
limit = 5000 + balance;
limit_sum = balance;
}
}
else
cout << "insufficient" << endl;
}
virtual void print() {
cout << name << " " << account;
if (balance < 0)
cout << " Balance:0";
else
cout << " Balance:" << balance;
cout << " limit:" << limit << endl;
}
};
int main() {
int t, b, d, w;
string n, a;
cin >> t;
BaseAccount* p;
while (t--) {
cin >> n >> a >> b;
if (a[1] == 'A') {
BaseAccount b1(n, a, b);
cin >> d >> w;
p = &b1;
p->deposit(d);
p->withdraw(w);
cin >> d >> w;
p->deposit(d);
p->withdraw(w);
p->print();
}
else {
BasePlus b1(n, a, b);
cin >> d >> w;
p = &b1;
p->deposit(d);
p->withdraw(w);
cin >> d >> w;
p->deposit(d);
p->withdraw(w);
p->print();
}
}
}