Basic Example using Multilevel Inheritance in C++


In this program, there are three classes: A, B, and C. B inherits from A, and C inherits from B. This forms a chain of inheritance, with C inheriting from B and B inheriting from A. Each of the classes has a constructor, which is called when an object of that class is created. When an object of class C is created in the main function, the constructor of class A is called first, followed by the constructor of class B, and finally the constructor of class C.

Source Code

#include<iostream>
using namespace std;
class A
{
  public:
    A()
    {
      cout<<"Constructor of A class"<<endl;
    }
};
class B: public A
{
  public:
    B()
    {
      cout<<"Constructor of B class"<<endl;
    }
};
class C: public B
{
  public:
    C()
    {
      cout<<"Constructor of C class"<<endl;
    }
};
int main()
{
   C obj;
   return 0;
}
To download raw file Click Here

Output

Constructor of A class
Constructor of B class
Constructor of C class

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples