Replacement in C++ - class descriptions

Here is an example class definition from the C++ version of the solitare program described in Chapter 8. The class CardPile represents a pile of card. The class SuitPile inherits from CardPile, and represents a specialized type of pile. The method canTake tells whether or not the pile can take the argument card. If we declare this method as virtual, then message passing will invoke the function based on the dynamic, run-time value of a variable, and not the declared type. But, this is true only if we use a pointer or a reference for the receiver. If a variable is declared simply as CardPile (that is, not as a pointer or a reference) then it will always execute the code in class CardPile, whether or not it is declared as virtual.

Thus, to obtain the effect of a polymorphic message binding, there are two things a programmer must do. First, they must use the virtual keyword. Second, they must access the value by means of a pointer or a reference.

[audio] [real] Text to accompany slide8, in Chapter 11 of An Introduction to Object-Oriented Programming