Function Overloading in C++

In this CPP tutorial, we are going to discuss the concept of Function overloading in C++ and its implementation.

What is Function Overloading in C++

It is a feature in C++ where a function can be overloaded means two or more than function can have the same name but their way of doing work is different. These functions are distinguished by changing in no. of parameters or by changing in the return type and no. of parameters.

Ex:-




void max ( int a, int b);

int max ( int a, int b, int c)

CPP code implementation of function overloading

#include<bits/stdc++.h>
using namespace std;

int maxx(int a, int b)
{
    int res = a>b? a: b;
    return res;
}

int maxx(int a , int b, int c)
{
    int res=a>b?a:b;
    return res>c?res:c;
}

 double maxx(double a , double b)
{
    return a>b? a: b;
}

char maxx(char a, char b)
{
    return a>b? a:  b;
}

int main()
{
    cout<<maxx(1,2,3)<<endl;
    cout<<maxx(1,2)<<endl;
    cout<<maxx(2.3,3.4)<<endl;
    cout<<maxx('a','d')<<endl;
}

OUTPUT

3
2
3.4
d

Functions that can not be overloaded

  1. A static member function declaration cannot be overloaded.
  2. Function which differs only in return type also cannot be overloaded.
  3. Parameters that differ only in pointer type declaration vs array type declaration are considered to be equivalent and are not considered in function overloading.
  4. Two parameters that differ only in the absence and presence of const are considered to be equivalent. Hence, not considered in function overloading.

Function overloading vs Function Overriding

  • Function Overloading is achieved at compile time while function overriding is achieved at run time.
  • This is using the same function name by just changing in data types of parameter or change in no. of parameters while function overriding is a redefinition of base class function in its derived class with the same return type and parameters using the keyword virtual in its base class function.
  • Function overriding cannot occur without inheritance while overloading can occur.
  • Overloaded functions are in the same scope while Overridden functions are in different scope.

Main() function can be overloaded ?

Main is not a reserved keyword in programming languages hence it can be overloaded. To overload the main function we can define a class and we can use main() as its member function.

CPP code implementation of main() function

 
#include<bits/stdc++.h>
using namespace std;

   class overload
 {
    public:
    
           int main(string s)
         {
             cout<<s<<endl;
             
              return 0;
         }
       
          int main(int a)
          {
              cout<<a<<endl;
             return 0;
          }
          
          int main( int a , int b)
          {
              cout<<a+b;
              
             return 0;
          }
 };
 
 int main()
 {
     overload o;
     
     o.main("main() called");
     o.main(1);
     o.main(1,2);
     
     return 0;
 }

OUTPUT

main() called
1
3

Also read,


Leave a Reply

Your email address will not be published. Required fields are marked *