In: Computer Science
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely:
Program requirements (Vehicle class):
Program requirements (Car class):
Program requirements (Truck class):
Write a main program that demonstrates the polymorphism capabilities, by:
The following is an example of how your program should run:
Car information
Wheels: 4
Weight: 2.5
PassengerLoad: 5
Truck information
Wheels: 8
Weight: 4.4
PayLoad: 9.5
/*
compile this code in dev c++ you change main method signature if you are using another compiler
*/
#include <iostream>
using namespace std;
class Vehicle
{
int wheels;
float weight;
public:
Vehicle(int _wheels,float
_weight)
{
setWheels(_wheels);
setWeight(_weight);
}
void setWheels(int w)
{
wheels=w;
}
int getWheels()
{
return
wheels;
}
void setWeight(float _weight)
{
weight=_weight;
}
float getWeight()
{
return
weight;
}
virtual void displayData()
{
cout<<"Wheels: "<<getWheels()<<endl;
cout<<"weight: "<<getWeight()<<endl;
}
};
class Car:public Vehicle
{
int passengerLoad;
public:
Car(int _wheels,float _weight,int
_passengerLoad):Vehicle(_wheels,_weight)
{
setPassengerLoad(_passengerLoad);
}
void setPassengerLoad(int
pass)
{
passengerLoad=pass;
}
int getPassengerLoad()
{
return
passengerLoad;
}
virtual void displayData()
{
cout<<"Car
Information"<<endl;
cout<<"Wheels: "<<getWheels()<<endl;
cout<<"weight: "<<getWeight()<<endl;
cout<<"PassengerLoad:
"<<getPassengerLoad()<<endl;
}
};
class Truck: public Vehicle
{
float payLoad;
public:
Truck(int _wheels,float
_weight,float _payLoad):Vehicle(_wheels,_weight)
{
setPayLoad(_payLoad);
}
void setPayLoad(int pass)
{
payLoad=pass;
}
int getPayLoad()
{
return
payLoad;
}
virtual void displayData()
{
cout<<"Truck Information"<<endl;
cout<<"Wheels: "<<getWheels()<<endl;
cout<<"weight: "<<getWeight()<<endl;
cout<<"PayLoad: "<<getPayLoad()<<endl;
}
};
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
int main(int argc, char** argv) {
Car car(4,2.5,5);
Truck truck(8,4.4,9.5);
Vehicle* ptrvehicle;
ptrvehicle=&car;
ptrvehicle->displayData();
ptrvehicle=&truck;
ptrvehicle->displayData();
return 0;
}
output:-