loading...

CS304 Assignment 3 Solution Spring 2017

Problem Statement:

In continuation of Assignment No. 1 and 2, we have the following part of class diagram:

Car, SUV and HDV are concrete classes as opposed to Vehicle with a pure virtual function.

You are required to implement polymorphism in the above scenario by writing a complete C++ program with the mentioned data members and member functions.

In the main function, create one object each of Car, SUV and HDV and call their respective showGears() function thus implementing the concept of polymorphism.

Following is the sample output that you need to print:

Solution :

 
 #include<iostream>
 using namespace std;
 class vihicle{
 protected:
 string model,color, noofgear;
 public:
 void getmodel(string mod)
 {
 model = mod;
 }
 void getcolor(string c)
 {
 color = c;
 }
 void getnoofgear(string gear){
 noofgear = gear;
 }
 virtual void showgear() = 0; //Pure virtual function.....
 };
 class car : public vihicle{
 public:
 void showgear()
 {
 getmodel("Corolla");
 getcolor("White");
 getnoofgear("4-6");
 cout<<"Model of Vihicle: "<<model<<endl;
 cout<<"Color of Vihicle: "<<color<<endl;
 cout<<"There are "<<noofgear<<" Gears in a car"<<endl;
 }
 };
 class SUV : public vihicle{
 public:
 void showgear()
 {
 getmodel("CRU");
 getcolor("Green");
 getnoofgear("6-8");
 cout<<"Model of Vihicle: "<<model<<endl;
 cout<<"Color of Vihicle: "<<color<<endl;
 cout<<"There are "<<noofgear<<" Gears in a SUV"<<endl;
 }
 };
 class HDV : public vihicle{
 public:
 void showgear()
 {
 getmodel("Truck");
 getcolor("Red");
 getnoofgear("8-16");
 cout<<"Model of Vihicle: "<<model<<endl;
 cout<<"Color of Vihicle: "<<color<<endl;
 cout<<"There are "<<noofgear<<" Gears in a HDV"<<endl;
 }
 };
 main()
 {
 car car;
 car.showgear();
 cout<<endl;
 SUV suv;
 suv.showgear();
 cout<<endl;
 HDV hdv;
 hdv.showgear();
 system("pause");
 }
DOWNLOAD SOLUTION HERE
loading...