Dynamic binding in C++ is a practice of connecting the function calls with the function definitions by avoiding the issues with static binding, which occurred at build time. Because dynamic binding is flexible, it avoids the drawbacks of static binding, which connected the function call and definition at build time.
In simple terms, Dynamic binding is the connection between the function declaration and the function call.
Dynamic Binding in C++
So, choosing a certain function to run up until runtime is what is meant by the term Dynamic binding. A function will be invoked depending on the kind of item.
Usage of Dynamic Binding
It is also possible to use dynamic binding with a single function name to handle multiple objects. Debugging the code and errors is also made easier and complexity is reduced with the help of Dynamic Binding.
Static Binding Vs Dynamic Binding
Static Binding
| Dynamic Binding
|
---|
It takes place at compile time which is referred to as early binding | It takes place at runtime so it is referred to as late binding |
Execution of static binding is faster than dynamic binding because of all the information needed to call a function. | Execution of dynamic binding is slower than static binding because the function call is not resolved until runtime. |
It takes place using normal function calls, operator overloading, and function overloading. | It takes place using virtual functions |
Real objects never use static binding | Real objects use dynamic binding |
Virtual functions
A virtual function is a member function declared in a base class and re-declared in a derived class (overridden). You can execute the virtual function of the derived class when you refer to its object using a pointer or reference to the base class. The concept of dynamic binding is implemented with the help of virtual functions.
Example:
C++
// C++ Program to Demonstrate the implementation of Dynamic
// Binding without the help of Virtual Function
#include <bits/stdc++.h>
using namespace std;
class GFG {
public:
void Add(int gfg1, int gfg2) // Function Definition
{
cout << gfg1 + gfg2 << endl;
return;
}
// Function Definition
void Sub(int gfg1, int gfg2) { cout << gfg1 - gfg2; }
};
int main()
{
GFG gfg;
gfg.Add(10, 12);
gfg.Sub(12, 10);
return 0;
}
Example:
C++
// C++ Program to Demonstrate the Concept of Dynamic binding
// with the help of virtual function
#include <iostream>
using namespace std;
class GFG {
public:
// function that call print
void call_Function() { print(); }
// the display function
virtual void print()
{
cout << "Printing the Base class Content" << endl;
}
};
// GFG2 inherited publicly
class GFG2 : public GFG {
public:
void print() // GFG2's display
{
cout << "Printing the Derived class Content"
<< endl;
}
};
int main()
{
GFG geeksforgeeks; // Creating GFG's object
geeksforgeeks.call_Function(); // Calling call_Function
GFG2 geeksforgeeks2; // calling GFG2
geeksforgeeks2.call_Function();
return 0;
}
OutputPrinting the Base class Content
Printing the Derived class Content
Similar Reads
Dynamic _Cast in C++ In C++, dynamic_cast is a cast operator that converts data from one type to another type at runtime. It is mainly used in inherited class hierarchies for safely casting the base class pointer or reference to derived class (called downcasting). To work with dynamic_cast, there must be one virtual fun
5 min read
Early binding and Late binding in C++ Binding refers to the process of converting identifiers (such as variable and performance names) into addresses. Binding is done for each variable and functions. For functions, it means that matching the call with the right function definition by the compiler. It takes place either at compile time o
2 min read
Avoid Bugs Using Modern C++ C++ has more constructs that can cause undefined behavior or exceptions as compared to languages like Java and Python. This is because C++ was developed with a primary objective to include classes in C and hence, support bug-inviting features such as pointers, macros, etc. It is also devoid of tools
3 min read
Naming Convention in C++ Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
6 min read
How to Create a Dynamic Library in C++? In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the
4 min read
Object Slicing in C++ When a derived class object is assigned to a base class object in C++, the derived class object's extra attributes are sliced off (not considered) to generate the base class object; and this whole process is termed object slicing. In simple words, when extra components of a derived class are sliced
4 min read