Home Tutorials C++ Tutorial Inheritance
Inheritance

Inheritance


C++ Inheritance

Definition: Inheritance is a core feature of Object-Oriented Programming that allows a new class (known as the Derived or Child Class) to inherit the attributes and methods of an existing class (known as the Base or Parent Class). This creates a hierarchical relationship between classes, similar to a family tree.

Why: Inheritance is included in beginner C++ OOP tracks because it is the primary tool for code reusability. Instead of writing the same code for multiple related classes, you can define common features in a base class and let specialized classes inherit them, making your software easier to maintain and extend.


Hierarchy: Base vs. Derived Classes

In C++, we use the colon (:) symbol to indicate that a class is inheriting from another. The public keyword used during inheritance ensures that the public members of the base class remain public in the derived class.

Base Class (Parent)

The class whose properties are inherited. It contains general features shared by many related objects (e.g., Animal).

Derived Class (Child)

The class that inherits from the base class. it can add its own unique features or override existing ones (e.g., Dog).

Example: Inheriting Animal Behaviors

In this example, the Dog class is empty, yet it can still perform the sound() action because it inherits that capability directly from the Animal class:

#include <iostream>
using namespace std;

// Base class (parent)
class Animal {
  public:
    void sound() {
        cout << "The animal makes a sound" << endl;
    }
};

// Derived class (child) inheriting from Animal
class Dog : public Animal {
    // Dog inherits sound() automatically
};

int main() {
    Dog d;
    
    // Calling inherited method
    d.sound(); // Outputs: The animal makes a sound
    
    return 0;
}

Types of Inheritance

Type Description
Single Inheritance A derived class with only one base class (as shown in the example).
Multilevel Inheritance A class is derived from a class which is also derived from another class (Grandparent → Parent → Child).
Multiple Inheritance A derived class can inherit from more than one base class simultaneously (e.g., a SmartPhone inheriting from Camera AND Phone).
Hierarchical Inheritance Multiple derived classes inherit from a single base class (e.g., both Dog and Cat inheriting from Animal).

Key Notes

  • Access to Private Members: Derived classes cannot access the private members of a base class. If you want a child class to access parent data while keeping it hidden from the rest of the program, use the protected access specifier.
  • Constructors in Inheritance: When you create an object of a derived class, the base class constructor is called first, followed by the derived class constructor.
  • Method Overriding: A child class can provide its own specific version of a function already defined in the parent class. For example, Dog could have its own sound() function that prints "Woof!" instead of "Animal sound."
  • Memory Efficiency: Inheritance allows you to build complex systems by simply "adding on" to existing, tested code rather than starting from scratch, which significantly reduces bugs.

🏋️ Test Yourself With Exercises

Take our quiz on Inheritance to test your knowledge.

Browse Quizzes »