7-1 马会飞
已知Horse类是Pegasus类的父类,根据以下主函数完善程序内容,以实现规定的输出。不允许改变主函数的内容。
int main()
{
Horse *p1 = new Horse; //输出:Horse 申请了空间...
Horse *p2 = new Pegasus;
cout << endl;
p1->Fly(); //输出:Just a horse.
p2->Fly(); //输出:I can fly!
cout << endl;
delete p1; //输出:Horse 释放了空间...
delete p2;
return 0;
}
输入格式:
无
输出格式:
根据样例格式输出。
输入样例:
在这里给出一组输入。例如:
无
输出样例:
在这里给出相应的输出。例如:
Horse 申请了空间... Horse 申请了空间... Pegasus 申请了空间... Just a horse. I can fly! Horse 释放了空间... Pegasus 释放了空间... Horse 释放了空间...
#includeusing namespace std; class Horse { public: Horse() { cout<<"Horse 申请了空间..."< Fly(); //输出:Just a horse. p2->Fly(); //输出:I can fly! cout << endl; delete p1; //输出:Horse 释放了空间... delete p2; return 0; }
7-2 动物爱吃什么
1.设计一个基类动物类(animal),包含private数据成员:动物编号int num;和动物名称string name;public成员函数:getnum( )用于获取其编号, getname( )用于获取其名称和一个纯虚函数eat( ),以及构造函数。
2.由animal类派生出狗类Dog和猫类Cat,每个类中均有自己的构造函数,根据输出结果设计这两个类并在主函数中完成设计类的输出测试
3.要求在主函数中必须使用基类指针调用虚函数eat( ).
输入格式:输入两行,每一行分别是动物编号num和动物名称name,中间空格分隔。
输出格式:输出两行,每一行显示动物编号num、动物名称name和爱吃的食物。
输入样例:在这里给出一组输入。例如:
1 金毛 2 波斯输出样例:
在这里给出相应的输出。例如:
1号金毛啃骨头 2号波斯吃小鱼
#includeusing namespace std; class animal { int num; string name; public: animal(int a,string s):num(a),name(s){}; int getnum() { return num; } string getname() { return name; } virtual void eat()=0; }; class Dog:public animal { public: Dog(int a,string s):animal(a,s){}; void eat() { cout< >n1>>na1>>n2>>na2; a = new Dog(n1,na1); b = new Cat(n2,na2); a->eat(); b->eat(); return 0; }
7-3 饮料的加工时间
一个茶吧提供三类饮料:茶、咖啡和牛奶。它们的加工时间有所不同,其中本地茶每份要50秒,其它茶要20秒;现磨咖啡要加120秒,其它咖啡加20秒;牛奶每份10秒。
给出下面的基类框架:
Class Drink
{
protected:
int NO; //编号
int amount; //数量
public:
virtual void display()=0;//输出提供时间
}
以Drink为基类,构建出Tea, Coffee和Milk三个类。
生成上述类,并编写主函数,要求主函数中有一个基类Drink指针数组,数组元素不超过10个。
Drink *pd[10];
主函数根据输入的信息,相应建立Tea, Coffee或Milk类对象,并给出收费。
输入格式:每个测试用例占一行,每行给出饮料的基本信息,第一个为饮料的类型,茶为1,咖啡为2,牛奶为3。接下来是申请的编号(100-999),然后是数量。对于茶叶来说,接下来输入一个地区代码,其中1代表本地。对于咖啡来说,接下来要输入一个加工代码,其中1代表现磨。最后一行为0。
要求输出编号和加工时间。
提示:应用虚函数实现多态
输入样例:
1 106 3 1
1 103 2 2
3 109 1
2 107 2 1
2 232 3 29
0
输出样例
106 150
103 40
109 10
107 240
232 60
#includeusing namespace std; class Drink { protected: int NO; int amount; public: virtual void display()=0; }; class tea:public Drink { int time,bj; public: tea(int n,int a,int b) { NO = n; amount = a; bj = b; } virtual void display() { if(bj == 1) time = 50; else time = 20; cout< >tp; if(tp == 0) break; switch(tp) { case 1: cin>>no>>num>>bj; pd[i] = new tea(no,num,bj); pd[i]->display(); i++; break; case 2: cin>>no>>num>>bj; pd[i] = new coffee(no,num,bj); pd[i]->display(); i++; break; case 3: cin>>no>>num; pd[i] = new milk(no,num); pd[i]->display(); i++; break; } } return 0; }
7-4 汽车收费
现在要开发一个系统,管理对多种汽车的收费工作。
给出下面的一个基类框架
class Vehicle
{
protected:
string NO;//编号
public:
virtual void display()=0;//输出应收费用
}
以Vehicle为基类,构建出Car、Truck和Bus三个类。
Car的收费公式为: 载客数8+重量2
Truck的收费公式为:重量*5
Bus的收费公式为: 载客数*3
生成上述类并编写主函数,要求主函数中有一个基类Vehicle指针数组,数组元素不超过10个。
Vehicle *pv[10];
主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数
输入格式:每个测试用例占一行,每行给出汽车的基本信息,每一个为当前汽车的类型1为car,2为Truck,3为Bus。接下来为它的编号,接下来Car是载客数和重量,Truck给出重量,Bus给出载客数。最后一行为0,表示输入的结束。
要求输出各车的编号和收费。
提示:应用虚函数实现多态
输入样例
1 002 20 5
3 009 30
2 003 50
1 010 17 6
0
输出样例
002 170
009 90
003 250
010 148
#includeusing namespace std; class Vehicle { protected: string NO; public: Vehicle(string n):NO(n){} virtual void display() = 0; }; class Car:public Vehicle { int num,weight,price; public: Car(string n,int nu,int w):Vehicle(n),num(nu),weight(w) { price = num * 8 + weight * 2; } void display() { cout << NO << " " << price << endl; } }; class Truck:public Vehicle { int weight,price; public: Truck(string n,int w):Vehicle(n),weight(w) { price = weight * 5; } void display() { cout << NO << " " << price << endl; } }; class Bus:public Vehicle { int num,price; public: Bus(string n,int nu):Vehicle(n),num(nu) { price = num * 3; } void display() { cout << NO << " " << price << endl; } }; int main() { Vehicle* pv[10]; int w,nu,tp,i = 0; string n; while(1) { cin>>tp; if(tp == 0) break; switch(tp) { case 1: cin >>n>>nu>>w; pv[i] = new Car(n,nu,w); pv[i]->display(); i++; break; case 2: cin >>n>>w; pv[i] = new Truck(n,w); pv[i]->display(); i++; break; case 3: cin >>n>>nu; pv[i] = new Bus(n,nu); pv[i]->display(); i++; break; } } return 0; }
7-5 饮料的价格
一个茶吧提供三类饮料:茶、咖啡和牛奶。其中本地茶要另加50%的服务费,其它茶要加20%的服务费;现磨咖啡要加100%的服务费,其它咖啡加20%的服务费;牛奶不加服务费,服务费精确到小数点一位。
给出下面的基类框架:
Class Drink
{
protected:
int NO; //编号
int amount; //数量
float price; //单价
public:
virtual void display()=0;//输出费用
}
以Drink为基类,构建出Tea, Coffee和Milk三个类。
生成上述类,并编写主函数,要求主函数中有一个基类Drink指针数组,数组元素不超过10个。
Drink *pd[10];
主函数根据输入的信息,相应建立Tea, Coffee或Milk类对象,并给出收费。
输入格式:每个测试用例占一行,每行给出饮料的基本信息,第一个为饮料的类型,茶为1,咖啡为2,牛奶为3。接下来是申请的编号(100-999),然后是数量和单价。对于茶叶来说,接下来输入一个地区代码,其中1代表本地。对于咖啡来说,接下来要输入一个加工代码,其中1代表现磨。最后一行为0。
要求输出编号和收费(小数点后保留1位小数)。
提示:应用虚函数实现多态
输入样例:
1 106 3 33 1
1 103 2 20 2
3 109 1 15
2 107 2 15.8 1
2 232 3 21 29
0
输出样例:
106 148.5
103 48.0
109 15.0
107 63.2
232 75.6
#includeusing namespace std; class Drink { protected: int NO; int amount; float price; public: virtual void display()=0; }; class tea:public Drink { int bj; float pri; public: tea(int n,int a,float pr,int b) { NO = n; amount = a; pri = pr; bj = b; } virtual void display() { if(bj == 1) price = pri*amount*1.5; else price = pri*amount*1.2; cout< >tp; if(tp == 0) break; switch(tp) { case 1: cin>>no>>num>>pr>>bj; pd[i] = new tea(no,num,pr,bj); pd[i]->display(); i++; break; case 2: cin>>no>>num>>pr>>bj; pd[i] = new coffee(no,num,pr,bj); pd[i]->display(); i++; break; case 3: cin>>no>>num>>pr; pd[i] = new milk(no,num,pr); pd[i]->display(); i++; break; } } return 0; }
7-6 宠物的生长(多态)
现在要开发一个系统,对宠物的生长状态进行管理。
给出下面的一个基类框架
class Pet
{
protected:
string name;//姓名
int length;//身长
int weight;//体重
int current;//当前日期
public:
virtual void display(int day)=0;//输出目标日期的身长和体重
Pet(string name, int l, int w, int c);
}
以Pet为基类,构建出Cat和Dog两个类:
Cat一天身长加1,体重加2。
Dog一天身长加2,体重加1。
生成上述类并编写主函数,要求主函数中有一个基类Pet指针数组,数组元素不超过10个。
Pet *pt[10];
主函数根据输入的信息,相应建立Cat类对象或Dog类对象,并给出目标日期宠物的身长和体重。
提示:应用虚函数实现多态
输入格式:每个测试用例占一行,每行给出宠物的基本信息,第一个为当前宠物的类型:1为Cat,2为Dog。接下来为它的名字,随后的两个数字为身长和体重,最后为测身长和体重的日期(不大于10的正整数)。最后一行为目标日期(大于10的正整数)
输出格式:输出目标日期宠物姓名、身长和体重
输入样例:1 Marry 22 12 5 2 Jack 10 9 9 1 Jim 11 8 6 11
#includeusing namespace std; class Pet { protected: string name; int length; int weight; int current; int day; public: virtual void display(int day)=0; Pet() { name = ""; length = 0; weight = 0; current = 0; day = 0; } }; class Cat:public Pet { public: Cat(string na,int l,int w,int c) { name = na; length = l; weight = w; current = c; } void display(int d) { cout< >tp; if(tp > 10) { for(int j = 0;j < i;j++) pt[j]->display(tp); break; } cin>>na>>l>>w>>c; if(tp == 1) { pt[i++]=new Cat(na,l,w,c); } else { pt[i++]=new Dog(na,l,w,c); } } return 0; }
7-7 2017Final进位与借位
凤湖小学二年级的陈老师吃惊地发现班上的同学竟然可以分成三类,一类总是可以正确地完成三位整数加减法(GroupA);一类总是可以正确地完成三位整数的加法,但对于减法运算来说,总是忘记借位的处理(GroupB);剩下的人总是忘记加法的进位,也总是忘记减法的借位(GroupC)。
现在请给出一次陈老师在课堂提问时,同学们会给出的回答。
实现时请基于下面的基类框架
class Group
{
public:
virtual int add(int x, int y)=0;//输出加法的运算结果
virtual int sub(int x, int y)=0;//输出减法的运算结果
}
构建出GroupA, GroupB和GroupC三个派出类:
并编写主函数,要求主函数中有一个基类Group指针数组,通过该数组元素统一地进行add和sub运算。
输入格式:首先输入一个整数n,这是班级的人数, 不超过20。
接下来再输入n个数字(取值为1,2,或3),它是各个学生所属的类别,第一个数字是第一位学生的类别,接下来是第二位学生的类别,..., 最后是第n位学生的类别。类别为1时,表明该学生是第A类;为2时,表明该生是B类,为3时表明是C类。
接下来每一行输入一个数学问题。数学问题由两部分构成,第一部分被提问学生的编号,它是一个不超过n的正整数,1表示第一位学生,2表示第二位学生,n表示第n位学生; 第二部分是具体的数学题,可能是加法,也可能是减法。注意:运算对象和加减号之间没有空格,两个运算对象均是不超过999的非负整数, 减法时,被减数不小于减数。
如果某一行以0开头,则说明提问结束。
输出格式:输出指定学生对于给定的问题的回答。
输入样例:4 1 2 3 3 1 79+81 2 81-79 4 183+69 0输出样例:
160 12 142
#includeusing namespace std; class Group { public: virtual int add(int x, int y)=0;//输出加法的运算结果 virtual int sub(int x, int y)=0;//输出减法的运算结果 }; class GroupA:public Group { public: int add(int x, int y) { return x+y; } int sub(int x, int y) { return x-y; } }; class GroupB:public Group { public: int add(int x, int y) { return x+y; } int sub(int x, int y) { int g = 0,s = 0,b = 0,g1,g2,s1,s2,b1,b2; b1 = x/100,b2 = y/100; s1 = x/10%10,s2 = y/10%10; g1 = x%10,g2 = y%10; s = s1-s2; g = g1-g2; if(b1) b = b1-b2; if(s < 0) s += 10; if(g < 0) g += 10; return g + s*10 + b*100; } }; class GroupC:public Group { public: int add(int x, int y) { int g,s,b,g1,g2,s1,s2,b1,b2,q; b1 = x/100,b2 = y/100; s1 = x/10%10,s2 = y/10%10; g1 = x%10,g2 = y%10; g = (g1 + g2)%10; s = (s1+s2)%10; b = (b1+b2)%10; return g + s*10 + b*100; } int sub(int x, int y) { int g = 0,s = 0,b = 0,g1,g2,s1,s2,b1,b2; b1 = x/100,b2 = y/100; s1 = x/10%10,s2 = y/10%10; g1 = x%10,g2 = y%10; s = s1-s2; g = g1-g2; if(b1) b = b1-b2; if(s < 0) s += 10; if(g < 0) g += 10; return g + s*10 + b*100; } }; int main() { int i,n,stu[20] = {0},no,x,y; cin>>n; for(i = 0;i < n;i++) cin>>stu[i]; char op; while(cin>>no && no != 0) { cin>>x>>op>>y; if(stu[no-1] == 1) { GroupA a; if(op == '+') cout<



