Skip to main content
Engineering LibreTexts

4.5: Types of Inheritance in C++

  • Page ID
    34662
  • Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.

    A derived class B that inherits only from one base class A

    Syntax:

    class subclass_name : access_mode base_class 
    { 
       //body of subclass 
    };
     
    // C++ program to      explain 
    // Single inheritance 
    #include <iostream>       
    using namespace std;
    
    // base class 
    class Vehicle 
    { 
          public: 
          Vehicle() 
          { 
             cout << "This is a Vehicle" << endl; 
          } 
    };
    
    // sub class derived from the base classes 
    // although this class does NOTHING
    class Car: public Vehicle
    {   
       public:
       Car()
       {
           cout << "This is a Car" << endl;
       }
    
    };
    
    // main function 
    int main() 
    { 
        // creating object of sub class will 
        // invoke the constructor of base classes 
        Car obj; 
        return 0; 
    } 

    Output - notice that the parent class is initialized first, then the child class - which inherits from the parent class.

    This is a Vehicle
    This is a Car
    

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

    • Was this article helpful?