Consider the following Class Diagram; detailed description of the diagram is given in the table.
Class Name
|
Attribute Name
|
Attribute Data Type
|
Behavior (Functions)
|
Transport
|
Weight
|
Float
|
-Load()
-Unload
-Ship()
|
Capacity |
Character String
| ||
Speed
|
Float
| ||
Land_Transport
|
Transportation_Mode
|
Character String
|
-Ship
|
Vehicle_Type
|
Character String
| ||
Air_Transport | Aircraft_Type | Character String | -Ship |
Airline_Name | Character String | -Ship |
In given class diagram, Transport is a base class; while Land Transport and Air Transport are its derived classes.
You are required to implement above class diagram in C++. For this you have to use the concept of inheritance and polymorphism.
Solution Guidelines:
- In Load () function of Transport class, a message should be displayed “Goods are loaded successfully”.
- In Unload () function of Transport class, a message should be displayed “Goods are unloaded successfully”.
- Ship() function of each class should display a message representing each class. Each class will display their own message when this function call will be made. For example, against Ship () function ofLand_Transport class, message “In land transport, shipping is done via Truck" should be displayed and against Ship () function of Air_Transport class, message “In air transport, shipping is done via air cargo"should be displayed.
- Behavior of ship () function must be implemented using polymorphism.
Idea solution :
class Transport
{
private:
float Weight,Speed;
string Capacity;
public:
void Load()
{
cout"\nGoods are loaded successfully\n";
}
void Unload()
{
cout"Goods are Unloaded successfully\n";
}
virtual void Ship(){} OR virtual void Ship()=0;
};
class Land_Transport :public Transport
{
private:
string Transportation_Mode;
string Vehicle_Type;
public:
void Ship()
{
cout"In land transport, shipping is done via Truck";
}
};
class Air_Transport :public Transport
{
private:
string Aircraft_Type;
string Airline_Name;
public:
void Ship()
{
cout"\nIn air transport, shipping is done via air cargo\n\n";
}
};
int main(int argc, char *argv[])
{
char choice;
while(1)
{
cout"\n\nDo you want to transport your goods?\n\n";
cout"*************************************\n\n";
choice = getch();
if(choice == 'y' || choice == 'Y')
{
Menu();
}
cout"\nThank You for using this Program...";
}
}
void Menu()
{
Transport *ptr=NULL;
char tpt;
cout"Press 'L' or 'l' to use Land Transport Service\n\n";
cout"Press 'A' or 'a' to use Air Transport Service\n";
cout"*************************************\n";
tpt = getche();
if(tpt == 'A' or tpt == 'a')
{
ptr=new Air_Transport();
ptr->Ship();
}
if(tpt == 'L' or tpt == 'l')
{
ptr=new Land_Transport();
ptr->Ship();
}
getch();
}
//****************************************************************************
{
private:
float Weight,Speed;
string Capacity;
public:
void Load()
{
cout"\nGoods are loaded successfully\n";
}
void Unload()
{
cout"Goods are Unloaded successfully\n";
}
virtual void Ship(){} OR virtual void Ship()=0;
};
class Land_Transport :public Transport
{
private:
string Transportation_Mode;
string Vehicle_Type;
public:
void Ship()
{
cout"In land transport, shipping is done via Truck";
}
};
class Air_Transport :public Transport
{
private:
string Aircraft_Type;
string Airline_Name;
public:
void Ship()
{
cout"\nIn air transport, shipping is done via air cargo\n\n";
}
};
int main(int argc, char *argv[])
{
char choice;
while(1)
{
cout"\n\nDo you want to transport your goods?
cout"*************************************\n\n";
choice = getch();
if(choice == 'y' || choice == 'Y')
{
Menu();
}
cout"\nThank You for using this Program...";
}
}
void Menu()
{
Transport *ptr=NULL;
char tpt;
cout"Press 'L' or 'l' to use Land Transport Service\n\n";
cout"Press 'A' or 'a' to use Air Transport Service\n";
cout"*************************************\n";
tpt = getche();
if(tpt == 'A' or tpt == 'a')
{
ptr=new Air_Transport();
ptr->Ship();
}
if(tpt == 'L' or tpt == 'l')
{
ptr=new Land_Transport();
ptr->Ship();
}
getch();
}
//****************************************************************************