有一个汽车类vehicle,它具有一个需传递参数的构造函数,汽车类vehicle中的数据成员为: 车轮个数wheels和车重weight放在保护段中,汽车类vehicle中的公有成员函数为:get_wheels()(返回车轮个数的值)、get_weight()(返回车重的值)、wheel_load()(返回每个轮胎的载重量的值:weight/wheels)、print()(输出车轮的个数和车重的公斤数);
小车类car是vehicle类的派生类,它具有一个需传递参数的构造函数,小车类car中的私有数据成员为:车载人数passenger_load,小车类car中的公有成员函数为:get_passengers()(返回车载人数的值)、print()(输出小车车轮的个数和车重的公斤数以及车载人数的个数);
卡车类truck是vehicle类的派生类,它具有一个需传递参数的构造函数,卡车类truck中的私有数据成员为:载人数passenger_load和载重量payload,卡车类truck中的公有成员函数为:get_passengers()(返回车载人数的值)、efficiency()(返回卡车的载重效率的值:payload/(payload+weight)、print()(输出卡车车轮的个数和车重的公斤数以及车载人数的个数和卡车的载重效率的值))。
生成上述类并编写主函数,根据输入的车辆基本信息,建立车辆对象,并能计算输出该车辆的基本信息。 输入格式:测试输入包含一个测试用例,每一行给出一个车辆的基本信息,每行的第一个字符处为当前车辆的类型,第二个数字为当前车辆的编号,若车辆为vehicle,后面跟随两个数字分别为wheels和weight,若车辆为car,后面跟随三个数字分别为wheels,weight和车载人数,若车辆为truck,后面跟随四个数字分别是wheels,weight、车载人数和载重量。(以上数字均为整型)。-1表示输入结束,相应结果不要输出。请注意输出格式,按照输入顺序进行编号 说明:本题中轮胎载重量、载重效率若需输出保留小数点后两位。
输入样例:
vehicle 101 4 1900
car 201 4 2000 5
truck 301 6 3000 2 9000
car 202 4 1800 4
-1
输出样例:
The 1st object is Vehicle No. 101: weight 1900 Kg and wheels 4
The 2nd object is Car No. 201: passenger_load 5 weight 2000 Kg and wheels 4
The 3rd object is Truck No. 301: passenger_load 2 weight 3000 Kg wheels 6 and efficiency 0.75
The 4th object is Car No. 202: passenger_load 4 weight 1800 Kg and wheels 4
下面给出我的代码,欢迎斧正
#include<iostream>
#include<iomanip>
using namespace std;
class vehicle
{
protected:
int wheels;
float weight;
public:
vehicle(float wh,float we):wheels(wh),weight(we){};
int get_wheels(){return wheels;}
float get_weight(){return weight;}
float wheel_load(){return weight/wheels;}
void print()
{
cout<<"weight "<<weight<<" Kg and wheels "<<wheels<<endl;
}
};
class car:protected vehicle
{
private:
int passenger_load;
public:
car(float wh,float we,int pa):vehicle(wh,we),passenger_load(pa){};
int get_passenger(){return passenger_load;}
void print()
{
cout<<"passenger_load "<<passenger_load<<" weight "
<<weight<<" Kg and wheels "<<wheels<<endl;
}
};
class truck:protected vehicle
{
private:
int passenger_load;
int payload;
public:
truck(float wh,float we,int pa,int load):vehicle(wh,we),passenger_load(pa),payload(load){};
int get_passengers(){return passenger_load;}
float efficiency(){return payload/(payload+weight);}
void print()
{
cout<<"passenger_load "<<passenger_load<<" weight "<<weight
<<" Kg wheels "<<wheels<<" and efficiency "<<setiosflags(ios::fixed)
<<setprecision(2)<<efficiency()<<setprecision(0)<<endl;
}
};
int main()
{
string type;
int ID,wheel,weight,passenger_load,payload,i=1;
cin>>type;
while(type!="-1")
{
cin>>ID>>wheel>>weight;
cout<<"The "<<i<<"";//无奈之举
if(i==1)
cout<<"st";
else if(i==2)
cout<<"nd";
else if(i==3)
cout<<"rd";
else
cout<<"th";
i++;
cout<<" object is ";
if(type=="vehicle")
{
cout<<"Vehicle"<<" No. "<<ID<<": ";
vehicle t(wheel,weight);
t.print();
}
if(type=="car")
{
cin>>passenger_load;
cout<<"Car"<<" No. "<<ID<<": ";
car t(wheel,weight,passenger_load);
t.print();
}
if(type=="truck")
{
cin>>passenger_load>>payload;
cout<<"Truck"<<" No. "<<ID<<": ";
truck t(wheel,weight,passenger_load,payload);
t.print();
}
cin>>type;
}
return 0;
}