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.CarHigh Power Sedan Coupe Family CarLow Power Sports Carengine performance:PerformanceCharacteristics
body Performance Characteristics<<powertype>>
shift Pointmax RPM
accelerate
Family:PerformanceCharacteristicsSport:PerformanceCharacteristicsKeep 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.CAUTIONFIGURE11.20
The relationship
between a Carobject
and its powertype.CarHigh Power Sedan CoupeLow Powerengine
bodyPerformance Characteristics
shift Point
max RPM
accelerateClass Car : public Vehicle
{
public:
Car();