Write a program that implements the ADT vehicle (a class with at least one pure
ID: 3823891 • Letter: W
Question
Write a program that implements the ADT vehicle (a class with at least one pure virtual function). Information you may want to keep about a vehicle includes
· identification information
· empty weight
· maximum gross weight
· maximum range
· maximum speed
The constructor should take all such information as arguments. Include virtual functions (may be pure or impure) for interrogating each such attribute. Also include a pure virtual function for interrogating the transportation mode.
Construct derived classes airplane, boat, and car each derived directly from vehicle. Each derived class should provide an interrogation function for the transportation mode, which should indicate by-air, by-land, or by-water (or something similar).
Write a short program which instantiates at least one object of each derived type. Verify all operations work correctly.
Explanation / Answer
CODE:
CODE:
#include <iostream>
#include <string>
using namespace std;
class Vehicle //Abstract base class
{
private:
string identificationInfo;
int emptyWeight;
int maxGrossWeight;
int maxRange;
int maxSpeed;
public:
virtual void transportationMode() = 0; //Pure Virtual Function
};
class Airplane:public Vehicle
{
private:
string identificationInfo;
int emptyWeight;
int maxGrossWeight;
int maxRange;
int maxSpeed;
public:
Airplane(idInfo,emWeight,maxGrossWght, maxRnge, maxSpd): identificationInfo(idInfo), emptyWeight(emWeight), maxGrossWeight(maxGrossWght),
maxRange(maxRnge),maxSpeed(maxSpd)
{
}
void transportationMode()
{ cout << "By Air";
}
};
class Boat:public Vehicle
{
private:
string identificationInfo;
int emptyWeight;
int maxGrossWeight;
int maxRange;
int maxSpeed;
public:
Boat(idInfo,emWeight,maxGrossWght, maxRnge, maxSpd): identificationInfo(idInfo), emptyWeight(emWeight), maxGrossWeight(maxGrossWght),
maxRange(maxRnge),maxSpeed(maxSpd)
{
}
void transportationMode()
{ cout << "By Water";
}
};
class Car:public Vehicle
{
private:
string identificationInfo;
int emptyWeight;
int maxGrossWeight;
int maxRange;
int maxSpeed;
public:
Car(idInfo,emWeight,maxGrossWght, maxRnge, maxSpd): identificationInfo(idInfo), emptyWeight(emWeight), maxGrossWeight(maxGrossWght),
maxRange(maxRnge),maxSpeed(maxSpd)
{
}
void transportationMode()
{ cout << "By Road";
}
};
int main()
{
Vehicle *b;
Airplane ar ("NewAirplane", 30, 20, 25, 15);
Boat bt("NewBoat", 30, 20, 25, 15);
Car cr("NewCar", 30, 20, 25, 15);
b = &ar;
b->transportationMode();
b = bt;
b->transportationMode();
b = &cr;
b->transportationMode();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.