Dynamic binding in C++
The binding which can be resolved by the compiler using runtime is known as static binding. For example, all the final, static, and private methods are bound at run time. All the overloaded methods are binded using static binding.
The concept of dynamic binding removed the problems of static binding.
The general meaning of binding is linking something to a thing. Here linking of objects is done. In a programming sense, we can describe binding as linking function definition with the function call.
So the term dynamic binding means to select a particular function to run until the runtime. Based on the type of object, the respective function will be called.
As dynamic binding provides flexibility, it avoids the problem of static binding as it happened at compile time and thus linked the function call with the function definition.
Use of dynamic binding
On that note, dynamic binding also helps us to handle different objects using a single function name. It also reduces the complexity and helps the developer to debug the code and errors.
How to implement dynamic binding?
The concept of dynamic programming is implemented with virtual functions.
A function declared in the base class and overridden(redefined) in the child class is called a virtual function. When we refer derived class object using a pointer or reference to the base, we can call a virtual function for that object and execute the derived class's version of the function.
Now let us see the following problem that occurs without virtual keywords.
Let us take a class A with a function final_print(), and class B inherits A publicly. B also has its final_print() function.
If we make an object of A and call final_print(), it will run of base class whereas, if we make an object of B and call final_print(), it will run of base only.
Printing from the base class Printing from the base class
Now let us solve this problem using virtual function.
Printing from the base class Printing from the derived class
Hence, dynamic binding links the function call with function definition with the help of virtual functions.