CPP Program for Implementing the Multilevel Inheritance Concept


To write a C++ program for implementing the multilevel inheritance concept.

Inheritance in C++

  • Inheritance is the capability of one class to acquire properties and characteristics from another class.
  • The class whose properties are inherited by another class is called the Parent or Base or Superclass.
  • And, the class which inherits properties of other class is called Child or Derived or Sub class.
  • Inheritance makes the code reusable.
  • When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.

NOTE: All members of a class except Private, are inherited

Purpose of Inheritance in C++

  1. Code Reusability
  2. Method Overriding (Hence, Runtime Polymorphism.)
  3. Use of Virtual Keyword

Access Control and Inheritance

  • A derived class can access all the non-private members of its base class.
  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to – who can access them in the following way −

Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono

A derived class inherits all base class methods with the following exceptions −

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

Basic Syntax of Inheritance:

class Subclass_name : access_mode Superclass_name
  • While defining a subclass like this, the super class must be already defined or atleast declared before the subclass declaration.
  • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.

Access Modifiers and Inheritance: Visibility of Class Members

  • Depending on the Access modifier used while inheritance, the availability of class members of Superclass in the subclass changes.
  • It can either be private, protected, or public.

1) Public Inheritance

  • This is the most used inheritance mode.
  • In this the protected member of super class becomes protected members of sub class and public becomes public.
class Subclass : public Superclass

2) Private Inheritance

  • In private mode, the protected and public members of super class become private members of derived class.
class Subclass : Superclass   // By default its private inheritance

3) Protected Inheritance

  • In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass

Types of Inheritance in C++

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

Multilevel Inheritance in C++

In this type of inheritance, the derived class inherits from a class, which in turn inherits from some other class. The Superclass for one is a subclass for the other.


using namespace std;
class base 
int x;
void getdata()
cout << "Enter value of x= "; cin >> x;cout<<x;
class derive1 : public base 
int y;
void readdata()
cout << "\nEnter value of y= "; cin >> y;cout<<y;
class derive2 : public derive1   
int z;
void indata()
cout << "\nEnter value of z= "; cin >> z;cout<<z;
void product()
cout << "\nProduct= " << x * y * z;
int main()
derive2 a;      
return 0;


7 3 2

Enter value of x= 7
Enter value of y= 3
Enter value of z= 2
Product= 42


Thus, the Implement the Multilevel Inheritance Concept was executed successfully.

Leave a Comment