C++20 sections · 1024 units
Open in Course

When to Use Inheritance vs Composition

Design decision guidelines

Use inheritance for "is-a" relationships: a dog is an animal. Use composition for "has-a" relationships: a car has an engine. Inheritance creates tight coupling. Changes to the base class ripple to all derived classes.

Composition gives you flexibility: swap out the engine without changing the car's interface. Favor composition over inheritance unless polymorphism or shared implementation makes inheritance the clear choice.