汽车收费 C++ PTA

7-1 汽车收费(10 分)

现在要开发一个系统,管理对多种汽车的收费工作。 给出下面的一个基类框架

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

 

#include<iostream>  
#include <string>  
using namespace std;  

/********************************/
/* 李娜娜!!!我爱你!!!        */
/*                ——静静         */ 
/********************************/

class Vehicle           // 父类、基类  Vehicle
{  
protected:              // 安全属性 
    string NO;          // 编号  
public:                  // 公共属性 
    Vehicle(string n) // 获取编号 
    {
        NO = n;  
    }  
    virtual int money()=0;//计算应收费用  
};  
  
class Car:Vehicle        // 子类Car,继承父类Verhicle 
{  
    public:  
        int guest,weight;  
        Car(string no1,int guest1,int weight1):Vehicle(no1)  
        {   
            weight=weight1;  
            guest=guest1;  
        }  
        int money()  
        {     
            return guest*8+weight*2;  //  载客数8+重量2 
        }  
};   

class Truck:Vehicle  // 子类Truck,继承父类Verhicle  
{  
        public:  
        int weight;  
        Truck(string no1,int weight1):Vehicle(no1)  
        {   
            weight=weight1;  
        }  
        int money()  
        {     
            return weight*5;  // 重量*5
        }  
};  

class Bus:Vehicle    // 子类Bus,继承父类Verhicle 
{   public:  
        int guest;  
        Bus(string no1,int guest1):Vehicle(no1)  
        {     
            guest=guest1;  
        }  
        int money()  
        {     
            return guest*3;  // 载客数*3
        }  
};  

int main()  
{  
    Car car("",0,0);  // 实例化对象类,初始化Car的 载客数量,重量  
    Truck truck("",0);  
    Bus bus("",0);
      
    int i, repeat, type, weight, guest;  
    string no;  
    while(cin >> type )
       
       {
        if(type == 0)
            break;else switch(type) { case 0: break; // 输入0退出的功能没有成功 case 1: cin >> no >> guest >> weight; // 获取 Car的 载客数量,重量 car = Car(no, guest, weight); cout << no << ' ' << car.money() << endl; // 输出收费 break; case 2: cin >> no >> weight; // 获取Truck 的重量 truck = Truck(no, weight); cout << no << ' ' << truck.money() << endl; break; case 3: cin >> no >> guest; // 获取Bus的载客数 bus = Bus(no, guest); cout << no << ' ' << bus.money() << endl; break; } } return 0; }

注:程序可能不能按要求正确运行,因为我笨啊!


 

转载于:https://www.cnblogs.com/chuijingjing/p/9044599.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值