CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions
Recap of lecture 16
• Polymorphism
• Pointer to derived type
• Virtual function
• VTABLE, VPTR
• Inheritance and VTABLE
Outline of Lecture 17
• Abstract base class
• Object slicing
• Overloading, overriding
• Constructor and destructors
• Multiple dispatching
• Downcasting
Abstract base class
• Base class to present only an interface for its derived classes
• No object is created of the base class
• This is accomplished by making that class abstract
• pure virtual function.
• It uses the virtual keyword and is followed by = 0
virtual type func-name(parameter-list) = 0;
Pure virtual definitions
• It’s possible to provide a definition for a pure virtual function
• The compiler will not allow objects of that abstract base class
• Pure virtual functions must still be defined in derived classes in order to create objects
• Demonstration
Object slicing
• Passing the addresses of objects and passing objects by value
• In case polymorphism there is a distinct difference
• Upcast to an object instead of a pointer or reference
• The object is “sliced”
• Demonstration
Overloading vs. Overriding
• Redefining an overloaded function in the base class
• Virtual functions: the behaviour is a little different.
• Compiler will not allow you to change the return type
• Demonstration
Virtual functions & constructors
• VPTR must be initialized to point to the proper VTABLE
• The constructor has the job of bringing an object into existence
• The constructor’s job to set up the VPTR
• Compiler inserts code into the beginning of the constructor
• Constructor will include the proper VPTR initialization code
• There are several implications
Implication 1: Efficiency
• Inline functions
• It initialize the VPTR, it must also check the value of this
• Calls base class constructor
• Code size can grow without any benefits in speed
Implication 2: Order of constructor calls
• The order of constructor calls and the way virtual calls are made within constructors
• Base-class constructors are always called in the constructor for an inherited class
• Compiler enforces a constructor call for every portion of a derived class
• Calling virtual function from the constructor
Virtual function & destructors
• Constructor assembles the object parts, while destructor disassembles it
• The disassembling process happens in the reverse order of that of the assembly
• A derived object can be manipulated using base class pointer
• The problem occurs when a pointer of this type is to be deleted for an object that has been created on the heap with new
• Demonstration
Operator overloading
• Operator functions can be made virtual
• Implementing virtual operators often becomes confusing
• Operating on two objects, both with unknown types
• Multiple dispatching
• Demonstration
Downcasting
• Downcasting: to move down a hierarchy
• There are usually several possibilities that you could cast to
• Dilemma is figuring out a way to safely downcast
• Type-safe downcast operation: dynamic_cast
• dynamic_cast uses information stored in the VTABLE to determine the actual type
• Demonstration
Summary
• Polymorphism C++ Virtual Function “Different forms”
• Polymorphism is a feature that cannot be viewed in isolation
• If it isn’t late binding, it isn’t polymorphism