HkfKu3yayl

Public Inheritance

Private Inheritance

Exempting Individual Members

Summary

Inheritance Type Rule of Thumb Use Case
public Use when you want to expose the base class’s public interface as part of the derived class. “is-a” relationship: class Dog : public Animal
protected Rarely used. Use when only derived classes should access base members, but not clients. Internal derivation where public interface is not exposed.
private Use when inheritance is purely an implementation detail, not visible to the client. “implemented-in-terms-of”: e.g., stack implemented via vector: class Stack : private std::vector<T>
Composition Type Rule of Thumb Use Case
by value Use when the composed object is always required and has the same lifetime. Car has Engine (always has one)
by reference Use when the composed object is shared, externally owned, and never null. Injecting dependencies (e.g., reference to a logger)
by pointer Use when the composed object is optional, polymorphic, or shared/owned dynamically. Optional or changeable behavior: std::unique_ptr<Strategy>