C++ Core
Inheritance
is-a reuse; base subobject first; watch object slicing.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Derived class takes on base members; code reuse + models is-a (Audi is-a Car). Contrast has-a = composition.
- A derived object contains a base subobject (base part laid out first); construction base→derived, destruction derived→base.
- Modes:
public(true is-a),protected,private(default forclass). protectedmember = accessible to the class's own methods AND derived classes, not outside.- 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.