Inheritance vs Composition Trade-offs
Compare inheritance and composition and pick the right tool for the design.
Inheritance vs Composition Trade-offs is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Reuse Code
You can reuse code by inheriting from another class or by composing it as a member. Both have their place.
Inheritance: "Is-a"
Use inheritance when the derived class genuinely is a special kind of the base.
class Vehicle {};
class Car : public Vehicle {}; // a Car IS a Vehicle
class SportsCar : public Car {};Composition: "Has-a"
Use composition when the relationship is "has-a" or "uses-a."
class Engine {};
class Car {
Engine engine_; // a Car HAS an Engine
};Coupling Trade-off
Inheritance creates tight coupling — derived classes depend on the base s internals. Composition keeps modules independent and easier to change.
Flexibility
Composition allows changing behavior at runtime — swap out the composed object. Inheritance binds behavior at compile time.
class Logger {
std::unique_ptr<LogSink> sink_; // can swap implementations
public:
void setSink(std::unique_ptr<LogSink> s) { sink_ = std::move(s); }
};Testing
Composed objects can be replaced with mocks for testing. Inherited base classes are harder to mock without virtual functions or templates.
The Fragile Base Class Problem
Changes in a base class can break many derived classes. Composition isolates change to the composing class.
Choose Inheritance When
Inheritance fits when:
- A clear "is-a" relationship exists
- Polymorphism through a base interface is needed
- The base class is explicitly designed for inheritance
Choose Composition When
Composition fits when:
- You want flexibility to swap implementations
- The relationship is "has-a" or "uses-a"
- You want low coupling and easier testing
The Strategy Pattern
Combine composition with polymorphism: store a base-class pointer to a strategy object that the host class composes.
class Sorter {
std::unique_ptr<SortStrategy> strategy_;
public:
void sort(std::vector<int>& v) { strategy_->execute(v); }
};Mixins and Policy Classes
For compile-time customization, use templated policy classes — composition at the type level. Powerful, zero runtime overhead.
Interface + Composition
A common modern pattern: define an interface (abstract base) and compose concrete classes that implement it. Inheritance for the interface, composition for the implementation.
Quick Check
Which approach generally gives more flexibility and looser coupling?
Recap
Inheritance models "is-a"; composition models "has-a." Composition is more flexible and less coupled — prefer it for code reuse. Use inheritance for polymorphism through an interface, often combined with composed implementations.
Frequently asked questions
Is the “Inheritance vs Composition Trade-offs” lesson free?
Yes — the full text of “Inheritance vs Composition Trade-offs” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Inheritance vs Composition Trade-offs”?
Compare inheritance and composition and pick the right tool for the design. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Inheritance vs Composition Trade-offs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Single and Multiple Inheritance
- Virtual Functions and the vtable
- Abstract Classes and Pure Virtual Methods
- Inheritance vs Composition Trade-offs