Study Guide · 08
Inheritance
The "is-a" relationship — building specialised classes from a base
1
Notes
2
Videos by Dr. Ibrahim Albluwi
3
Code examples
4
Lab walkthroughs
Key Concepts
This page is a short summary. The full notes are in Inheritance.pdf on Google Drive.
Summary
01 · What is inheritance?
- Software reuse: a derived (child) class absorbs a base (parent) class's data members and functions, then adds its own.
- It models IS-A — every Student is also a Person. HAS-A (a Car has an Engine) is composition, not inheritance.
- Single (Dog → Animal), multiple (SmartBoat → Boat, ComputerSystem) and multilevel (BabyDog → Dog → Animal).
- Syntax:
class Student : public Person { … };
02 · Access control
- Private: only the class. Protected: the class and its derived classes. Public: everyone.
- Base private members are never directly accessible in a derived class — go through public/protected setters and getters.
- Public inheritance keeps public and protected as they are; protected inheritance makes both protected; private inheritance makes both private.
03 · Constructors & destructors
- Neither is inherited.
- Constructors run base → derived. Call a parameterised base constructor in the initialiser list:
Student() : Person("Ali") { } - If you don't, the base default constructor is called — a compile error if the base only has parameterised ones.
- Destructors run the other way:
~Derivedfirst, then~Base.
04 · Function overriding
05 · Multiple inheritance
Extra Notes · beyond the PDF
Good to know
Common mistakes
- Not calling a parameterised base constructor when the base has no default constructor — compile error.
- Initialising base members in the derived initialiser list (
Student() : name_("Ali")) — call the base constructor instead. - Using inheritance for HAS-A relationships — that's composition.
- Redefining a function with a different parameter list hides every base version with that name.