Wednesday, October 19, 2016

Polymorphism example in c++ language

Problem

Implement the following given scenario, include two  constructors (no argument, with arguments), two functions getvalue(), and  displayvalue() for all classes.
polymorphism program in c++

Code

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Vehicle
{
      protected:
                int i;
                string s;
     
      public:
             Vehicle(): i(0), s("")
             {         }
             Vehicle(int a,string ss): i(a), s(ss)
             {           }
             void getdata()
             {
                  cout<<"Enter Vehicle Number: ";
                  cin>>i;
                  fflush(stdin);
                  cout<<"Enter vehicle name: ";
                  getline(cin, s);
             }
           
             void display()
             {
                  cout<<"Vehicle Number: "<<i;
                  cout<<"Vehicle Name: "<<s;
             }
};

class land_Vehicle : public Vehicle
{
      protected:
                int b;
                string s1;
     
      public:
             land_Vehicle(): b(0), s1("")
             {         }
             land_Vehicle(int a,string ss, int r, string s0): b(a), s1(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Land Vehicle Number: ";
                  cin>>b;
                  fflush(stdin);
                  cout<<"Enter Land vehicle name: ";
                  getline(cin, s1);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<b;
                  cout<<"Vehicle Name: "<<s1;
             }
};

class Boat : public Vehicle
{
      protected:
                int c;
                string s2;
     
      public:
             Boat(): c(0), s2("")
             {         }
             Boat(int a,string ss, int r, string s0): c(a), s2(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Boat Number: ";
                  cin>>c;
                  fflush(stdin);
                  cout<<"Enter Boat name: ";
                  getline(cin, s2);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<c;
                  cout<<"Vehicle Name: "<<s2;
             }
};

class aircraft : public Vehicle
{
      protected:
                int d;
                string s3;
     
      public:
             aircraft(): d(0), s3("")
             {         }
             aircraft(int a,string ss, int r, string s0): d(a), s3(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Aircraft Number: ";
                  cin>>d;
                  fflush(stdin);
                  cout<<"Enter Aircarft name: ";
                  getline(cin, s3);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<d;
                  cout<<"Vehicle Name: "<<s3;
             }
};

class car : public land_Vehicle
{
      protected:
                int e;
                string s4;
     
      public:
             car(): e(0), s4("")
             {         }
             car(int a,string ss, int q1, string q2,int q3,string q4): e(a), s4(ss),land_Vehicle(q1,q2,q3,q4)
             {
             }
             void getdata()
             {
                  land_Vehicle :: getdata();
                  cout<<"Enter car Number: ";
                  cin>>e;
                  fflush(stdin);
                  cout<<"Enter car name: ";
                  getline(cin, s4);
             }
           
             void display()
             {
                  land_Vehicle :: display();
                  cout<<"Vehicle Number: "<<e;
                  cout<<"Vehicle Name: "<<s4;
             }
};

class truck  : public land_Vehicle
{
      protected:
                int f;
                string s5;
     
      public:
             truck(): f(0), s5("")
             {         }
             truck(int a,string ss, int q, string qq,int q3,string q4): f(a), s5(ss),land_Vehicle(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  land_Vehicle :: getdata();
                  cout<<"Enter truck Number: ";
                  cin>>f;
                  fflush(stdin);
                  cout<<"Enter truck name: ";
                  getline(cin, s5);
             }
           
             void display()
             {
                  land_Vehicle :: display();
                  cout<<"Vehicle Number: "<<f;
                  cout<<"Vehicle Name: "<<s5;
             }
};

class sports_car  : public car
{
      protected:
                int g;
                string s6;
     
      public:
             sports_car(): g(0), s6("")
             {         }
           
             sports_car(int a,string ss, int q, string qq,int q3,string q4,int q5,string q6): g(a), s6(ss),car(q,qq,q3,q4,q5,q6)
             {           }
             void getdata()
             {
                  car:: getdata();
                  cout<<"Enter sports car Number: ";
                  cin>>g;
                  fflush(stdin);
                  cout<<"Enter sports car name: ";
                  getline(cin, s6);
             }
           
             void display()
             {
                  car:: display();
                  cout<<"Vehicle Number: "<<g;
                  cout<<"Vehicle Name: "<<s6;
             }
};

class yacht  : public Boat
{
      protected:
                int h;
                string s7;
     
      public:
             yacht(): h(0), s7("")
             {         }
             yacht(int a,string ss, int q, string qq,int q3,string q4): h(a), s7(ss),Boat(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  Boat::getdata();
                  cout<<"Enter yacht Number: ";
                  cin>>h;
                  fflush(stdin);
                  cout<<"Enter yacht name: ";
                  getline(cin, s7);
             }
           
             void display()
             {
                  Boat :: display();
                  cout<<"Vehicle Number: "<<h;
                  cout<<"Vehicle Name: "<<s7;
             }
};

class sail_boat  : public Boat
{
      protected:
                int k;
                string s8;
     
      public:
             sail_boat(): k(0), s8("")
             {         }
             sail_boat(int a,string ss, int q, string qq,int q3,string q4): k(a), s8(ss),Boat(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  Boat :: getdata();
                  cout<<"Enter sail_boat Number: ";
                  cin>>k;
                  fflush(stdin);
                  cout<<"Enter sail_boat name: ";
                  getline(cin, s8);
             }
           
             void display()
             {
                  Boat :: display();
                  cout<<"Vehicle Number: "<<k;
                  cout<<"Vehicle Name: "<<s8;
             }
};

class f16 : public aircraft
{
       protected:
                int j;
                string s9;
     
      public:
             f16(): j(0), s9("")
             {         }
             f16(int a,string ss, int q, string qq,int q3,string q4): j(a), s9(ss), aircraft(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  aircraft :: getdata();
                  cout<<"Enter f16 Number: ";
                  cin>>j;fflush(stdin);
                  cout<<"Enter f16 model: ";
                  getline(cin, s9);
             }
           
             void display()
             {
                  aircraft :: display();
                  cout<<"f16 Number: "<<j;
                  cout<<"f16 model: "<<s9;
             }
};

class helicopter  : public aircraft
{
      protected:
                int l;
                string s10;
     
      public:
             helicopter(): l(0), s10("")
             {         }
             helicopter(int a,string ss, int q, string qq,int q3,string q4): l(a), s10(ss),aircraft(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  aircraft :: getdata();
                  cout<<"Enter helicopter Number: ";
                  cin>>l;
                  cout<<"Enter helicopter model: ";
                  getline(cin, s10);
             }
           
             void display()
             {
                  aircraft :: display();
                  cout<<"helicopter Number: "<<l;
                  cout<<"helicopter model: "<<s10;
             }
};

int main()
{
    aircraft a;
    a.getdata();
    a.display();
   
    Boat b;
    b.getdata();
    b.display();
   
    land_Vehicle l;
    l.getdata();
    l.display();
   
    car c;
    c.getdata();
    c.display();
   
    truck t;
    t.getdata();
    t.display();
   
    sports_car sp;
    sp.getdata();
    sp.display();
   
    yacht y;
    y.getdata();
    y.display();
   
    sail_boat s;
    s.getdata();
    s.display();
   
    f16 f;
    f.getdata();
    f.display();
   
    helicopter h;
    h.getdata();
    h.display();
   
    cout<<"Press any key exit.";
    getch();
    return 0;
}

Output

polymorphism program in c++

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact