Explanation of Function Overloading in C++

Explanation of Function Overloading in C++

Hello everyone, in this article we are going to talk about function overloading with polymorphism concept in object-oriented programming in C++ with the help of examples.

Let's begin

What is Function overloading?

Function overloading is a flexible way to define multiple methods with same name and different parameters. Function overloading is a feature of polymorphism in object-oriented programming. We can define methods with same name but different parameters. We can define different type of parameters and different number of parameters for these functions. This makes the the programming language more flexible and readable. During development compiler decide the to which one will be called according the used signature of overloaded functions.

We need to pay attention when function overloading to functions must have same name and parameters should be different from each other. Types of parameters or order of the parameters must be different. (Signature of the methods must be different)

Advantages of Function Overloading:
  • Function overloading provides the code reusability, We can use same name for similar operations and it improves the readable of the code.
  • Usage of overloaded functions are being decided on compile time. Unused functions are not including by compiled program.
  • With function overloading cleaner code can be implemented and development can be more efficient and flexible.

Let's make some examples about function overloading.

Below example we will simply overload a function and use them in main function.

#include <iostream>
using namespace std;

// Overloaded functions
void log(int val) {
    std::cout << "Integer: " << val << std::endl;

void log(std::string text) {
    std::cout << "String: " << text << std::endl;

int main() {
    log(15);  // Calls log(int)
    log("thecodeprogram");  // Calls log(std::string)

    return 0;
Program output will be like below

Integer: 15
String: thecodeprogram

As you can see above we have overloaded log functions with int and string variable types. So in main function we have used same function for int value and string value. Now we can log both types of values wth our function. During compilation both of the functions are compiled with the application to use both.

Now lets make another example to understand more

Below example we will calculate the area of a rectangle and a square. Below we will define same function with different number of parameters.

#include <iostream>

void area(double length) {
    std::cout << "Square area: " << length * length << std::endl;

void area(double x, double y) {
    std::cout << "Rectangle area: " << x * y << std::endl;

int main() {
    area(6.9);      // Calculates the square
    area(3.3, 14.9);   // Calculates as rectangle.

    return 0;

As you can see above we have defined two methods with same name but with different parameters. In this way compiler compiles the both of the methods for function overloading.


Function overloading is very efficient way to define multiple methods for same purpose. We can add more functionality to a method with method overloading. It improves the code maintainability and flexibility. We can organise our code-base in better way.

That is all.

Have a great function overloading.

Burak Hamdi TUFAN.


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!


  • There is no comment. Be the owner of first comment...