C++ – Static Polymorphism
Polymorphism: In programming language we can say : Different functions can have the same name and if programmer call a function, which one to be executed is decided based on the arguments or the type of parameters that invokes the function.
Function call binding: Connecting a function call to function body is known as function binding. In simple term this means calling a function.
Polymorphism is of 2 types:
1. Static polymorphism
2. Dynamic polymorphism.
Static polymorphism: If function call binding is performed at compile time. This is also known as Compile time binding / Early binding.
Dynamic polymorphism: If function call binding is performed at run time. This is also known as Run-time binding / Late binding.
In this article we will explain about Static Polymorphism. (Dynamic polymorphism will be covered in next article)
Static polymorphism: This is achieved through function overloading and operator over loading.
Function overloading: More than one functions with same name with different signature (Number of argument, Types of argument, Sequence of arguments) in a class or in a same scope is called Function overloading.
Internally the compiler decorates the name, the scope, and the argument lists to produce internal names for the overloaded functions. And when a call happen to a function then the linker uses the names to connect the function body to call.
e.g:
void show_name(int);
void show_name(char);
The compiler produce internal names as _show_name_int & _show_name_char . These names varies from compiler to compiler. The exact internal names can be found out from the assembly-language output.
e.g:Function overloading as a global function :
Sample code 1:
using namespace std;
void show_user_input(int data)
{
cout<<"User entered an integer which is : " << data<< endl;
}
void show_user_input(std::string data)
{
cout<<"User entered an string which is : "<<data<<endl;
}
int main()
{
int int_data;
std::string string_data;
cout<<"enter an interger input"<<endl;
cin>>int_data;
show_user_input(int_data);
cout<<"enter an string input"<<endl;
cin>>string_data;
show_user_input(string_data);
getchar();
return 0;
}
I/P : 10
Output: “User entered an integer which is : 10
I/P : “XYZ”
Output: “User entered an string which is : XYZ
e.g:Function overloading as a member function :
Sample code 2:
using namespace std;
class test
{
public:
void show_data(int data);
void show_data(char data);
};
void test::show_data(int data)
{
cout<<"Integer data ="<<data<<endl;
}
void test::show_data(char data)
{
cout<<"Charcter data =" << data <<endl;
}
int main()
{
test obj;
obj.show_data(10);
obj.show_data('A');
getchar();
return 0;
}
O/P : Interger data =10 Charcter data =A
Note -> C++ doesn’t allowed the function to overload based on return type. Because programmer can ignore the return value of the function. In that case compiler can’t decide which function to call.
e.g :
void fun()
int fun()
int x = fun(); // Compiler can decide to call int fun()
fun(); // In this case Compiler can’t decide which one to call
So return type overloading is prohibited in c++ and compiler will through error if programmer tries to do that.
Operator overloading: This mechanism is used to providing new implementation of existing operators to work with user-defined data types.
e.g: Class complex represents complex numbers: data members as integral and imaginary part. The requirement is to add 2 complex numbers. To achieve that user have to overload “+” operator and provide the logic as definition of overloaded function
class complex
{
int realPart;
int imaginaryPart;
public:
complex(int real, int imaginary);
void show_number();
complex operator +(complex data);
};
complex::complex(int real = 0, int imaginary = 0)
{
realPart = real;
imaginaryPart = imaginary;
}
complex complex::operator + (complex data)
{
complex temp;
temp.realPart = realPart + data.realPart;
temp.imaginaryPart = imaginaryPart + data.imaginaryPart;
return temp;
}
void complex::show_number()
{
cout<< realPart<< "+j" << imaginaryPart <<endl;
}
int main()
{
complex num1(2,3);
complex num2(5,7);
complex result = num1+num2;
cout<< "num1 + num2 =";
result.show_number();
getchar();
return 0;
}
o/p : num1 + num2 =7+j10
I’ll stop here. In next article I’ll try to explain dynamic polymorphism.
Thanks for reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles.

1 Response
[…] Static Polymorphism : If function call binding is performed at compile time. This is also known as Compile time binding / Early binding. (Please refer to previous blog for details of Static Polymorphism : C++ – Static Polymorphism ) […]