Sams Teach Yourself C++ in 21 Days

(singke) #1
Object-Oriented Analysis and Design 359

11


It is important to come back to the fundamentals: Public inheritance should always
model generalization. The common expression for this is that inheritance should model
is-arelationships. If you want to model the has-a relationship (for example, a car has-a
steering wheel), you do so with aggregation, as shown in Figure 11.15.

FIGURE11.14
False inheritance.


Car

Steering Wheel Door Tire

FIGURE11.15
Aggregation. Car


Steering Wheel Door Tire

1
2..5

4

The diagram in Figure 11.15 indicates that a car has a steering wheel, four wheels, and
two to five doors. This is a more accurate model of the relationship among a car and its
parts. Notice that the diamond in the diagram is not filled in; this is so because this rela-
tionship is being modeled as an aggregation, not as a composition. Composition implies
control for the lifetime of the object. Although the car hastires and a door, the tires and
door can exist before they are part of the car and can continue to exist after they are no
longer part of the car.
Free download pdf