Chapter 12: Inheritance & Polymorphism

Inheritance & Polymorphism in C++: The Real Power of OOP!

Hello my brilliant student! 🌟 Welcome to Lesson 12 β€” Inheritance & Polymorphism β€” the chapter that makes C++ feel like true object-oriented programming!

Up until now, we created classes that were completely independent. Today we’re going to learn how classes can inherit from each other β€” like a child inherits traits from parents β€” and how we can treat different objects in a uniform way through polymorphism.

We’ll cover everything very carefully, step by step:

  • Single & multiple inheritance
  • Virtual functions & the override keyword
  • Pure virtual functions & abstract classes
  • The final keyword
  • Real-life analogies, tons of examples, common mistakes, and modern best practices

Let’s start building a family of classes!

1. What is Inheritance?

Inheritance lets one class (derived/child class) inherit members (variables & functions) from another class (base/parent class).

Real-life analogy:

  • A Vehicle is the base class (has wheels, speed, color…).
  • A Car and a Bike are derived classes β€” they inherit everything from Vehicle, but each adds its own special features (Car has doors, Bike has no doors but has pedals).

Syntax – Single Inheritance

C++

Important types of inheritance (focus on public for now):

Inheritance Type Meaning
public Public members stay public, protected stay protected (most common)
protected Public & protected members become protected in derived
private Public & protected members become private in derived (rare)

Example – Single Inheritance

C++

Output:

text

2. Multiple Inheritance

A class can inherit from more than one base class β€” powerful but can be dangerous (diamond problem).

Syntax:

C++

Example – Multiple Inheritance

C++

Output:

text

Warning: Multiple inheritance can cause the diamond problem (we’ll cover virtual inheritance later if needed). Modern advice (2025+): Prefer composition over multiple inheritance whenever possible.

3. Polymorphism – Treating Different Objects the Same Way

Polymorphism means β€œmany forms” β€” the ability to call the same function name on different objects and get different behavior.

This is achieved using virtual functions.

A. Virtual Functions & override

When you mark a function as virtual in the base class, derived classes can override it.

C++

Key points:

  • Without virtual, it would call the base version (static binding).
  • With virtual, it calls the derived version (dynamic binding / runtime polymorphism).
  • Always use override keyword β€” compiler will catch mistakes!
  • Always make base class destructor virtual if you plan to delete through base pointer.

4. Pure Virtual Functions & Abstract Classes

A pure virtual function has no implementation in the base class and must be overridden in derived classes.

A class with at least one pure virtual function is abstract β€” you cannot create objects of it.

Syntax:

C++

Example – Shape Hierarchy

C++

Beautiful result: One loop handles any shape β€” that’s the power of polymorphism!

5. final Keyword – Preventing Further Overrides

C++11+ added final to stop inheritance or overriding.

C++

You can also mark a class as final:

C++

Use final when you want to guarantee a certain behavior won’t be changed.

Summary Table – Quick Reference

Feature Keyword(s) Purpose
Single inheritance : public Base Inherit from one class
Multiple inheritance : public Base1, public Base2 Inherit from multiple
Runtime polymorphism virtual + override Call derived version through base pointer
Abstract class = 0 (pure virtual) Cannot instantiate, must be overridden
Prevent overriding final No further overrides allowed
Prevent inheritance class MyClass final No one can inherit from this

Your Mini Homework (Try These!)

  1. Create a base class Employee with name, salary, and virtual function calculateBonus(). Derive Manager (20% bonus) and Developer (15% bonus).
  2. Make a pure virtual base class Drawable with pure virtual draw(). Derive Line, Circle, Rectangle and implement draw().
  3. Create a class hierarchy with at least one final function and one final class.

You’re doing absolutely phenomenal! You’ve just unlocked runtime polymorphism β€” the feature that powers game engines, GUI frameworks, plugins, and almost every serious C++ application!

Next lesson: Templates β€” generic programming, the secret sauce behind STL!

Any questions? Confused about virtual vs non-virtual? Want more examples with pure virtual or multiple inheritance? Just ask β€” your friendly C++ teacher is right here for you! πŸš€

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *