A crisis
Of course, any
upcast loses type information about an object. If you
say
Wind w;
Instrument* ip = &w;
the compiler can deal with ip
only as an Instrument pointer and nothing else. That is, it
cannot know that ip actually happens to point to a Wind
object. So when you call the play( ) member function by saying
ip->play(middleC);
the compiler can know only that
it’s calling play( ) for an Instrument pointer, and
call the base-class version of Instrument::play( ) instead of what
it should do, which is call Wind::play( ). Thus, you won’t get
the correct behavior.
This is a significant problem; it is
solved in Chapter 15 by introducing the third cornerstone of object-oriented
programming: polymorphism (implemented in C++ with virtual
functions).