Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 13 of 441 min read6 questions

Inheritance

is-a reuse; base subobject first; watch object slicing.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. Derived class takes on base members; code reuse + models is-a (Audi is-a Car). Contrast has-a = composition.
  2. A derived object contains a base subobject (base part laid out first); construction base→derived, destruction derived→base.
  3. Modes: public (true is-a), protected, private (default for class).
  4. protected member = accessible to the class's own methods AND derived classes, not outside.
  5. Object slicing: assigning derived → base by value copies only the base part → use base pointers/references for polymorphism.
Watch out

Easily confused

is-a vs has-a
is-a = inheritance (Car is-a Vehicle). has-a = composition (Car has-an Engine as a member). Prefer composition unless it's truly is-a.
protected vs private
private = only this class. protected = this class and its derived classes ('family').
slicing vs polymorphism
Copy a derived into a base BY VALUE → derived part sliced off. Use a base POINTER/REFERENCE to keep the full object and get polymorphism.

Test yourself

6 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.