AppDividend
Latest Code Tutorials

Multiple Multilevel and Hierarchical Inheritance in C++ Example

0

Multiple Multilevel and Hierarchical Inheritance in C++ Example is today’s topic. Before getting started with Multiple, Multilevel, and Hierarchical inheritance, it is essential to understand inheritance. Inheritance allows properties of the base class to be used in the derived class. It is a necessary feature of object-oriented programming. In more technical words we can say that when an object of base class acquires all the properties of the parent object automatically and we can access the features of the parent class with the help of child’s object then it is an inheritance.

Multiple Inheritance in C++

When a derived class is derived from more than one base class, it is known as multiple inheritance.

 

Multiple Inheritance

In the above example, we can see that the child is derived from both the parents.

There is an ambiguity in multiple inheritance which occurs at the time of function overriding. For example, two-parent classes have the same function which is not overridden in the child class, and if we try to call the function using the object of the child class, it shows in compiler error as the compiler doesn’t know which function to call. (Example program in the last).

#Multilevel Inheritance in C++

When one class inherits the properties of another class, which is further inherited by another class, it is known as multilevel inheritance.

 

Multilevel Inheritance in C++

In the above example, we can see that the child is derived from parents and parents are further derived from grandparents; hence, they show multilevel inheritance.

The level of inheritance can be extended to any number depending upon the relation.

See examples at last for better understanding.

#Hierarchical Inheritance in C++

It is an inheritance in which we usually derive more than one derived class from a particular base class.

 

Multiple Multilevel and Hierarchical Inheritance

In the above example, we can see that Civil, CSE, and Mechanical are hierarchically derived from Engineering itself, which shows hierarchical inheritance.

#Programs of Multiple, Multilevel and Hierarchical Inheritance in C++

Write a program to show a mechanism of Multiple Inheritance.

#include <iostream>
using namespace std;

class patna
{ //Creating base class of patna
public:
  patna()
  {
    cout << "Hello I am from base class of Patna !! " << endl;
  }
};

class delhi
{
public:
  delhi()
  {
    cout << "Hello I am from base class of Delhi" << endl; //creating base class of delhi
  }
};

class check : public patna, public delhi //derived class of two base classes
{
public:
  void check_fun()
  {
    cout << "hello I am derived class function" << endl;
  }
};

int main()
{
  check obj;
  obj.check_fun();
  return 0;
}

See the following output.

 

Multiple Inheritance Program

Write the program to show the mechanism of Multilevel Inheritance.

#include <iostream>
using namespace std;

class city
{
public:
  city()
  {
    cout << "Hello I am the 1st base class of cities" << endl;
  }
};
class delhi : public city
{
public:
  delhi()
  {
    cout << "Hello I am the capital of India Delhi" << endl;
  }
};
class location : public delhi
{
public:
  location()
  {
    cout << "I am vasant kunj(location) in Delhi" << endl;
  }
};

// main function
int main()
{
  location obj;
  return 0;
}

See the following output.

 

Multilevel Inheritance Program

Write a program to show the mechanism of Hierarchical Inheritance.

#include <iostream>
using namespace std;

class city
{
public:
  city()
  {
    cout << "I am the base class of cities" << endl;
  }
};

class delhi : public city
{
public:
  void check_1()
  {
    cout << "I am the capital of India Delhi" << endl;
  }
};

class mumbai : public city
{
public:
  void check_2()
  {
    cout << "I am the city of dreams Mumbai" << endl;
  }
};

int main()
{
  delhi obj1;
  mumbai obj2; //constructor will be called two times as both the objects will call the constructor of the base class.
  obj1.check_1();
  obj2.check_2();

  return 0;
}

See the following output.

 

Hierarchical Inheritance Program

Finally, Multiple Multilevel and Hierarchical Inheritance in C++ Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.