What are early binding and late binding functions in C++?

When we compile our program, all variables and functions are mapped to addresses in memory. These addresses are then used for reference in the program. This process of converting variables and functions into addresses is known as binding.

There are two types of binding. Let’s look at each one of them individually.

Early binding

Early binding is also called static binding. Early binding occurs when we make the explicit or direct function call in our program.

When the compiler encounters a function call in the program, it replaces that function call with a machine language instruction to go to that function.

Therefore, when the point of execution comes in that function call line, it reads that instruction and then jumps to the function given by memory address.

Consider the following code:

#include<iostream>
using namespace std;
void display(int x){
cout << x;
}
int main(){
display(5);
return 0;
}

Late binding

Late binding is also called dynamic binding. Late binding occurs when we make implicit or indirect function calls in our program. An example of this is using function pointers or virtual functions when using classes. Here, the function call’s memory reference is not determined at compile-time, but rather at run-time.

A function pointer is a pointer that stores the memory address of a function instead of a variable.

Consider the following code:

#include<iostream>
using namespace std;
void display(int x){
cout << x;
}
int main(){
void (*ptrFunc)(int) = display;
ptrFunc(5);
return 0;
}

In this example, the compiler does not know which function is stored in that function pointer until the runtime, and therefore cannot do the early binding.

In this case, the compiler converts the function pointer into a machine language instruction and stores it in the next sequential available address.

When the line of execution comes at that machine language instruction, it goes to the memory address of that function pointer. The line of execution dereferences it, then makes the function call and goes to the body of the function

Consider the following code to understand the late binding with a virtual function:

#include<iostream>
using namespace std;
class parent {
public:
virtual void DisplayMessage()
{
cout << "Virtual function from parent class."<<endl;
}
};
class child : public parent {
public:
void DisplayMessage()
{
cout << "Child class override virtual function. "<<endl;
}
};
int main()
{
parent *ptr = new child;
ptr->DisplayMessage();
return 0;
}

To learn virtual functions in detail, please visit this Answer.

Summary

If the compiler knows at the compile-time which function is called, it is called early binding.

If a compiler does not know at compile-time which functions to call up until the run-time, it is called late binding.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved