Sunday, 1 July 2018

12th D Prog-7 Inheritance Programming;

//7A C++ program to explain
// Multiple inheritance
#include <iostream.h>
// first base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// second base class
class FourWheeler
{
  public:
    FourWheeler()
    {
      cout << "This is a 4 wheeler Vehicle" << endl;
    }
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler
{
};
// main function
int main()
{  
    // creating object of sub class will
    // invoke the constructor of base classes
    Car obj;
}

Output:
This is a Vehicle
This is a 4 wheeler Vehicle

//7B C++ program to implement
// Multilevel Inheritance
#include <iostream.h>

// base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
class fourWheeler: public Vehicle
public:
    fourWheeler()
    {
      cout<<"Objects with 4 wheels are vehicles"<<endl;
    }
};
// sub class derived from two base classes
class Car: public fourWheeler{
   public:
     car()
     {
       cout<<"Car has 4 Wheels"<<endl;
     }
};
// main function
int main()
{  
    //creating object of sub class will
    //invoke the constructor of base classes
    Car obj;
    
}




//7C C++ program to implement
// Hierarchical Inheritance
#include <iostream.h>
// base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle
{
     
};
// main function
int main()
{  
    // creating object of sub class will
    // invoke the constructor of base class
    Car obj1;
    Bus obj2;
}



Output:
This is a Vehicle
This is a Vehicle

No comments:

Post a Comment