Chapter 10 Virtual functions and polymorphism 1. With virtual functions and polymorphism, it becomes possible to design and implement systems that are more easily extensible. Programs can be written to process objects of types that may not exist when the program is under development. 2. Polymorphic programming with virtual functions can eliminate the need for switch logic. The programmer can use the virtual function mechanism to perform the equivalent logic automatically, thus avoiding the kinds of errors typically associated with switch logic. Client code making decisions about object types and representations indicates poor class design. 3. Derived classes can provide their own implementations of a base class virtual function if necessary, but if they do not, the base class's implementation is used. 4. C++ enables polymorphism -- the ability for objects of different classes related by inheritance to respond differently to the same function call. 5. Polymorphism is implemented via virtual functions. 6. Through the use of virtual functions and polymorphism, one member function call can cause different actions depending on the type of object receiving the call. 7. When a base class provides a virtual member function, derived classes can override the virtual function, but they do not have to override it. Thus a derived class can use a base class's version of a virtual member function, and this would be indicated in the vtable. 8. Polymorphism as implemented with virtual functions and dynamic binding is efficient. 9. Virtual functions and dynamic binding enable polymorphic programming as opposed to switch logic programming. 10. An interesting consequence of using virtual functions and polymorphism is that programs take on a simplified appearance. They contain less branching logic in favor of simpler sequential code. This facilitates testing, debugging, program maintenance and bug avoidance. 11. When a derived class chooses not to define a virtual function, the derived class simply inherits its immediate base class's virtual function definition. 12. With virtual functions and polymorphism, the programmer can deal in generalities and let the execution-time environment concern itself with the specifics. The programmer can command a wide varirty of objects to behave in manners appropriate to those objects without even knowing the types of those objects. 13. Polymorphism promotes extensibility: software written to invoke polymorphic behavior is written independently of the types of the objects to which messages are sent. Thus new types of objects that can respond to existing messages can be added into such a system without modifying the base system. Except for client code that instantiates new objects, programs need not be recompiled.