Open In App

Function overloading and const keyword

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.

The parameters should follow any one or more than one of the following conditions for Function overloading:

  • Parameters should have a different type

add(int a, int b)
add(double a, double b)

Below is the implementation of the above discussion:

C++




#include <iostream>
using namespace std;
 
 
void add(int a, int b)
{
  cout << "sum = " << (a + b);
}
 
void add(double a, double b)
{
    cout << endl << "sum = " << (a + b);
}
 
// Driver code
int main()
{
    add(10, 2);
    add(5.3, 6.2);
 
    return 0;
}


Output

sum = 12
sum = 11.5
  • Parameters should have a different number 

add(int a, int b)
add(int a, int b, int c)

Below is the implementation of the above discussion:

C++




#include <iostream>
using namespace std;
 
void add(int a, int b)
{
  cout << "sum = " << (a + b);
}
 
void add(int a, int b, int c)
{
    cout << endl << "sum = " << (a + b + c);
}
 
// Driver code
int main()
{
    add(10, 2);
    add(5, 6, 4);
 
    return 0;
}


Output

sum = 12
sum = 15
  • Parameters should have a different sequence of parameters.

add(int a, double b)
add(double a, int b)

Below is the implementation of the above discussion:

C++




#include<iostream>
using namespace std;
 
void add(int a, double b)
{
    cout<<"sum = "<<(a+b);
 
void  add(double a, int b)
{
    cout<<endl<<"sum = "<<(a+b);
 
// Driver code
int main()
{
    add(10,2.5);
    add(5.5,6);
 
      return 0;
}


Output

sum = 12.5
sum = 11.5

Predict the output of the following C++ program. 

CPP




#include <iostream>
using namespace std;
 
class Test {
protected:
    int x;
 
public:
    Test(int i)
        : x(i)
    {
    }
    void fun() const
    {
        cout << "fun() const called " << endl;
    }
    void fun() { cout << "fun() called " << endl; }
};
 
int main()
{
    Test t1(10);
    const Test t2(20);
    t1.fun();
    t2.fun();
    return 0;
}


Output

fun() called 
fun() const called 

The two methods ‘void fun() const’ and ‘void fun()’ have the same signature except that one is const and the other is not. Also, if we take a closer look at the output, we observe that ‘const void fun()’ is called on the const object, and ‘void fun()’ is called on the non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function returns a reference or pointer. We can make one function const, that returns a const reference or const pointer, and another non-const function, that returns a non-const reference or pointer. See this for more details. What about parameters? Rules related to const parameters are interesting. Let us first take a look at the following two examples. Program 1 fails in compilation, but program 2 compiles and runs fine. 

CPP




// PROGRAM 1 (Fails in compilation)
#include<iostream>
using namespace std;
 
void fun(const int i)
{
    cout << "fun(const int) called ";
}
void fun(int i)
{
    cout << "fun(int ) called " ;
}
int main()
{
    const int i = 10;
    fun(i);
    return 0;
}


Output:

Compiler Error: redefinition of 'void fun(int)'

CPP




// PROGRAM 2 (Compiles and runs fine)
#include<iostream>
using namespace std;
 
void fun(char *a)
{
cout << "non-const fun() " << a;
}
 
void fun(const char *a)
{
cout << "const fun() " << a;
}
 
int main()
{
const char *ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}


Output

const fun() GeeksforGeeks

C++ allows functions to be overloaded on the basis of the const-ness of parameters only if the const parameter is a reference or a pointer. That is why program 1 failed in compilation, but program 2 worked fine. This rule actually makes sense. In program 1, the parameter ‘i’ is passed by value, so ‘i’ in fun() is a copy of ‘i’ in main(). Hence fun() cannot modify ‘i’ of main(). Therefore, it doesn’t matter whether ‘i’ is received as a const parameter or a normal parameter. When we pass by reference or pointer, we can modify the value referred or pointed, so we can have two versions of a function, one which can modify the referred or pointed value, other which can not.

As an exercise, predict the output of the following program. 

CPP




#include<iostream>
using namespace std;
 
void fun(const int &i)
{
    cout << "fun(const int &) called ";
}
void fun(int &i)
{
    cout << "fun(int &) called " ;
}
int main()
{
    const int i = 10;
    fun(i);
    return 0;
}


Output

fun(const int &) called 


Last Updated : 17 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads