Skip to main content
Engineering LibreTexts

4.7: Types of Inheritance

  • Page ID
    34664
  • Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.

    A derived class A inherits from base class B, which in turn inherits from base class C

    // C++ program to implement  
    // Multilevel Inheritance 
    #include <iostream> 
    using namespace std; 
    
    // 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; 
    
        return 0; 
    } 

    Output:

    This is a Vehicle
    Objects with 4 wheels are vehicles
    Car has 4 Wheels


    by-sa.png
    Contributed by Harsh Agarwal
    Geeks for Geeks