Chapter 9
Chapter 9
Pointers, Virtual
Functions and
Polymorphism
The overloaded member functions are ‘selected’ for invoking
by matching arguments, both type and number. This
information is known to the compiler at the compile time and,
therefore, compiler is able to select the appropriate function
for a particular call at the compile time itself. This is called
early binding or static binding or static linking. Also known as
compile time polymorphism, early binding simply means that
an object is bound to its function call at compile time.
Example:
int (*num_function(int x));
When we use the same function name in both the base and
derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal
declaration. When a function is made virtual, C++
determines which function to use at run time based on the
type of object pointed to by the base pointer, rather than the
type of the pointer. Thus, by making the base pointer to point
to different objects, we can execute different versions of the
virtual function.