5.1) Inheritance in C++

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create a new class (the derived class) based on an existing class (the base class or parent class). Inheritance establishes a relationship between classes, enabling the derived class to inherit attributes and methods from the base class. This promotes code reuse, modularity, and the creation of hierarchical class structures.

Here’s a detailed explanation of inheritance with code examples:

1. Single Inheritance


In single inheritance, a derived class inherits from a single base class.

#include <iostream>
#include <string>

// Base class
class Animal {
public:
    Animal(std::string n) : name(n) {}

    void eat() {
        std::cout << name << " is eating." << std::endl;
    }

    void sleep() {
        std::cout << name << " is sleeping." << std::endl;
    }

private:
    std::string name;
};

// Derived class
class Dog : public Animal {
public:
    Dog(std::string n, std::string b) : Animal(n), breed(b) {}

    void bark() {
        std::cout << "Woof! Woof!" << std::endl;
    }

private:
    std::string breed;
};

int main() {
    Dog dog("Buddy", "Golden Retriever");

    dog.eat();   // Inherited from Animal
    dog.bark();  // Defined in Dog
    dog.sleep(); // Inherited from Animal

    return 0;
}

Output:

Buddy is eating.
Woof! Woof!
Buddy is sleeping.

2. Multiple Inheritance


C++ supports multiple inheritance, where a class can inherit from multiple base classes. However, it can lead to ambiguity and complexity, so it should be used cautiously.

class A {
public:
    void print() {
        std::cout << "A" << std::endl;
    }
};

class B {
public:
    void print() {
        std::cout << "B" << std::endl;
    }
};

class C : public A, public B {
public:
    void printC() {
        A::print(); // Specify which base class's print to call
        B::print();
    }
};

int main() {
    C c;
    c.printC(); // Output: A B

    return 0;
}

3. Hierarchical Inheritance


Hierarchical inheritance involves multiple classes inheriting from the same base class.

class Shape {
public:
    virtual double area() const = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    Circle(double r) : radius(r) {}

    double area() const override {
        return 3.14159 * radius * radius;
    }

private:
    double radius;
};

class Square : public Shape {
public:
    Square(double s) : side(s) {}

    double area() const override {
        return side * side;
    }

private:
    double side;
};

int main() {
    Circle circle(5.0);
    Square square(4.0);

    Shape* shape1 = &circle;
    Shape* shape2 = &square;

    std::cout << "Circle area: " << shape1->area() << std::endl;
    std::cout << "Square area: " << shape2->area() << std::endl;

    return 0;
}

Output:

Circle area: 78.5398
Square area: 16

Inheritance enables you to create more structured and organized code by reusing and extending existing classes. It facilitates the modeling of real-world relationships and hierarchies in your programs. By using inheritance carefully and appropriately, you can design more efficient and maintainable software systems.

Leave a Reply