01 / 21

C++ Inheritance

Learn how to model is-a relationships using inheritance: build class hierarchies, reuse code, and write polymorphic programs in C++.

C++ Object-Oriented Programming

What Is Inheritance?

Inheritance is a fundamental mechanism in C++ that lets a new class derive from an existing class, automatically gaining its data members and member functions. The existing class is called the base class (or parent), and the new class is called the derived class (or child).

The Is-A Relationship

Inheritance models an is-a relationship: every derived object is also a base object. This is different from has-a (composition), where one object contains another.

Is-A (Inheritance)
A Car is a Vehicle. Car inherits from Vehicle.
Has-A (Composition)
A Car has an Engine. Car contains an Engine member.

Why Inheritance Matters

Code Reuse
Write common logic once in the base class. Every derived class inherits it automatically.
Extensibility
Add new derived classes without modifying existing code: open for extension, closed for modification.
Polymorphism
Treat different derived objects uniformly through a base-class pointer or reference.

The Syntax of Inheritance

class Base { public: void greet() { /* shared behavior */ } protected: int sharedData; }; class Derived : public Base { public: void special() { /* new behavior */ } };

Access Specifiers in Inheritance

Base Class public: greet(), name protected: sharedData inherits Derived Class public: greet(), name, special() protected: sharedData Visibility in Derived Class (public inheritance) public Stays public ✓ protected Stays protected ✓ private Not accessible ✕ Derived class can access public + protected, but NOT private members of Base

Public vs Protected vs Private

Base Member public inheritance protected inheritance private inheritance
public public protected private
protected protected protected private
private inaccessible inaccessible inaccessible

How to Apply Inheritance

1
Identify the is-a relationship. Ask: "Is every X naturally a Y?" If yes, Y is the base, X is derived.
2
Design the base class. Place all shared data and behavior in the base. Use protected for data that derived classes need but outsiders should not access.
3
Write the derived class. Use the colon syntax: class Derived : public Base. Add only new or overridden members.
4
Chain constructors. The derived constructor should call the base constructor in its initializer list to properly initialize inherited members.
5
Test with a base pointer. Confirm that a Base* can point to a Derived object and call inherited functions.

Example 1: Shape Hierarchy

class Shape { protected: std::string color; public: Shape(std::string c) : color(c) {} void printColor() { std::cout << "Color: " << color << "\n"; } }; class Circle : public Shape { double radius; public: Circle(std::string c, double r) : Shape(c), radius(r) {} double area() { return 3.14159 * radius * radius; } };

Example 2: Constructor Chaining

class Animal { protected: std::string name; int age; public: Animal(std::string n, int a) : name(n), age(a) { std::cout << "Animal constructed\n"; } }; class Dog : public Animal { std::string breed; public: Dog(std::string n, int a, std::string b) : Animal(n, a), breed(b) { std::cout << "Dog constructed\n"; } };

Example 3: A Vehicle Hierarchy

Vehicle speed, fuel, drive() Car doors, trunk() Truck payload, haul() Motorcycle lean, wheelie() ElectricCar battery, charge() Hierarchy reads: "ElectricCar is-a Car is-a Vehicle"

Overriding Member Functions

class Animal { public: void speak() { std::cout << "..." << std::endl; } }; class Dog : public Animal { public: void speak() { // overrides Animal::speak() std::cout << "Woof!" << std::endl; } };

Calling the Base Version

class Dog : public Animal { public: void speak() { Animal::speak(); // call base version first std::cout << "Woof!" << std::endl; } };

A Glimpse of Polymorphism

void makeItSpeak(Animal& a) { a.speak(); // which version runs? } Dog d; makeItSpeak(d); // prints "..." (Animal's version!)

Common Mistakes

✕ Using inheritance for has-a
Making Car inherit from Engine instead of containing it. A Car is not an Engine.
✕ Forgetting base constructor
Not calling the base constructor in the initializer list. The compiler may default-construct the base, leaving members uninitialized.
✕ Accessing private members
Trying to use the base's private members directly in the derived class. Use protected or public getters instead.

Inheritance Summary

Concept Key Point Example
Is-A Relationship Derived is a Base Dog is an Animal
Syntax class D : public B class Dog : public Animal
Access public and protected inherited private stays inaccessible
Constructors Base runs first, derived second D(args) : B(args) {}
Overriding Redefine base function in derived void speak() in Dog
Scope Resolution Call base version explicitly Animal::speak()

Question 1 of 5

Which keyword is used to establish a public inheritance relationship between a derived class and a base class?

Question 2 of 5

A Student class inherits from a Person class. Which members of Person can Student directly access?

Question 3 of 5

When a Dog object is created (where Dog inherits from Animal), which constructor runs first?

Question 4 of 5

A Car class has an Engine as a data member. A SportsCar class inherits from Car. Which relationship is modeled incorrectly?

Question 5 of 5

How does a derived class call the base class version of an overridden function named display()?