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.
Theoretically, inheritance represents an "Is-A" relationship.
A Car is-a Vehicle.
A Manager is-a Employee.
A Dog is-a Animal.
Base Class (Parent): The existing class whose features are inherited.
Derived Class (Child): The new class that inherits features from the base class. It can also have its own unique members.
In C++, inheritance is declared using the colon (:) symbol followed by an access specifier and the name of the base class.
class Base { // Parent members }; class Derived : public Base { // Child members };
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.
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.
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.
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.
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.
Single Inheritance: One child inherits from one parent.
Multiple Inheritance: One child inherits from two or more parents (e.g., a TA inherits from both Student and Teacher).
Multilevel Inheritance: A child inherits from a parent, who in turn inherited from a grandparent.
Hierarchical Inheritance: Multiple children inherit from one single parent.
Hybrid Inheritance: A combination of the above (often leading to the "Diamond Problem").
#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; }
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.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION