Early Binding vs Late Binding in C++: vtable and vptr Explained

Binding means deciding which function should be called for a particular function call. In C++, this decision can happen either at compile time or at run time.
When the function call is resolved at compile time, it is called Early Binding or Static Binding.
When the function call is resolved at run time based on the actual object type, it is called Late Binding or Dynamic Binding.

Let’s look into the differences first.

Early BindingLate Binding
Happens at compile time.Happens at run time.
Also called Static BindingAlso called Dynamic Binding
Compiler has all information to invoke correct function version at compile time.Compiler doesn’t have information to identify correct function version.
Normal function calls are example of Early Binding.Virtual functions are example of Late Binding.
Functions invoked by object is example of early bindingFunctions invoked by pointers can be example of late binding.
Early binding is efficient as no cost paid at run-time for function resolution.Late Binding is slightly costly as function resolution happens at run time.
Early binding doesn’t provide flexibility of one method multiple interfaces.Late Binding provide flexibility.
Doesn’t have any virtual table or virtual pointer.It uses Virtual Table and Virtual Pointer.
(more…)
Early Binding vs Late Binding in C++: vtable and vptr Explained Read More

Virtual Functions in C++ Explained With Simple Example

A virtual function is a member function which is defined in Base class and redefined by the derived class. “virtual” keyword must be added before the function declaration to define a function virtual. These functions gets inherited by Derived class from Base class and Derived class can choose to provide it’s own definition.
A pure virtual function is a virtual function which has no definition in Base class. Pure virtual function definition is used to ensure all derived class must override the base class function definition.

Normal syntax to define virtual function is as follows:

virtual int samplefun (int x, char c);
(more…)
Virtual Functions in C++ Explained With Simple Example Read More