This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: [ Viewing Hints ] [ Exercise Solutions ] [ Volume 2 ] [ Free Newsletter ] [ Seminars ] [ Seminars on CD ROM ] [ Consulting ] Thinking in C++, 2nd ed. Volume 1 2000 by Bruce Eckel [ Previous Chapter ] [ Table of Contents ] [ Index ] [ Next Chapter ] 15: Polymorphism &amp; Virtual Functions Polymorphism (implemented in C++ with virtual functions) is the third essential feature of an object-oriented programming language, after data abstraction and inheritance. It provides another dimension of separation of interface from implementation, to decouple what from how . Polymorphism allows improved code organization and readability as well as the creation of extensible programs that can be grown not only during the original creation of the project, but also when new features are desired. Encapsulation creates new data types by combining characteristics and behaviors. Access control separates the interface from the implementation by making the details private . This kind of mechanical organization makes ready sense to someone with a procedural programming background. But virtual functions deal with decoupling in terms of types . In Chapter 14, you saw how inheritance allows the treatment of an object as its own type or its base type. This ability is critical because it allows many types (derived from the same base type) to be treated as if they were one type, and a single piece of code to work on all those different types equally. The virtual function allows one type to express its distinction from another, similar type, as long as theyre both derived from the same base type. This distinction is expressed through differences in behavior of the functions that you can call through the base class. In this chapter, youll learn about virtual functions, starting from the basics with simple examples that strip away everything but the virtualness of the program. Evolution of C++ programmers C programmers seem to acquire C++ in three steps. First, as simply a better C, because C++ forces you to declare all functions before using them and is much pickier about how variables are used. You can often find the errors in a C program simply by compiling it with a C++ compiler. The second step is object-based C++. This means that you easily see the code organization benefits of grouping a data structure together with the functions that act upon it, the value of constructors and destructors, and perhaps some simple inheritance. Most programmers who have been working with C for a while quickly see the usefulness of this because, whenever they create a...
View Full Document
- Spring '11