Advertisement



< Prev
Next >



C++ Function Overloading





In C++, function Overloading is creating a function with the same name as an existing function in a class. Hence in simple words, function overloading allows us to have multiple versions of a function within a class.




Why function overloading is used?


function overloading is mainly used in a situation when we want to create a multiple specialized versions of a function in a class, with each version having the same name and doing some specific specialized task.




How function overloading is achieved?







    Rules to function overloading -



  1. Overloaded functions Must have different arguments.

  2. This is the most important rule to perform function overloading. In order to create a multiple versions of a function is that each version of this function MUST have different type of arguments.

    //Overloading add function of Math class.
    
    #include<iostream>
    
    using namespace std;
    
    class Math
    {
    public:
    void add(int a, int b)		//add function adding two integers
    {
    	cout<< a+b << "n";
    }
    
    
    void add(float a, float b)	//add function adding two floats
    {
    	cout<< a+b << "\n";
    }
    };
    
    
    
    int main()
    {
    //Creating an object of Math class.
    Math ob; 
    
    //Calling an overloading version of add() function which takes two int values.
    ob.add(1,2); 
    
    //Calling an overloading version of add() function which takes two float values.
    ob.add(2.5f,4.7f);
    }


    Output -


    3
    7.2

    • We have overloaded the add() function and have created its two different versions within its Math class.
    • One version of add function is adding two integers.
    • While, the second version of add function is adding two floating point numbers.




  3. Overloaded methods may/may not have different return types.

  4. In the previous coding example, you have seen overloaded add() methods having a similar, void return type. In this example, we will overload add() function with different return types.

    //Math class with overloaded add methods with different return types
    
    #include<iostream>
    
    using namespace std;
    
    class Math
    {
    public:
    
    //add function adding two integers
    int add(int a, int b)
    {
    return(a+b);
    }
    
    
    //add function adding two float numbers
    float add(float a, float b)
    {
    return(a+b);
    }
    
    };
    
    
    int main()
    {
    //Creating an object of Math class.
    Math ob;
    
    //Calling an overloading version of add() function which takes two int values.
    cout << ob.add(1,2) << "\n";
    
    //Calling an overloading version of add() function which takes two int values.
    cout << ob.add(2.5f,4.7f) << "\n";
    }


    Output -


    3
    7.2



    Advertisement




  5. Calling a function, the actual integral arguments could be promoted to another integral data type.

  6. When a function is called, the compiler tries to find a function whose prototype exactly matches the type of arguments used to call the function.

    If the exact match is not found, the compiler promotes the integral arguments used to call the function to a function with wider or narrower integral arguments, for example -
    • char to int / int to char
    • int to long/ long to int
    • int to float/ float to int
    • int to double/ double to int
    • float to double / double to float
    • long to double / double to long


    Let us see an example - when a function which takes two int arguments is called and passed two char values, the char values are promoted to int and the function is called. Let's see an example.

    • //Math class with add(int,int) function is called with two char values  i.e. char is promoted to int
      
      #include<iostream>
      #include<string>
      
      using namespace std;
      
      class Fun
      {
      public:
      
      //add function adding two integers
      void add(int a, int b)
      {
      cout << a+b << "\n";
      }
      
      
      //add function adding two string
      void add(string s1, string s2)
      {
      cout << s1+s2;
      }
      
      };
      
      
      int main()
      {
      //Creating an object of Math class.
      Fun ob;
      
      //Calling an overloading version of add() function with two char values, which are promoted to int
      ob.add('a','b');
      
      //Calling an overloading version of add() function with two string values
      ob.add("He", "llo");
      }


    • Output -


      195
      Hello


      • We have overloaded the add() function and have created its two different versions within its Math class.
      • One version of add function is adding two int values.
      • While, the second version of add function is adding two string values.
      • When the add() function is called with two char values, the char values are promoted to int and the add() function which takes two int values is called.





      Let us see an example - when a function which takes two int arguments is called and passed two long values, the long values are converted to int and the function is called. Let's see an example.

    • //Math class with add(int,int) function is called with two long values  i.e. long is converted to int
      
      #include<iostream>
      #include<string>
      
      using namespace std;
      
      class Fun
      {
      public:
      
      //add function adding two integers
      void add(int a, int b)
      {
      cout << a+b << "\n";
      }
      
      
      //add function adding two string
      void add(string s1, string s2)
      {
      cout << s1+s2;
      }
      
      };
      
      
      int main()
      {
      //Creating an object of Math class.
      Fun ob;
      
      //Calling an overloading version of add() function with two long values, which are promoted to int
      ob.add(100L,200L);
      
      //Calling an overloading version of add() function with two string values
      ob.add("He", "llo");
      }


    Output -


    195
    300





Overloading through inheritance does not work in C++


In C++, function overloading does not work in inheritance(unlike some other languages) i.e. when a class has a function and it inherits a function having a same name from another class. Let's see a case to prove this point.

Tennis.cpp
#include<iostream>
#include<string>

using namespace std;

//Base class
class Sports
{
public:
void play()
{
	cout<<"Play Sports";
}
};

//subclass
class Tennis : public Sports	
{
public:
void play(string name)	//overloaded play() function with String arguments
{
	cout<< "Play " << name << "\n";
}
};


int main()
{
Tennis ob;
ob.play();
ob.play("Tennis");

return 0;
}


Output -


Tennis.cpp: In function 'int main()':
Tennis.cpp:30:9: error: no matching function for call to 'Tennis::play()'
 ob.play();
         ^
Tennis.cpp:20:6: note: candidate: void Tennis::play(std::__cxx11::string)
 void play(string name) //overloaded play() function with String arguments
      ^~~~
Tennis.cpp:20:6: note:   candidate expects 1 argument, 0 provided


Program Analysis






Please share this article -




< Prev
Next >
< Polymorphism
Function Overriding >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement