Topic :Function Overloading in C++ Programming Language:

Function Overloading

In Our previous tutorial Operator Overloading we discuss about the concept of Overloading, and discuss Operator Overloading. in this tutorial we will discuss about function Overloading.





When More than one functions use the same name with different arguments, are known as Function Overloading. It is the dominant feature of C++ that allows us to use the same name for different functions to perform either same or different task in the same class. Function overloading is usually used to enhance the readability of the program. If we have to perform one single Operation but with a different number of types of arguments. then we can simply overload the function.



How to overload a function:

We can Overload any function, by changing number of arguments, or by having different types of arguments. as show in example:



//function without parameter 
void show() 
{
    //function body;
}
 
//overload function using single parameter.
void show(int a) 
{
    //function body;
}
 
//overload function using 2 parameters.
void show(int a, char c) 
{
    //function body:
}

In Above Example: we have three function, Function name is same, but having different arguments. its mean we overload function 2 times. the first with single argument, and second with double argument. that's how we can overload a function to use it for different tasks. Let's have an simple example, where we will overload function to perform different tasks.


/*Example: Function Overloading: InfoBrother:*/
 
#include<iostream>
using namespace std;
 
class Overload
{
   public:
      //function without parameter 
       void show() 
       {    
          cout<<"Am function having no Parameter:"<<endl;
       }
 
     //overload function using single parameter.
      void show(int a) 
      {
         cout<<"Am Overloaded Function having an Integer "<<a<<endl;
      }
 
 
    //overload function using 2 parameters.
      void show(int a, char c) 
      {
         cout<<"Am Overloaded Function Having Two paramter: Int= "
             <<a<<" & char= "<<c<<endl;
      }    
};
 
main()
{
    Overload load;
    load.show();
    load.show(15);
    load.show(15,'c');
 
    return 0;
}


It is really fun to use function overloading. It minimizes the complexity and maximizes the reliability of our code. We just have to follow the rules, and we will be getting the precise and creative code.









I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement