Skip to main content
Engineering LibreTexts

4.6: Types of Inheritance in C++

  • Page ID
    34663
  • Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.

    A derived class A that inherits from base classes B and C

    Syntax:

    class subclass_name : access_mode base_class1, access_mode base_class2, ....
    {
      //body of subclass
    };
    

    Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for every base class must be specified.

    
    // C++ program to explain  
    // multiple inheritance 
    #include <iostream> 
    using namespace std; 
      
    // 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 
    {
        public:
        Car()
        {
          cout << "This vehicle is a 4 wheeldrive Car" << 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
    This is a 4 wheeler Vehicle
    This vehicle is a 4 wheeldrive Car
    

     

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

    • Was this article helpful?