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.


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:

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:

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

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.

One thought on “C++ – Static Polymorphism

This site uses Akismet to reduce spam. Learn how your comment data is processed.