Static Binding (Early Binding) in C++

Static Binding(early binding) in C++

Static Binding or Early Binding is nothing but most popular compile time polymorphic technique method overloading

Definition of Static Binding: Defining multiple methods with the same name but difference in the number of arguments or the data type of arguments or ordering of arguments and resolving this method calls among multiple methods at compilation  itself is called Static Binding or Early binding or Method overloading

It is called as Static Binding or early binding because the compiler gets clarity among multiple methods, which is the exact method to be executed at compilation itself

Static Binding in C++

A simple example to understand method overloading

#include<iostream>
using namespace std;

class overloaddemo
{
public:
void add(int a,int b)
{
cout<<"2 parameter int method executed:"<<a+b<<endl;
}
//overloaded methods
void add(float a,float b)
{
cout<<"2 parameter float method executed:"<<a+b<<endl;
}

void add(int a,int b,int c)
{
cout<<"3 parameter int method executed:"<<a+b+c<<endl;
}

void add()
{
int a ,b;
cout<<"enter 2 numbers:";
cin>>a>>b;
cout<<"zero-parameter add method executed:"<<a+b<<endl;
}

void add(char a,int b)
{
cout<<"1char,1int method executed:"<<char(a+b)<<endl;
}

};
main()
{

overloaddemo d;
d.add();//zero parameter add
d.add(66,67,68);// 3 int parameter add
d.add(66.5f,67.3f);// 2 float parameter add
d.add(66,66);//2 int parameter add
d.add('A',1);//one char one int

}

O/P

Enter two numbers:2 3 
zero-parameter add method executed:5
3 parameter int method executed:201
2 parameter float method executed: 133.8
2 parameter int method executed: 132
1char,1int method executed:B

How method overloading works

  • Here we have Defined Multiple methods with the same name add() but with the difference in the number of arguments for the data type of argument but the functionality is same
  • Whenever you call add(66,67,68) method with the object the compiler  first checks the method name and then it finds multiple methods with the same name
  • It resolves this ambiguity by checking the number of arguments or  the data type of arguments, as we have passed three integer type arguments which are matched with the method declaration so that particular method is been executed
  • The compiler gets clarity about the method that is to be executed among these multiple methods at the compilation itself hence it is called polymorphism or early binding or 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 each and every method where functionality is the same the naming burden of  remembering this 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

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 same, then it causes of ambiguity, the compiler is unable to find what method is to be executed
int add(int a,int b) 
{
return a+b;
}
int add(int a,int b) //error: Ambiguity in defining method s
{
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

Please Login/Signup to comment