Skip to main content
Engineering LibreTexts

4.7: Types of Inheritance

  • Page ID
    34664
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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


    This page titled 4.7: Types of Inheritance is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?