Multiple Multilevel Hierarchical and Inheritance In C++

Multiple Multilevel Hierarchical and Inheritance In C++

Using examples, we will learn about the many inheritance models used in C++ programming, including multiple, multilevel, and hierarchical inheritance.

Today’s topic is an example of multiple multilevel and hierarchical inheritance in C++. It is crucial to comprehend inheritance before beginning to use multiple, multilevel, and hierarchical inheritance.

The derived class can utilize the base class’s properties thanks to inheritance. It is an element that object-oriented programming must have.

In more precise terms, we can say that inheritance occurs when a base class object immediately inherits all of the parent object’s properties and when we can access the parent class’s features using a child object.

One of the four cornerstones of object-oriented programming is inheritance (OOPs). A class can acquire the traits and properties of another class thanks to this functionality.

Because the derived class or the child class can reuse the members of the base class by inheriting them, inheritance enables code reuse.

To comprehend the idea of inheritance clearly, think about a real-world illustration. The capacity to speak, walk, eat and other abilities are only a few of the traits that a child receives from his or her parents.

However, his parents are not the only ones that have these characteristics. These characteristics come from the class of mammals, which his parents also inherit.

Once more, this mammal class inherits these traits from the animal class. The same principles govern inheritance.

Depending on the visibility option selected, the data members of the base class can be accessible after being copied via inheritance. Accessibility is always arranged in descending order, from public to protected. This article will primarily cover five different types of inheritance in C++. These are what they are:

  • Individual Inheritance
  • Several Inheritances
  • Several-Level Inheritance
  • inheritance in a hierarchy
  • hybrid succession

One essential component of an object-oriented programming language is inheritance. It enables programmers to derive a new class from an existing one. The base class’s features are passed down to the derived class (existing class).

In C++ programming, there are several inheritance models to choose from in Multiple Multilevel Hierarchical Inheritance.

C++ Multilevel Inheritance

In addition to being able to derive a class from the base class in C++ programming, you can also do so from the derived class. Multilevel inheritance is the name given to this inheritance type.

Multilevel Inheritance is the process by which a class can be descended from another descended class. A, B, and C are the three classes, let’s say.

The base class that class B derives from is A. B is hence A’s derived class. The class that comes from class B now is C.

As a result, class B becomes the foundation class for class C while also deriving from class A.

Multilevel Inheritance is the name given to this situation. According to the defined visibility modes, each respective derived class has access to the data members of its respective base class.

class A {
… .. …
};
class B: public A {
… .. …
};
class C: public B {
… … …
};

In this instance, class B is descended from base class A, while class C is descended from derived class B.

Example 1: C++ Multilevel Inheritance

#include <iostream>
using namespace std;

class A {
    public:
      void display() {
          cout<<"Base class content.";
      }
};

class B : public A {};

class C : public B {};

int main() {
    C obj;
    obj.display();
    return 0;
}

Output

Base class content.

Class C in this program is descended from class B. (derived from base class A).

The main() function defines the obj object of class C.

Display() in class A is called when the display() function is invoked. It’s because classes C and B don’t have a display() function.

The show() function in class C is the first place the compiler looks. It looks for the function in class B because it doesn’t exist there (as C is derived from B).

Additionally, the function is absent from class B, therefore the compiler searches for it in class A. (as B is derived from A).

If the show() function is present in C, the compiler overrides the class A display() (due to the member function overriding).

C++ Multiple Inheritance

A class in C++ programming can originate from several parents. For instance, the base classes Mammal and WingedAnimal are the ancestors of the class Bat.

It makes sense because a bat is both a mammal and an animal with wings.

Multiple inheritances are the inheritance process in which a class can take traits from more than one base class, or a derived class can have more than one source class.

At the point of inheritance, it provides access specifiers independently for each base class.

The combined features of all these classes can be derived by the derived class, and the derived or child class can access the data members of all the base classes in accordance with the access specifiers.

Example 2: Multiple Inheritance in C++ Programming

#include <iostream>
using namespace std;

class Mammal {
  public:
    Mammal() {
      cout << "Mammals can give direct birth." << endl;
    }
};

class WingedAnimal {
  public:
    WingedAnimal() {
      cout << "Winged animal can flap." << endl;
    }
};

class Bat: public Mammal, public WingedAnimal {};

int main() {
    Bat b1;
    return 0;
}

Output

Mammals can give direct birth.
Winged animal can flap.

Multiple Inheritance Ambiguity

When a function is overridden, multiple inheritances are the most obvious issue.

Let’s say that two base classes share a function that isn’t changed by derived classes.

The compiler displays an error if you attempt to call the function using an object from the derived class. It is a result of the compiler not knowing which function to call. For instance,

class base1 {
  public:
      void someFunction( ) {....}  
};
class base2 {
    void someFunction( ) {....} 
};
class derived : public base1, public base2 {};

int main() {
    derived obj;
    obj.someFunction() // Error!  
}

The scope resolution function can be used to resolve this issue by indicating which function should be classified as base1 or base2.

int main() {
obj.base1::someFunction( ); // Function of base1 class is called
obj.base2::someFunction(); // Function of base2 class is called.
}

C++ Hierarchical Inheritance

Hierarchical inheritance is the process of more than one class deriving properties from the base class.

All characteristics shared by child classes are included in the base class under hierarchical inheritance.

For instance, Science class is the foundation for Physics, Chemistry, and Biology. Dog, Cat, and Horse are also descended from the Animal class.

Hierarchical inheritance is the process of passing down several derived classes from a single base class.

Due to the fact that each class serves as the base class for one or more child classes, this inheritance has a tree-like structure.

Each derived class’s visibility mode is individually selected during inheritance, and it accesses the data members accordingly.

Syntax of Hierarchical Inheritance

class base_class {
     ... .. ...
}

class first_derived_class: public base_class {
     ... .. ...
}

class second_derived_class: public base_class {
     ... .. ...
}

class third_derived_class: public base_class {
     ... .. ...
}

Example 3: Hierarchical Inheritance in C++ Programming

// C++ program to demonstrate hierarchical inheritance

#include <iostream>
using namespace std;

// base class
class Animal {
   public:
    void info() {
        cout << "I am an animal." << endl;
    }
};

// derived class 1
class Dog : public Animal {
   public:
    void bark() {
        cout << "I am a Dog. Woof woof." << endl;
    }
};

// derived class 2
class Cat : public Animal {
   public:
    void meow() {
        cout << "I am a Cat. Meow." << endl;
    }
};

int main() {
    // Create object of Dog class
    Dog dog1;
    cout << "Dog Class:" << endl;
    dog1.info();  // Parent Class function
    dog1.bark();

    // Create object of Cat class
    Cat cat1;
    cout << "\nCat Class:" << endl;
    cat1.info();  // Parent Class function
    cat1.meow();

    return 0;
}

Output

Dog Class:
I am an animal.
I am a Dog. Woof woof.
Cat Class:
I am an animal.
I am a Cat. Meow.

In this case, the Animal class is where both the Dog and Cat classes are derived from. As a result, the info() function of the Animal class is accessible to both of the derived classes.

you may like:

Array In C++ with Example (Basic Example of Array)

Leave a Reply