Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Inheritance Models: Multiple, Multilevel, Hierarchical

C++ Inheritance Models: Multiple, Multilevel, Hierarchical

In this tutorial, we explore C++ inheritance patterns—multiple, multilevel, and hierarchical—with practical code examples.

Inheritance is a foundational feature of object‑oriented programming. It lets developers create new classes that inherit properties and behavior from existing ones.

Below we examine three key inheritance models available in C++.


C++ Multilevel Inheritance

In a multilevel hierarchy, a class derives from another derived class. The chain continues until a base class is reached.

class A {
    // Base class implementation
};
class B : public A {
    // B inherits from A
};
class C : public B {
    // C inherits from B
};

Here, B inherits from A, and C inherits from B, forming a vertical inheritance chain.

Example 1: Multilevel Inheritance in C++

#include <iostream>
using namespace std;

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

class B : public A {};

class C : public B {};

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

Output

Base class content.

This program demonstrates that an object of C can invoke display() defined in the ultimate base class A because no overriding implementation exists in B or C. The compiler resolves the call by searching the inheritance chain from the most derived class upward.


C++ Multiple Inheritance

C++ allows a class to inherit from more than one base class. For instance, a Bat can derive from both Mammal and WingedAnimal because a bat is simultaneously a mammal and a winged creature.

C++ Inheritance Models: Multiple, Multilevel, Hierarchical

Example 2: Multiple Inheritance in C++

#include <iostream>
using namespace std;

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

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

class Bat : public Mammal, public WingedAnimal {};

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

Output

Mammals can give direct birth.
Winged animals can flap.

Resolving Ambiguity in Multiple Inheritance

When two base classes provide a function with the same name, the compiler cannot decide which one to invoke, leading to an ambiguity error.

class Base1 {
public:
    void someFunction() { /* ... */ }
};

class Base2 {
public:
    void someFunction() { /* ... */ }
};

class Derived : public Base1, public Base2 {};

int main() {
    Derived obj;
    // obj.someFunction(); // Error: ambiguous
}

Use the scope resolution operator to specify the desired base‑class function:

int main() {
    Derived obj;
    obj.Base1::someFunction(); // Calls Base1's implementation
    obj.Base2::someFunction(); // Calls Base2's implementation
}

C++ Hierarchical Inheritance

In hierarchical inheritance, several derived classes inherit from a single base class. All common attributes and behavior are encapsulated in the base class, while each derived class can add its own specialization.

Examples include Physics, Chemistry, and Biology deriving from Science; and Dog, Cat, Horse deriving from Animal.

Syntax of Hierarchical Inheritance

class BaseClass {
    // Base class content
};

class DerivedOne : public BaseClass {
    // Additional members
};

class DerivedTwo : public BaseClass {
    // Additional members
};

class DerivedThree : public BaseClass {
    // Additional members
};

Example 3: Hierarchical Inheritance in C++

// C++ program demonstrating 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() {
    // Dog instance
    Dog dog1;
    cout << "Dog Class:" << endl;
    dog1.info();
    dog1.bark();

    // Cat instance
    Cat cat1;
    cout << "\nCat Class:" << endl;
    cat1.info();
    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.

Both Dog and Cat inherit the info() method from Animal, while each class introduces its own specialized behavior.


C Language

  1. Mastering C# Inheritance: Concepts, Types, and Practical Code
  2. C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
  3. Master C++ Inheritance: Build Powerful Classes with Reusable Code
  4. Master C++ Inheritance: Public, Protected & Private Explained
  5. C++ Friend Functions and Friend Classes: Mastering Access Control
  6. Mastering C++ Class Templates: A Practical Guide
  7. Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)
  8. C++ Classes & Objects: A Practical Guide with Code Examples
  9. Master C# Inheritance & Polymorphism: Practical Code Examples
  10. Mastering C# Inheritance: Build Reusable, Maintainable Code