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: 23Student:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5Teacher:
Name: wang-li
Sex: m
Age: 23
Position: assistant
Department: computerGradOnWork:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5
Position: assistant
Department: computer
Direction: robot
Tutor: zhao-jun
#include<iostream> using namespace std; class Cpeople { protected:string name,sex,age; public:Cpeople(string n,string s,string a):name(n),sex(s),age(a){}void print(){cout << "People:" << endl;cout << "Name: " << name << endl << "Sex: " << sex << endl << "Age: " << age << endl;} }; class cstudent :virtual public Cpeople { protected:string no, score; public:cstudent(string n, string s, string a,string noo,string sc):Cpeople(n,s,a),no(noo),score(sc){}void print(){cout << "Student:" << endl;cout << "Name: " << name << endl << "Sex: " << sex << endl << "Age: " << age << endl;cout << "No.: " << no << endl << "Score: " << score << endl;}}; class cteacher :virtual public Cpeople { protected:string job, Department; public:cteacher(string n, string s, string a, string j, string D) :Cpeople(n, s, a), job(j), Department(D){}void print(){cout << "Teacher:" << endl;cout << "Name: " << name << endl << "Sex: " << sex << endl << "Age: " << age << endl;cout << "Position: " << job << endl << "Department: " << Department << endl;}}; class CGradOnWork:public cteacher,public cstudent { protected:string direction, tutor; public:CGradOnWork(string n, string s, string a, string noo, string sc, string j, string D,string d,string t) :Cpeople(n, s, a), cstudent(n, s, a, noo, sc), cteacher(n, s, a, j, D),direction(d),tutor(t){}void print(){cout << "GradOnWork:" << endl;cout << "Name: " << name << endl << "Sex: " << sex << endl << "Age: " << age << endl;cout << "No.: " << no << endl << "Score: " << score << endl;cout<< "Position: " << job << endl << "Department: " << Department << endl;cout << "Direction: " << direction << endl << "Tutor: " << tutor;}}; int main() {string name,sex,age,no,score,job, Department,direction, tutor;cin >> name>>sex>>age;cin >> no >> score;cin >> job >> Department;cin >> direction >> tutor;Cpeople p1(name, sex, age);cstudent s1(name, sex, age, no, score);cteacher t1(name, sex, age, job, Department);CGradOnWork g1(name, sex, age, no, score, job, Department,direction,tutor);p1.print(); s1.print(); t1.print(); g1.print(); }
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函数产生输出。
输入
第一行:最高速度 速度 重量 第二行:高度 第三行:座位数输入样例1
100 60 20
28
2输出样例1
Vehicle:
max_speed:100
speed:60
weight:20Bicycle:
max_speed:100
speed:60
weight:20
height:28Motocar:
max_speed:100
speed:60
weight:20
seat_num:2Motocycle:
max_speed:100
speed:60
weight:20
height:28
seat_num:2
#include<iostream> using namespace std;class vehicle { protected:int max, speed, weight; public:vehicle(int m, int s, int w) :max(m), speed(s), weight(w) {}void display(){cout << "Vehicle:" << endl;cout << "max_speed:" << max << endl << "speed:" << speed << endl << "weight:" << weight << endl;} };class bicycle :virtual public vehicle { protected:int height; public:bicycle(int m, int s, int w, int h) :vehicle(m, s, w), height(h) {}void display(){cout << "Bicycle:" << endl;cout << "max_speed:" << max << endl << "speed:" << speed << endl << "weight:" << weight << endl << "height:" << height << endl;} };class motocar :virtual public vehicle { protected:int seatnum; public:motocar(int m, int s, int w, int seat) :vehicle(m, s, w), seatnum(seat){}void display(){cout << "Motocar:" << endl;cout << "max_speed:" << max << endl << "speed:" << speed << endl << "weight:" << weight << endl << "seat_num:" << seatnum << endl;} };class motocycle :public bicycle, public motocar { public:motocycle(int m, int s, int w, int h, int seat) :vehicle(m, s, w), bicycle(m, s, w, h), motocar(m, s, w, seat){}void display(){cout << "Motocycle:" << endl;cout << "max_speed:" << max << endl << "speed:" << speed << endl << "weight:" << weight << endl << "height:" << height << endl << "seat_num:" << seatnum << endl;} };int main() {int m, s, w, h, seat;cin >> m >> s >> w >> h >> seat;vehicle v(m, s, w);bicycle b(m, s, w, h);motocar car(m, s, w, seat);motocycle moto(m, s, w, h, seat);v.display(); cout << endl;b.display(); cout << endl;car.display(); cout << endl;moto.display(); }
C. 商旅信用卡(多重继承)
旅程会员卡,有会员卡号(int)、旅程积分(int),通过会员卡下订单,按订单金额累计旅程积分。
信用卡,有卡号(int)、姓名(string)、额度(int)、账单金额(float)、信用卡积分(int)。------请注意数据类型
信用卡退款m,账单金额-m,信用卡积分减去退款金额。
初始假设信用卡积分、旅程积分、账单金额为0。
生成旅程信用卡对象,输入卡信息,调用对象成员函数完成旅程网下单、信用卡刷卡、信用卡退款、信用卡积分兑换为旅程积分等操作。
根据上述内容,定义旅程会员卡类、信用卡类,从两者派生出旅程信用卡类并定义三个类的构造函数和其它所需函数。
通过旅程信用卡在旅程网下单,旅程积分和信用卡积分双重积分(即旅程积分和信用卡积分同时增加)。
旅程信用卡可以按 旅程积分:信用卡积分= 1:2 的比例将信用卡积分兑换为旅程积分。
信用卡消费金额m,若加已有账单金额超额度,则不做操作;否则,账单金额+m,信用卡积分按消费金额累加。
某旅游网站(假设旅程网)和某银行推出旅游综合服务联名卡—旅程信用卡,兼具旅程会员卡和银行信用卡功能。
输入
第一行:输入旅程会员卡号 信用卡号 姓名 额度第二行:测试次数n
第三行到第n+2行,每行:操作 金额或积分
o m(旅程网下订单,订单金额m)
c m(信用卡消费,消费金额m)
q m (信用卡退款,退款金额m)
t m(积分兑换,m信用卡积分兑换为旅程积分)
输出
输出所有操作后旅程信用卡的信息:旅程号 旅程积分
信用卡号 姓名 账单金额 信用卡积分
1000 2002 lili 3000
4
o 212.5
c 300
q 117.4
t 2001000 312
2002 lili 395.1 195#include<iostream> using namespace std;class vip { protected:int num;//会员卡号int points;//旅程积分 public:vip(int n) :num(n), points(0){} };class credit { protected:int num2, limit;int points2;//信用卡积分string name;float bill;//账单金额 public:credit(int n, string na, int l) :num2(n), name(na), limit(l), points2(0),bill(0){} };class vipcredit :public vip, public credit { public:vipcredit(int n, int n2, string s, int l) :vip(n), credit(n2, s, l) {}void refund(float m)//退款{if (m <= bill){bill -= m; points2 -= (int)m;}}void order(float m)//下单{points += (int)m; //旅程积分和信用卡积分同时增加points2 += (int)m; bill += m;}void consume(float m)//信用卡消费{if (m + bill <= limit){bill += m;points2 += (int)m;}}void exchange(float m)//兑换{if (m <= points2){points2 -= (int)m;points += (int)m / 2;}}void display(){cout << num << " " << points << endl;cout << num2 << " " << name << " " << bill << " " << points2 << endl;} };int main() {int num, num2, limit, n;string name, command;cin >> num >> num2 >> name >> limit >> n;vipcredit v(num, num2, name, limit);while (n--){float m;cin >> command >> m;if (command == "o"){v.order(m);}if (command == "c"){v.consume(m);}if (command == "q"){v.refund(m);}if (command == "t"){v.exchange(m);}}v.display(); }
本文发布于:2024-02-01 00:42:58,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671937832589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |