The process where the compiler determines the binding during runtime is referred to as static binding. For instance, methods that are final, static, or private are resolved at runtime. Static binding is also applicable to overloaded methods.
The idea of dynamic binding resolved the issues associated with static binding.
Dynamic binding
The basic concept of binding involves associating one entity with another. This process establishes connections between different elements. From a programming perspective, binding can be defined as connecting a function's definition to its corresponding function call.
The concept of dynamic binding refers to the process of determining a specific function to execute during runtime. Depending on the object's type, the corresponding function will be invoked.
Due to its flexibility, dynamic binding helps prevent the issue of static binding which occurs during compile time and associates the function call with the function definition.
Use of dynamic binding
In addition, dynamic binding enables the management of diverse objects through a unified function name. This approach simplifies complexities and aids developers in effectively debugging code and identifying errors.
How to implement dynamic binding?
The idea of dynamic programming is executed using virtual methods.
Virtual functions
A method defined in the parent class and redefined in the subclass is known as a virtual function. When we access an object of the derived class using a pointer or reference to the base class, we can invoke a virtual function for that object and run the subclass's implementation of the function.
Characteristics
- Run time function resolving
- Used to achieve runtime polymorphism
- All virtual functions are declared in the base class
- Assurance of calling correct function for an object regardless of the pointer(reference) used for function call.
- A virtual function cannot be declared as static
- No virtual constructor exists but a virtual destructor can be made.
- Virtual function definition should be the same in the base as well as the derived class.
- A virtual function can be a friend of another class.
- The definition is always in the base class and overrides in the derived class
Now, let's examine the ensuing issue that arises in the absence of virtual keywords.
Example
Let's consider a class named A containing a method named finalprint, and class B inheriting publicly from class A. Class B also possesses its own finalprint method.
If an instance of A is created and the finalprint method is invoked, it will execute the method from the base class. Conversely, when an instance of B is instantiated and finalprint is called, it will only execute the method from the base class.
#include <iostream>
using namespace std;
class A {
public:
void final_print() // function that call display
{
display();
}
void display() // the display function
{
cout<< "Printing from the base class" <<endl;
}
};
class B : public A // B inherit a publicly
{
public:
void display() // B's display
{
cout<< "Printing from the derived class" <<endl;
}
};
int main()
{
A obj1; // Creating A's pbject
obj1.final_print(); // Calling final_print
B obj2; // calling b
obj2.final_print();
return 0;
}
Output
Printing from the base class
Printing from the base class
Now, we will address this issue by employing virtual functions.
#include <iostream>
using namespace std;
class A {
public:
void final_print() // function that call display
{
display();
}
virtual void display() // the display function
{
cout<< "Printing from the base class" <<endl;
}
};
class B : public A // B inherit a publicly
{
public:
virtual void display() // B's display
{
cout<< "Printing from the derived class" <<endl;
}
};
int main()
{
A obj1; // Creating A's pbject
obj1.final_print(); // Calling final_print
B obj2; // calling b
obj2.final_print();
return 0;
}
Output
Printing from the base class
Printing from the derived class
Therefore, dynamic binding connects the function call to the function definition using virtual functions.