车辆选择(继承)

有一个汽车类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;
//vehicle
class vehicle{
protected:
    float wheels;
    float weight;
public:
    vehicle(float a,float b):wheels(a),weight(b){};
    float getWheels(){return wheels;}
    float getWheight(){return weight;}
    float wheelLoad(){return weight/wheels;}
    void print(){cout<<"weight "<<weight<<" Kg and wheels "<<wheels<<endl;}
};
//car
class car : public vehicle{
protected:
    int passenger_load;
public:
    car(float a,float b,int c) : vehicle(a,b),passenger_load(c){};
    int getPassenger(){return passenger_load;}
    void print(){cout<<"passenger_load "<<passenger_load<<" weight "<<weight<<" Kg and wheels "<<wheels<<endl;}
};
//truck
class truck : public car{
protected:
    int payLoad;
public:
    truck(float a,float b,int c,int d) : car(a,b,c) , payLoad(d){};
    float efficiency(){return payLoad/(payLoad+weight);}
    void print();
};
void truck::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 num, wheels;
    float weight;
    int i=1;
    while(cin>>type)
    {
        if(type=="-1")break;
        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")
        {
            cin>>num>>wheels>>weight;
            cout<<"Vehicle"<<" No. "<<num<<": ";
            vehicle hello(wheels,weight);
            hello.print();
        }else if(type=="car")
        {
            int pass;
            cin>>num>>wheels>>weight>>pass;
            cout<<"Car"<<" No. "<<num<<": ";
            car hello(wheels,weight,pass);
            hello.print() ;
        }else
        {
            int pass, load;
            cin>>num>>wheels>>weight>>pass>>load;
            cout<<"Truck"<<" No. "<<num<<": ";
            truck hello(wheels,weight,pass,load);
            hello.print();
        }
    }
    return 0;
}

欢迎指教,一起学习!

未经本人允许,请勿转载!

谢谢!

个人分享,欢迎指导,未经允许,请勿转载。谢谢!
原文地址:https://www.cnblogs.com/hello-OK/p/6993047.html