Module 6 · Inheritance and Polymorphism — topic 1 of 5
Inheritance
A derived class inherits members of a base class and can extend them. Learn public inheritance, constructor chaining, protected members, and types of inheritance.
Learning objectives
- Derive classes with public inheritance
- Call base-class constructors from derived constructors
- Distinguish single, multilevel, hierarchical and multiple inheritance
Definition
Inheritance is a mechanism by which a derived class acquires the members of one or more base classes, modelling an “is-a” relationship; with public inheritance, a derived object can be used wherever a base object is expected.
Introduction
A SavingsAccount is an Account with interest; a Manager is an Employee with a team. Inheritance lets the derived class reuse everything in the base and add or adjust behaviour.
Why it matters
Inheritance is one of the four OOP pillars and the foundation for polymorphism.
Misused inheritance causes rigid designs; understanding when to use it (is-a) versus composition (has-a) is a key design skill.
Detailed explanation
class Derived : public Base { ... }; — public members of Base stay public in Derived. Protected members are accessible in Derived but not outside. Private members exist in the derived object but are not directly accessible.
Construction order: base first, then members, then the derived constructor body. Destruction is the reverse. Pass arguments to the base in the initialiser list: Derived(int x) : Base(x) {}.
Types: single (B : A), multilevel (C : B : A), hierarchical (B : A and C : A), multiple (C : A, B). Diamond shapes in multiple inheritance are resolved with virtual inheritance.
Prefer composition (a member object) when the relationship is has-a: a Car has an Engine, it is not an engine.
Syntax
- Use
publicinheritance for is-a relationships. - Base constructors are called in the initialiser list.
- Default inheritance for
classis private, forstructpublic.
Code examples
Reusing and extending
Bruno is eating Bruno fetches the ball
Construction and destruction order
Base Derived ~Derived ~Base
More examples
Multilevel inheritance with constructor chaining
Ravi (student) at 4%: 5200 5200
Step-by-step explanation
- Each constructor passes arguments up to its direct base.
StudentSavingsfixes the rate at 4%.add_interest(from SavingsAccount) updatesbalance_(from Account).describecan read protected members of both ancestors.
Expected output
Running “Multilevel inheritance with constructor chaining” prints exactly:
Practical applications
- GUI frameworks: Button is-a Widget.
- Game entities: Enemy is-a Character.
- Exception hierarchies: std::out_of_range is-a std::logic_error is-a std::exception.
Best practices
- Use public inheritance only for genuine is-a relationships.
- Keep data private and expose protected functions rather than protected data where possible.
- Prefer composition by default.
Common mistakes
Forgetting to call a base constructor that needs arguments
Avoid
Prefer
If Animal has no default constructor, Dog must pass arguments to it.
Inheriting for code reuse when it's not is-a
Avoid
Prefer
A car has an engine; use composition.
Important notes
- Converting a derived object to a base object by value 'slices' off the derived part; pass by reference or pointer to keep the full object.
- Virtual inheritance (
class B : virtual public A) shares one A sub-object in diamond hierarchies.
Key takeaways
- Derived classes inherit and extend bases.
- Bases construct first and destroy last.
- Use inheritance for is-a, composition for has-a.
Try it yourself
- 1Create
Vehicle → Car → ElectricCarwith constructors that chain correctly and a description method. - 2Rewrite a
Car : public Enginedesign using composition.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhich relationship should public inheritance model?
- BeginnerA `protected` member of a base class is accessible in…