How do composition, non-owning aggregation, and inheritance differ in ownership, lifetime, and substitutability?
Show answer and explanation
Composition represents an owning has-a relationship: a value member or owning smart pointer makes the containing object responsible for the component's lifetime. Aggregation is a modeling term rather than a distinct C++ feature and commonly uses a reference, pointer, or view to observe an independently owned object, so its lifetime contract must be explicit. Public inheritance represents an is-a relationship and requires substitutability under the base contract; use it for genuine polymorphism, and prefer composition when the goal is only to reuse or combine implementation.
Composition represents an owning has-a relationship: a value member or owning smart pointer makes the containing object responsible for the component's lifetime. Aggregation is a modeling term rather than a distinct C++ feature and commonly uses a reference, pointer, or view to observe an independently owned object, so its lifetime contract must be explicit. Public inheritance represents an is-a relationship and requires substitutability under the base contract; use it for genuine polymorphism, and prefer composition when the goal is only to reuse or combine implementation.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.