车辆类,派生小轿车、卡车、吉普车类,收费站对来往车辆收费。_百度知 ...
发布网友
发布时间:2024-08-05 10:34
我来回答
共1个回答
热心网友
时间:2024-08-14 18:16
#include <iostream>
using namespace std;
class Car
{
protected:
double wight;//车辆自重
unsigned passenger;//载客人数
//高速收费与这两个参数有关
public:
Car(double,unsigned);
inline double GetW()const;
inline unsigned GetP()const;
};
Car::Car(double wight,unsigned passenger):
wight(wight),passenger(passenger)
{
}
double Car::GetW() const
{
return wight;
}
unsigned Car::GetP() const
{
return passenger;
}
class Jeep:public Car
{
public:
Jeep(double,unsigned);
};
Jeep::Jeep(double wight,unsigned passenger):
Car(wight,passenger)
{
}
class PrivateCar:public Car
{
public:
PrivateCar(double,unsigned);
};
PrivateCar::PrivateCar(double wight,unsigned passenger):
Car(wight,passenger)
{
}
class Truck:public Car
{
public:
Truck(double,unsigned);
};
Truck::Truck(double wight,unsigned passenger):
Car(wight,passenger)
{
}
class FeeStation
{
public:
FeeStation();
double GetFee(const Jeep&);
double GetFee(const PrivateCar&);
double GetFee(const Truck&);
double CountFee()const;
private:
double feeParam[3];//收费参数,不同车辆在构造函数中初始化不同收费参数
double fee;
};
FeeStation::FeeStation()
{
fee=0.0f;
feeParam[0]=1.1;//吉普收费参数
feeParam[1]=2.2;//轿车收费参数
feeParam[2]=3.3;//卡车收费参数
}
double FeeStation::GetFee(const Jeep& jeep)
{
double fee = jeep.GetP()+jeep.GetW()*feeParam[0];//收费算法,自己乱编的,依据情况而定
this->fee+=fee;
return fee;
}
double FeeStation::GetFee(const PrivateCar& PCar)
{
double fee = PCar.GetP()+PCar.GetW()*feeParam[1];
this->fee+=fee;
return fee;
}
double FeeStation::GetFee(const Truck& truck)
{
double fee = truck.GetP()+truck.GetW()*feeParam[2];
this->fee+=fee;
return fee;
}
double FeeStation::CountFee()const
{
return fee;
}
int main()
{
FeeStation fStation;
Jeep jeep(2000,7);
PrivateCar Pcar(1000,5);
Truck truck(3000,2);
cout<<"吉普收费:"<<fStation.GetFee(jeep)<<endl;
cout<<"轿车收费:"<<fStation.GetFee(Pcar)<<endl;
cout<<"卡车收费:"<<fStation.GetFee(truck)<<endl;
cout<<"共计收费:"<<fStation.CountFee()<<endl;
return 0;
}