题目描述
现在要开发一个系统,实现对多种汽车的收费工作。 汽车基类框架如下所示:
class Vehicle
{ protected:
string no;//编号
public:
virtual void display()=0;//应收费用
}
以Vehicle为基类,构建出Car、Truck和Bus三个类。
Car的收费公式为: 载客数8+重量2
Truck的收费公式为:重量*5
Bus的收费公式为: 载客数*3
生成上述类并编写主函数,要求主函数中有一个基类指针Vehicle *pv;用来做测试用。
主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数。
输入
第一行表示测试次数。从第二行开始,每个测试用例占一行,每行数据意义如下:汽车类型(1为car,2为Truck,3为Bus)、编号、基本信息(Car是载客数和重量,Truck给出重量,Bus给出载客数)。
输出
车的编号、应缴费用
输入样例
4
1 002 20 5
3 009 30
2 003 50
1 010 17 6
输出样例
002 170
009 90
003 250
010 148
代码
#include<iostream>
#include<string>
using namespace std;
class Vehicle{
protected:
string no;
public:
Vehicle(string n):no(n){}
virtual void display()=0;
};
class Car:public Vehicle{
private:
int num,weight;
public:
Car(string n,int Num,int w):Vehicle(n),num(Num),weight(w){}
virtual void display(){
cout<<no<<" "<<num*8+weight*2<<endl;
}
};
class Truck:public Vehicle{
private:
int weight;
public:
Truck(string n,int w):Vehicle(n),weight(w){}
virtual void display(){
cout<<no<<" "<<weight*5<<endl;
}
};
class Bus:public Vehicle{
private:
int num;
public:
Bus(string n,int Num):Vehicle(n),num(Num){}
virtual void display(){
cout<<no<<" "<<num*3<<endl;
}
};
int main()
{
int t;
cin>>t;
string n;
int Num,w,kind;
Vehicle *pv;
while(t--)
{
cin>>kind>>n;
if(kind==1)
{
cin>>Num>>w;
pv = new Car(n,Num,w);
}
if(kind==2)
{
cin>>w;
pv = new Truck(n,w);
}
if(kind==3)
{
cin>>Num;
pv = new Bus(n,Num);
}
pv->display();
}
return 0;
}