C++

Inheritance in C++

Inheritance is one of the four pillars of Object-Oriented Programming (OOP). It allows a new class (the Derived Class or Child) to acquire the properties and behaviors (data and methods) of an existing class (the Base Class or Parent).

This creates a hierarchical relationship and facilitates Code Reusability. Instead of writing the same code for different types of objects, you put common features in a Base class and specific features in Derived classes.

1. The Theory: The "Is-A" Relationship

Theoretically, inheritance represents an "Is-A" relationship.

  • A Car is-a Vehicle.

  • A Manager is-a Employee.

  • A Dog is-a Animal.

Key Terminology:

  1. Base Class (Parent): The existing class whose features are inherited.

  2. Derived Class (Child): The new class that inherits features from the base class. It can also have its own unique members.

2. Syntax of Inheritance

In C++, inheritance is declared using the colon (:) symbol followed by an access specifier and the name of the base class.

C++
class Base {
    // Parent members
};

class Derived : public Base {
    // Child members
};

3. Long Theory: Modes of Inheritance and Access Control

C++ provides three modes of inheritance that control how the members of the base class appear in the derived class. This is the "Visibility" theory.

A. Public Inheritance (public)

The most common mode.

  • public members of the base class remain public in the derived class.

  • protected members remain protected.

  • Result: The "Is-A" relationship is preserved. The outside world can still see the inherited public interface.

B. Protected Inheritance (protected)

  • public and protected members of the base class both become protected in the derived class.

  • Result: The outside world cannot access inherited members, but future children of the derived class can.

C. Private Inheritance (private)

  • All members of the base class become private in the derived class.

  • Result: The inheritance stops here. No further child classes can access the base class features.

D. The protected Access Specifier (Crucial Theory)

private members of a base class are never accessible to a derived class. To allow a child class to access data but keep it hidden from the main() function, we use protected.

4. Types of Inheritance (The Architecture)

  1. Single Inheritance: One child inherits from one parent.

  2. Multiple Inheritance: One child inherits from two or more parents (e.g., a TA inherits from both Student and Teacher).

  3. Multilevel Inheritance: A child inherits from a parent, who in turn inherited from a grandparent.

  4. Hierarchical Inheritance: Multiple children inherit from one single parent.

  5. Hybrid Inheritance: A combination of the above (often leading to the "Diamond Problem").

5. Practical Code Example

#include <iostream>
#include <string>
using namespace std;

// Base Class
class Animal {
protected:
    string species;

public:
    void eat() {
        cout << "This animal is eating..." << endl;
    }
};

// Derived Class
class Dog : public Animal {
public:
    void bark() {
        cout << "Woof! Woof!" << endl;
    }
    
    void setSpecies(string s) {
        species = s; // Accessible because species is 'protected'
    }

    void display() {
        cout << "I am a " << species << endl;
    }
};

int main() {
    Dog myDog;

    // Accessing method from Base class
    myDog.eat();

    // Accessing method from Derived class
    myDog.bark();

    myDog.setSpecies("German Shepherd");
    myDog.display();

    return 0;
}

6. Long Theory: Constructor and Destructor Execution Order

When inheritance is involved, the "birth" and "death" of an object follow a very specific order in memory:

  • Constructor Order: The Base class constructor runs first, followed by the Derived class constructor. Theoretically, you cannot build a child's floor until the parent's foundation is laid.

  • Destructor Order: The Derived class destructor runs first, followed by the Base class destructor. It is a "Last-In, First-Out" (LIFO) process.

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now