Advertisement



< Prev
Next >



C++ Function Overloading





In C++, the process of 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?







    Explaining the rules to function overloading


    • Overloaded functions must have different arguments.

    • Besides the basic and the most important rule to function overloading, which says that "all versions of an overloaded function must have the same name", the next most important rule to perform function overloading is that each version of a function being overloaded 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.




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

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

      //Creating a Math class with overloaded version of add fuction,
      //with each version having 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




    • When calling a normal function or an overloaded fuction, the actual integral arguments could be promoted to another integral data type.

    • When a normal or an overloaded function is called, the compiler tries to find a function whose prototype exactly matches the type of arguments used to call the function.

      And, 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


        • In the last example, 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 another example, in which 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





Function 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 already has a function and it inherits a function having a same name from another class, accessing the overloaded version throws a compile error. Let's see an example 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

C#, PYTHON and DJANGO


Advertisement