Polymorphism
–
implemented in C++ with virtual functions – means “different
forms.” In object-oriented programming, you have the same face (the
common interface in the base class) and different forms using that face: the
different versions of the virtual functions.
You’ve
seen in this chapter that it’s impossible to understand, or even create,
an example of polymorphism without using data abstraction and inheritance.
Polymorphism is a feature that cannot be viewed in isolation (like
const
or a
switch
statement, for example), but instead works only in concert, as part of a
“big picture” of class relationships. People are often confused by
other, non-object-oriented features of C++, like overloading and default
arguments, which are sometimes presented as object-oriented. Don’t be
fooled: If it isn’t late binding, it isn’t polymorphism.
To
use polymorphism, and thus object-oriented techniques, effectively in your
programs you must expand your view of programming to include not just members
and messages of an individual class, but also the commonality among classes and
their relationships with each other. Although this requires significant effort,
it’s a worthy struggle, because the results are faster program
development, better code organization, extensible programs, and easier code
maintenance.
Polymorphism
completes the object-oriented features of the language, but there are two more
major features
in C++: templates (Chapter XX), and exception handling (Chapter XX). These
features provide you as much increase in programming power as each of the
object-oriented features: abstract data typing, inheritance, and polymorphism.