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 in C++?







    Explaining the rules to function overloading


    • Versions of an overloaded function 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.

      //C++ Example of function overloading 
      //We will create a Math class and will overload its add()function
      
      #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 the add() function is adding two integers.
      • While, the second version of this add() function is adding two floating-point numbers.




    • Version of an overloaded function may/may not have different return types.

    In the previous coding example, you have seen the overloaded versions of an add() function, where each version has the same void return type. In this example, we will overload the same add() function again but, each overloaded version will be defined with a different return type.

    //Creating a Math class with overloaded version of add fuction,
    //with each version has different return type.
    
    
    #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




    Function overloading through inheritance does not work in C++


    In C++, function overloading performed through inheritance does not work(unlike some other languages), and accessing the overloaded version created through inhertiance throws a compile error. Let's see an example to prove this point.

    Tennis.cpp
    //C++ Function overloading through inheritance does not work in C++
    
    
    #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


    • In the last example, we have created a class Sports with a play() function in it and this class is inherited by another class Tennis, which already has a play(string) function. By doing this, the play(string) function within the Tennis class is overloaded by the inherited play() function of the Sports class.

    • Although the C++ compiler acknowledges when a normal function is inherited by a class through inheritance but it does not acknowledge when inheriting a class by another class results in creating an overloaded version of a function, hence, the compiler is complaining about the inherited overloaded version of play() function in the Tennis class.




    An important point:


    When we call a function(a normal or an overloaded function) which is defined with integral type arguments, the compiler first tries to find a function whose prototype matches exactly with the type of integral arguments used to call the function.

    But if the exact match is not found, the compiler converts the type of integral arguments used to call the function, to match with a function having a wider or narrower integral type arguments in its prototype.

    • Compiler converting the integral arguments used to call the function, to match with a function having wider integral arguments.

      Let us see an example in which a function which takes two int arguments is called and passed two char values, the char values are promoted to a wider integral type i.e int and the function is called.


    • //Math class with overloaded 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.



    • Compiler converting the integral arguments used to call the function, to match with a function having narrower integral arguments.

      Let us see an 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.


    • //Math class with overloaded 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 -


    300
    Hello





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