7-19 车辆选择(继承) (40分)

13 篇文章 0 订阅
9 篇文章 0 订阅

 

有一个汽车类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>
#include <string>
using namespace std;
class vehicle {
protected:
    int id;
    int wheels;
    int weight;
public:
    int get_id() { return id; }
    int get_wheels() {
        return wheels;
    }
    int get_weight() {
        return weight;
    }
    double wheel_load() {
        return weight / 4.0;
    }
    void print() {
        cout << "weight "<< weight<<" Kg and wheels "<<wheels << endl;
    }
    vehicle(int id, int wh = 0, double weit = 0) { this->id = id; wheels = wh; weight = weit; }
};
class car :public vehicle {
private:
    int passenger_load;
public:
    car(int id,int wh, double wei, int ps) :vehicle(id,wh, wei) { passenger_load = ps; }
    int get_passengers() {
        return passenger_load;
    }
    void print() {
        cout <<"passenger_load "<< passenger_load << " weight " << weight<< " Kg and wheels " <<wheels<<endl;
    }
};
class truck :public vehicle {
private:
    int passenger_load;
    double payload;
public:
    truck(int id,int wh, double wei, int pl, double _payload) :vehicle(id,wh, wei) {
        passenger_load = pl; payload = _payload;
    }
    int get_passengers() {
        return passenger_load;
    }
    double efficiency() {
        return payload / (payload + weight);
    }
    void print() {
        cout <<"passenger_load "<<passenger_load<<" weight "<<weight<<" Kg wheels "
             <<wheels <<" and efficiency "<<fixed<<setprecision(2)<<efficiency() << endl;
    }
};
int main()
{
    string name;
    int id, wheel,ps,weight;
    double pl;
    int i = 1;
    string no;
    while (cin >> name,name != "-1") {
        if (i > 3) { no = to_string(i) + "th"; }
        else if (i == 1)no = "1st";
        else if (i == 2)no = "2nd";
        else if (i == 3)no = "3rd";
        if (name=="vehicle") {
            cin >> id >> wheel >> ps;
            vehicle v(id, wheel, ps);
            cout << "The "<<no<<" object is Vehicle No. " << v.get_id() << ": ";
            v.print();
            i++;
        }
        else if (name=="car") {
            cin >> id >> wheel >> weight >> ps;
            car c(id, wheel, weight, ps);
            cout << "The " << no << " object is Car No. " << c.get_id() << ": ";
            c.print();
            i++;
        }
        else if (name == "truck") {
            cin >> id >> wheel >> weight >> ps>>pl;
            truck t(id, wheel, weight, ps, pl);
            cout << "The " << no << " object is Truck No. " << t.get_id() << ": ";
            t.print();
            i++;
        }
    }
    return 0;
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值