Skip to content
Related Articles

Related Articles

Dynamic Binding in C++

Improve Article
Save Article
  • Last Updated : 06 Oct, 2022
Improve Article
Save Article

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++

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;
}


Output

22
2

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
    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 pbject
    geeksforgeeks.call_Function(); // Calling call_Function
    GFG2 geeksforgeeks2; // calling GFG2
    geeksforgeeks2.call_Function();
    return 0;
}


Output

Printing the Base class Content
Printing the Base class Content

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!