设计交通工具类族: 开发一个名为Vehicle 的类的层次体系。创建两个类Taxi 和Truck,均以公有模式从类Vehicle 中继承而来。Taxi 类中应包含一个数据成员passenger说明其是否载客。Truck类应包含一个数据成员cargo说明其是否载货。根据题后附的测试程序输出结果 为类Vehicle添加必要的数据成员,并为所有类添加必要的函数来控制和访问类的数据。编写一段测试程序,将Vehicle对象、Truck 对象和Taxi对象打印到屏幕。(30分)
测试程序输出实例为:
Vehicle
Number of doors: 2
Number of cylinders: 6
Transmission type: 3
Color: blue
Fuel level: 14.6
Taxi
Number of doors: 4
Number of cylinders: 6
Transmission type: 5
Color: yellow
Fuel level: 3.3
The taxi has no passengers.
Truck
Number of doors: 2
Number of cylinders: 16
Transmission type: 8
Color: black
Fuel level: 7.54
The truck is carrying cargo.
就是测试多态性呗,有门啥的一些属性
class Vehicle
{
private:
int doors;
int cylinders;
int transmission;
string color;
float fuel;
public:
Vehicle() {}
Vehicle(int d,int c,int t,string col,float f)
{
doors = d;
cylinders = c;
transmission = t;
color = col;
fuel = f;
}
virtual void print()
{
cout<<Number of doors:"<<doors<<endl;
cout<<Number of cylinders:"<<cylinders<<endl;
cout<<Transmisson type:"<<transmission<<endl;
cout<<Color:"<<color<<endl;
cout<<"Fuel level:"<<fuel<<endl;
}
};
class Taxi : public Vehicle
{
private:
int passagers;
public:
Taxi() {}
Taxi(int d,int c,int t,string col,float f,int p) : Vehicle(d,c,t,col,f)
{
passagers = p;
}
virtual void print()
{
Vehicle::Print();
if(passagers == 0)
cout<<"the taxi has no passengers"<<endl;
else
cout<<"Ther taxi is carrying passengers."<<endl;
}
};
class Trunk: public Vehicle
{
private:
int cargo;
public:
Trunk() {}
Trunk(int d,int c,int t,string col,float f,int car) : Vehicle(d,c,t,col,f)
{
cargp = cargo;
}
virtual void print()
{
Vehicle::Print();
if(cargo == 0)
cout<<"the trunk has no cargo"<<endl;
else
cout<<"Ther trunk is carrying cargo."<<endl;
}
};
int main()
{
Vehicle *p = new Vehicle(2,6,3,"blue",14.6);
p->print();
delete p;
p = new Taxi(4,6,5,"yellow",3.3,0);
p->print();
delete p;
p = new Trunk(2,16,8,"black",7.5,10);
p->print();
delete p;
}