Sams Teach Yourself C++ in 21 Days

(singke) #1
Figure 11.16 models composition. This model says that the body is not only an aggrega-
tion of a head, two arms, and two legs, but that these objects (head, arms, legs) are cre-
ated when the body is created and disappear when the body disappears. That is, they
have no independent existence; the body is composed of these things and their lifetimes
are intertwined.

360 Day 11


FIGURE11.16
Composition. Body

Head Arms Legs

1
2

2

Discriminators and Powertypes How might you design the classes required to
reflect the various model lines of a typical car manufacturer? Suppose that you’ve been
hired to design a system for Acme Motors, which currently manufactures five cars: the
Pluto (a slow, compact car with a small engine), the Venus (a four-door sedan with a
middle-sized engine), the Mars (a sport coupe with the company’s biggest engine, engi-
neered for maximum performance), the Jupiter (a minivan with the same engine as the
sports coupe but designed to shift at a lower RPM and to use its power to move its
greater weight), and the Earth (a station wagon with a small engine but high RPM).
You might start by creating subtypes of car that reflect the various models, and then cre-
ate instances of each model as it rolls off the assembly line, as shown in Figure 11.17.

FIGURE11.17
Modeling subtypes.

Car

Earth Jupiter Mars Venus Pluto
Free download pdf