Referring to an Object as Its Class or Its Superclass

A look at the new main.cpp in Listing 22.3 shows that a reference to a superclass can refer to a descendant class as a result of inheritance.

Listing 22.3. main.cpp Using the New Classes
 *1: #include "PersistentTapeExternalInterfaceModule.h"
  2: #include "AccumulatorModule.h"
 *3: #include "PersistentTapeModule.h"
  4: #include "ControllerModule.h"
  5:
  6: int main(int argc, char* argv[])
  7: {
 *8:    SAMSCalculator::aPersistentTapeExternalInterface ExternalInterface(argv[1]);
  9:    SAMSCalculator::anAccumulator Accumulator;
*10:    SAMSCalculator::aPersistentTape Tape(argv[1]);
 11:
*12:    SAMSCalculator::aController Calculator
*13:       (
*14:          ExternalInterface,
*15:          Accumulator,
*16:          Tape
*17:       );
 18:
 19:    return Calculator.Operate();
 20: }

Lines 1 and 3 include the new modules for the derived classes.


Lines 8 and 10 now define instances of aPersistentTape and aPersistentTapeExternalInterface in place of the old aTape and anExternalInterface variables. These variables use the special new constructors offered by the derived classes.

If you look back at Figure 22.1, you will notice that aController has not been changed. It still expects aTape and anExternalInterface references in its constructor's arguments, and it stores those references as member variables for use when Operate() is called.

This demonstrates a special feature of inheritance called class polymorphism. Class polymorphism means that a descendant class reference can be used anywhere a superclass reference can be used. For instance, an aPersistentTape object can be passed through an aTape reference argument and can be assigned to an aTape reference variable. As you will see, this does not mean that the object loses the capabilities added by the descendant. However, it does mean that those parts of the program that do not use the new public members of the derived class do not need to be changed. This reduces the risk of introducing errors during maintenance considerably.

Caution

Class polymorphism depends on the use of pointers or references. This is one of the reasons it is critical to avoid passing an actual class object as an argument and to avoid storing a copy of an instance. Where possible, use a reference.


Polymorphism

in C++, this refers to the ability to have many implementations with the same name. C++ supports three types of polymorphism: method polymorphism (overloaded functions), operator polymorphism (overloaded operators), and class polymorphism, which is the ability to reference a descendant class as if it were its superclass.


Class polymorphism works because a derived class can do everything a superclass can do—and more.

Levels of Inheritance

Keep in mind that there may be more than one level of derived class. C++ does not restrict the degree to which a class can be inherited, and it is not unusual for a mature class library to have four or five levels of classes inheriting from a superclass. In addition, more than one class can inherit from the same superclass. For this reason, programmers sometimes refer to the pattern of inheritance as the inheritance tree. It is an upside-down tree with the root at the top, as shown in Figure 22.2.

Figure 22.2. An inheritance tree.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.100.202