< Prev
Next >



Function Overloading in C++





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





  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




  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 -


    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.



    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 -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Polymorphism
Function Overriding >

Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2