Powertypes enable you to create a variety of logicaltypes without using inheritance. You
can thus manage a large and complex set of types without the combinatorial explosion
you might encounter with inheritance.
Typically, you implementthe powertype in C++ with pointers. In this case, the Carclass
holds a pointer to an instance of PerformanceCharacteristicsclass (see Figure 11.20).
I’ll leave it as an exercise to the ambitious reader to convert the body and engine discrim-
inators into powertypes.
362 Day 11
FIGURE11.19
A discriminator as a
powertype.
Car
High Power Sedan Coupe Family Car
Low Power Sports Car
engine performance:PerformanceCharacteristics
body Performance Characteristics<<powertype>>
shift Pointmax RPM
accelerate
Family:PerformanceCharacteristics
Sport:PerformanceCharacteristics
Keep in mind that the practice of creating new types in this way at runtime
can reduce the benefits of C++ strong typing, in which the compiler can
enforce the correctness of interclass relationships. Therefore, use it carefully.
CAUTION
FIGURE11.20
The relationship
between a Carobject
and its powertype.
Car
High Power Sedan Coupe
Low Power
engine
body
Performance Characteristics
shift Point
max RPM
accelerate
Class Car : public Vehicle
{
public:
Car();