Function overloading is a way to exploit polymorphism in C++.

Function Overloading is the feature where two functions with same name but with different parameter type, no. of parameter can behave as two seperate functions.

Function overloading is compile time polymorphism

  1. Possible with type of paramter and number of parameter. But
int fun(int a);

int fun(double a);

fun(2.0) // error as float or double can be implicitely converted to int.

double x = 2.0;
fun(x); // works

  1. Not with different return type. As compiler need to know at compile time which function you want to call but if only return type differs this is not possible.
int Add(int a,int b);

double Add(int a,int b);

cout<<Add(1,2); // error as compiler doesn't know which function to call

// or

int x  =  fun(1,2); // error as compiler doesn't know which function to call because double or float can get implicitly converted to int

// or

fun(1,2); // error as compiler doesn't know which function to call

  1. Functions can be overridden on the constant-ness of the parameters if parameter is a pointer or passed by reference.
void fun(int &i);       // 1st    // fun(int *i) 
void fun(const int &i); // 2nd   // fun(const int *i)

void fun2(int i);
void fun2(const int i);

int i;
fun(i); // 1st called
fun2(i); // error as compiler doesn't know which one to call

const int j;
fun(j); // 2nd called
fun2(j); // error as compiler doesn't know which one to call

And its makes sense as if vaiable is send by value then a copy of that vaiable is passed and Hence function can never can the variable (in main) no matter it is passed as const or not.

  1. Member function of a class can be overloaded based on consant-ness of the member function.

    In class, we can even make a function const which means that this function would not be changing the current object.

    const member function can be called on any object type but non const member function can only be called on non-constant class object. Same is the case when parameter passed by referenced /pointer is const ot not.

class Test{
  public:
    int x;
    Test(int x) : x(x) {}
    void fun() { return 0; }   // 1st function
    void fun() const { return 0; } // 2nd function
};

int main(){
  const Test t1(20);
  Test t2(30);

  t1.fun(); // 2nd function called
  t2.fun(); // 1st function called

}

  1. Member function differing in only static keyword can not be overloaded.
class Test { 
   static void fun(int i) {} 
   void fun(int i) {}    

   // redefination error even if we don't initiate a single objectr
}; 

  1. Function differing only in pointer vs array are equivalent. Only the second and subsequent array dimensions are significant in parameter types.

  2. Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent.

  3. Two parameter declarations that differ only in default values are equivalent in their function signature.

int fun(int a,int b);
int fun(int a,int b = 10);

// redefination error even if we don't initiate a single objectr