Example of Inheritance and OOP with Class Vehicle


VEHICLE IS PARENT CLASS:
Land vehicle and Water vehicle are Child classes of (VEHICLE)
THEY both inherit the properties of Parent class(Vehicle)


  1 #include <stdio.h>
  2  #include <conio.h>
  3  #include <iostream>
  4  using namespace std;
  5 
  6  class vehicle    /*it is parent class*/
  7  {   protected:    /* Protected=Accessible from own class+derived class but not FROM outside the class*/
  8      char companyname[15];
  9      int speed;
 10      int capacity;
 11      int weight;
 12      int price;
 13      char movement[30];
 14      public:
 15      void movementv(); /*Member Functions are declared here and defined outside the class*/
 16      void parentin(); 
 17      void parentout();
 18  };
 19  /*Member functions of CLASS VEHICLES ARE DEFINED HERE*/
 20  void vehicle::movementv()
 21  {
 22      cout<<"enter the movement of vehicle";
 23      gets(movement);
 24  }
 25  void vehicle::parentin()
 26  {
 27      cout<<"enter the name of companay\n";
 28      gets(companyname);
 29      cout<<"enter the speed of your vehicle \n";
 30      cin>>speed;
 31      cout<<"enter the capacity of your vehicle\n";
 32      cin>>capacity;
 33      cout<<"enter the price\n";
 34      cin>>price;
 35  }
 36  void vehicle::parentout()
 37  {
 38      cout<<"Company name: "<<companyname<<"\n";
 39      cout<<"SPEED="<<speed<<"\n";
 40      cout<<"Capacity: "<<capacity<<"\n";
 41      cout<<"Price: "<<price<<"\n";
 42      cout<<"Movement:"<<movement<<"\n";
 43  }
 44 
 45  /*wE HAVE CREATED a child class here named it>land vehicle,
 46  it has all the behaviours of original class(VEHICLE)+some of its own abbilites*/
 47  class landvehicle:public vehicle
 48  {
 49  private:
 50      int gears;
 51      int tyres;
 52  public:
 53     void landin();
 54      void landout();
 55  };
 56 
 57  void landvehicle::landin()
 58  {
 59      cout<<"enter total gears and tyres \n";
 60      cin>>gears;
 61      cin>>tyres;
 62 
 63  }
 64 
 65  void landvehicle::landout()
 66  {
 67      cout<<"Gears:"<<gears<<"\n";
 68      cout<<"tyres:"<<tyres<<"\n";
 69  }
 70 
 71  /*this is our second Child class WATERVEHICLE IT has behaviours of its PARENT(CLASS VEHICLE)+its own abiliites*/
 72  class watervehicle:public vehicle
 73  {
 74  private:
 75      int nooffans;
 76      int lifesvingjackets;
 77  public:
 78      void waterin()
 79      {
 80          cout<<"enter the number of fans in your ship\n";
 81          cin>>nooffans;
 82          cout<<"enter the number of life saving jaceksts\n";
 83          cin>>lifesvingjackets;
 84      }
 85      void waterout()
 86      {
 87          cout<<"JACKETS:"<<lifesvingjackets<<"\n";
 88          cout<<"No.of fans:"<<nooffans<<"\n";
 89      }
 90  };
 91 
 92  int main()
 93  {
 94     landvehicle car1,car2; /*objects of (class LANDVEHICLE) YOU CAN EXTEND IT TO CAR3,CAR4.....*/
 95     watervehicle boat1,boat2;
 96     cout<<"enter the info of car1\n";
 97     car1.movementv();
 98     car1.parentin();
 99     car1.landin();
100     cout<<"enter the info of car2\n";
101     car2.movementv();
102     car2.parentin();
103     car2.landin();
104 
105     cout<<"enter the info of boat1\n";
106     boat1.movementv();
107     boat1.parentin();
108     boat1.waterin();
109     cout<<"enter the info of boat2\n";
110     boat2.movementv();
111     boat2.parentin();
112     boat2.waterin();
113 
114 
115     car1.parentout();
116     car1.landout();
117     car2.parentout();
118     car2.landout();
119 
120     boat1.parentout();
121     boat1.waterout();
122     boat2.parentout();
123     boat2.waterout();
124  }