TITLE: Choosing inheritance (is-a) versus containment (uses) [ Tip adapted from "Inheritance Versus Containment", Rainer H. Liffers, ACM SigPlan Notices, V. 28, No. 9, Sept 93, p. 36 Designers using C++ sometimes have confusion about whether the relationship between two classes should be is-a or has-a. This problem is exacerbated because of the ability to use non-virtual multiple inheritance to mimic containment [ugh]. class Robot : public Arm, public Arm, public Leg, public Leg {...} Below is a simple guideline you can use to help determine which relationship you should use. -adc ] Let X be a class for which it is not clear whether it should be derived from class Y or it should contain a member of class Y. Assume further that there are at least two different instances of both X and Y. Then, for any instance of X, if we exchange the current Y component against a different instance of Y, and we regard the new instance of X as being identical to the instance of X before the exchange, then the correct relationship between X and Y is the has-a relationship, and otherwise it is the is-a relationship. Example 1: If, for a specific instance of class Human of which class Heart is a component, we exchange the current instance of class Heart against a different instance, then we would still regard the new instance of Human as being identical to the instance before the exchange. Therefore, a Human "has a" Heart, and it is not the case that a Human "is a" Heart. Example 2: If, for a specific instance of class Human of which class Primate is a component, we exchange the current instance of class Primate against a different instance, then the Human instance would lose its identity. Therefore, a Human "is a" Primate, and it is not the case that a Human "has a" Primate. [ Of course, no guideline is razor-sharp. If we exchange a Brain instead of a Heart, we might be a little fuzzy in answering the question of identity. This particular problem is due more to a "resemblence relationship" than an "identity relationship". -adc ]