Hierarchical Inheritance in C++

 


1️⃣ What is Hierarchical Inheritance?

Hierarchical inheritance is a type of inheritance where multiple derived classes inherit from a single base class. It allows code reuse while enabling different classes to have their own unique functionalities.

🔹 Key Features of Hierarchical Inheritance

  • Multiple derived classes inherit from the same base class.
  • Each derived class has access to the base class properties but can also define its own methods.
  • Helps in code reusability and structuring relationships.

2️⃣ Syntax of Hierarchical Inheritance

class Base {
    // Base class
};

class Derived1 : public Base {
    // Derived class 1
};

class Derived2 : public Base {
    // Derived class 2
};

class Derived3 : public Base {
    // Derived class 3
};
  • All derived classes (Derived1, Derived2, Derived3) inherit from the same base class Base.

3️⃣ Example: Hierarchical Inheritance in C++

#include <iostream>
using namespace std;

// Base Class
class Animal {
public:
    void eat() {
        cout << "Animals can eat." << endl;
    }
};

// Derived Class 1
class Dog : public Animal {
public:
    void bark() {
        cout << "Dogs can bark." << endl;
    }
};

// Derived Class 2
class Cat : public Animal {
public:
    void meow() {
        cout << "Cats can meow." << endl;
    }
};

// Derived Class 3
class Cow : public Animal {
public:
    void moo() {
        cout << "Cows can moo." << endl;
    }
};

int main() {
    Dog dog;
    Cat cat;
    Cow cow;

    // All objects can access eat() from Animal class
    dog.eat();
    dog.bark();

    cat.eat();
    cat.meow();

    cow.eat();
    cow.moo();

    return 0;
}

🔹 Output:

Animals can eat.
Dogs can bark.
Animals can eat.
Cats can meow.
Animals can eat.
Cows can moo.

🔹 Explanation:

  • Dog, Cat, and Cow inherit from Animal, so they can all use the eat() method.
  • Each derived class also has its own unique behavior (bark(), meow(), moo()).

4️⃣ Real-Life Example: Hierarchical Inheritance

Let's consider a Vehicle hierarchy:

#include <iostream>
using namespace std;

// Base Class
class Vehicle {
public:
    void start() {
        cout << "Vehicle is starting..." << endl;
    }
};

// Derived Class 1
class Car : public Vehicle {
public:
    void drive() {
        cout << "Car is driving..." << endl;
    }
};

// Derived Class 2
class Bike : public Vehicle {
public:
    void ride() {
        cout << "Bike is riding..." << endl;
    }
};

// Derived Class 3
class Truck : public Vehicle {
public:
    void load() {
        cout << "Truck is loading cargo..." << endl;
    }
};

int main() {
    Car car;
    Bike bike;
    Truck truck;

    car.start();
    car.drive();

    bike.start();
    bike.ride();

    truck.start();
    truck.load();

    return 0;
}

🔹 Output:

Vehicle is starting...
Car is driving...
Vehicle is starting...
Bike is riding...
Vehicle is starting...
Truck is loading cargo...

🔹 Explanation:

  • Car, Bike, and Truck all inherit from Vehicle.
  • All vehicles can start(), but each one has its own specific function (drive(), ride(), load()).

5️⃣ Advantages of Hierarchical Inheritance

Code Reusability: Common functionalities are defined in the base class and shared among derived classes.
Clear Relationship: A well-structured parent-child relationship.
Easier Maintenance: Modifications in the base class automatically reflect in all derived classes.


6️⃣ Disadvantages of Hierarchical Inheritance

Code Duplication: If different derived classes need similar modifications, they must be made separately.
Tightly Coupled Code: Changes in the base class can affect all derived classes.
Inheritance Overhead: Multiple derived classes may increase memory usage.


7️⃣ When to Use Hierarchical Inheritance?

✅ When multiple classes share common behavior but have unique features.
✅ When modeling real-world hierarchies, like animals, vehicles, employees, etc.
✅ When aiming for better code structure and reusability.


8️⃣ Summary of Hierarchical Inheritance

Feature Description
Definition Multiple classes inherit from the same base class.
Code Reusability Common functionality is defined once in the base class.
Flexibility Each derived class can add its own unique behavior.
Example Dog, Cat, and Cow inheriting from Animal.
Common Issues Code duplication, dependency on base class changes.

Would you like an example with constructors or a comparison of all inheritance types? 😊

Post a Comment

0 Comments