Hierarchical Inheritance

Anonymous contributor's avatar
Anonymous contributor
Published Feb 16, 2025
Contribute to Docs

In C++, hierarchical inheritance is a type of inheritance where multiple derived classes inherit from a single base class, meaning one parent class has multiple child classes. This allows for code reusability, as common methods and properties in the base class can be shared across different derived classes.

Syntax

The following syntax can be used to achieve hierarchical inheritance in C++:

class BaseClass {
  // Base class members (properties and methods)
};

class DerivedClass1 : accessSpecifier BaseClass {
  // Derived class 1 members
};

class DerivedClass2 : accessSpecifier BaseClass {
  // Derived class 2 members
};

Example

The following example demonstrates hierarchical inheritance in C++:

#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks." << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "Cat meows." << endl;
}
};
int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.eat(); // Inherited from Animal class
dog1.bark(); // Dog's own function
// Create object of Cat class
Cat cat1;
cout << "\nCat Class:" << endl;
cat1.eat(); // Inherited from Animal class
cat1.meow(); // Cat's own function
return 0;
}

The output for the above code is:

Dog Class:
This animal eats food.
Dog barks.
Cat Class:
This animal eats food.
Cat meows.

In this example, the base class Animal has a function eat(). The derived classes Dog and Cat inherit from Animal, allowing them to access the eat() function. Each derived class also has its own unique function: bark() for Dog and meow() for Cat.

Codebyte Example

Run the following codebyte to understand how hierarchical inheritance works in C++:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy