











Static Binding (Early Binding) in C++
Static Binding(early binding) in C++


What is static Binding/ Early Binding?
Static Binding or Early Binding is most popular compile-time polymorphic technique method overloading.
It is called Static Binding or early binding because
- The compiler gets clarity among multiple methods with the same names.
- Knows which is the exact method to be executed at compilation itself
The compiler binds the connection from the function call to the actual function to be called at compile time itself.
This is why its called early binding as binding happens at compile time (before runtime)


Which is why its also referred to as Static Binding
Example Static Binding caused by Method overloading
Lets have a look at the example below
// The compiler knows which function to call at compilation itself // The compiler binds the connection from function call // to the actual function to be called at compile time itself // Which is why its called as early binding as binding happens // at compile time (before runtime) #includeusing namespace std; class Numbers { public: int findSum(int val1, int val2) { return val1 + val2; } double findSum(double val1, double val2){ return val1 + val2; } int findSum(int val1, int val2, int val3) { return val1 + val2 + val3; } }; int main() { Numbers obj; cout << "Sum is " << obj.findSum(10, 20) << endl; cout << "Sum is " << obj.findSum(12.5, 28.3) << endl; cout << "Sum is " << obj.findSum(10, 20, 30) << endl; return 0; }
Output
Sum is 30
Sum is 40.8
Sum is 60
How method overloading works
- Here we have Defined Multiple methods with the same name findSum()
- obj.findSum(10, 20) calls findSum(int val1, int val2)
- obj.findSum(12.5, 28.3) calls findSum (double val1, double val2)
- obj.findSum(10, 20, 30), calls findSum(int val1, int val2, int val3)
The binding of function call via object to actual function in class happens early at the compile time, rather than Run Time. Hence called as early binding.
Since, at run time even if there are user inputs the binding won’t change for these function call and bonded signature of functions this often also referred to as Static Binding
Why should you perform overloading?
Naming burden
In some situations where we need to have same functionalities but with a different type of inputs or a different number of inputs, in that situation
If you maintain separate method names for every method where functionality is the same the naming burden of remembering these method names increases on the user
Example: In C language we have abs(), sabs(), labs(), fabs() methods, the functionality of all these methods is same, but still the user needs to remind all these method names
Code readability
Increases code readability vastly
Points to Remember:
Impact of return type:
In method overloading, there is no impact on return type, you can maintain different return type or the same return type for overloaded methods
void demo(int a){ cout << a; } int demo(){ int a = 2; return a; } int demo(char a){ return a; }
Ambiguity:
If you define methods with the same signature(same name, number of arguments, the order of argument, the data type of arguments )exactly the same, then it causes ambiguity, the compiler is unable to find what method is to be executed
int add(int a, int b){ return a + b; } // error: Ambiguity in defining methods int add(int a, int b) { return a + b; }
Ordering of arguments
void add(char a, int b){ return a + b; } void add(int a, char b){ return a + b; }
In the above example, overloading is valid because even if you have the same number of arguments but the ordering of data types is different
Login/Signup to comment