When to use private inheritance and when composition ?

Hi guys ,
I'm wondering when should I use private inheritance and when to use Composition.
From my understanding , private inheritance seals the Derived class after it inherits privately from Base class ,meaning , that the grandson would have no access to Derived , or basically , it seals Derived from the outside world . But I still find it hard to see the difference between them , and especially when to use each of them . Can you please
explain ?

Regards ,Davi
Last edited on
closed account (1vRz3TCk)
private inheritance is a IS-IMPLEMENTED-IN-TERMS-OF relationship.

composition is either a HAS-A or IS-IMPLEMENTED-IN-TERMS-OF relationship.

Scott Meyers wrote:
When you write software, you deal with two worlds. You deal with the world you want to model, the outside world. You also deal with a world that exists only inside the software, which involves just getting the code to work. HAS-A corresponds to something in the real world. A Car HAS Wheels or a Person HAS Friends. HAS-A corresponds to the application domain. IS-IMPLEMENTED-IN-TERMS-OF never exists in the real world; it is part of the implementation domain. So you couldn't say a Car IS-IMPLEMENTED-IN-TERMS-OF Wheels. A Car HAS Wheels. But you could say the ParkingLot IS-IMPLEMENTED-IN-TERMS-OF a List. There's no List in the real world. The List only exists inside the software. So HAS-A is a relationship between classes that exists in the application domain. IS-IMPLEMENTED-IN-TERMS-OF is a relationship between classes that exists in the implementation domain.


Scott Meyers also wrote:
How are you supposed to choose between them? The answer is simple: use composition whenever you can, and use private inheritance whenever you must. When must you? Primarily when protected members and/or virtual functions enter the picture, though there's also an edge case where space concerns can tip the scales toward private inheritance.
Last edited on
Topic archived. No new replies allowed.