What is function overloading and operator overloading?

  Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types. Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).  

Showing Answers 1 - 9 of 9 Answers

The C++ compiler identifies a function by its signature (function signature). The function signature is broken down into the following components in that order.
<return type>
<function label or name>
<parameters list>

The parameter list is further defined as <data type> parameter label or name. If the order of the parameters (datatype name) changes, even if the function name/label does not change, the signature is considered unique. So, in C++, it is conceivable to have many different functions/methods with exact same label name (for example Function1) so long as the entire signature is considered unique. So, changing the number of parameters, their data types, the order of them, or any combination of these can change the function signature.
Important Note: Changing the return type ALONE is NOT considered change in signature.

Now, answering the actual question of overloading function vs. operator:
Function Overloading: When you define many different functions (unique signatures) which have the exact label name, it is function overloading. For example, void Function_Foo();
void Function_Foo(int);        // Overloaded
void Function_Foo(int, int);  // Overloaded
int Function_Foo();    // Error - function redefined (because only return type is different)

Operator Overloading: In C++, basic operators are also considered functions at a compiler level. So, when you define a different operation for an operator by changing the standard parameter list, it is operator overloading. For example
MyClass operator + (MyClass& cls); //Add the data members of two instances of the same class

... etc.

Here are some examples
1) Function overloading

class FuncOver {
public:
      // Constructors can also be overloaded.
      FuncOver();

      // Overloaded constructor.
      FuncOver(int i);

      // Functions example.
      int sum(int a, int b);

      int sum(float a, float b);
};

Operator overloading example...

class MyInt {
public:
       // + operator overloaded.
       int operator+(MyInt b);
private:
    int a;
};

  Was this answer useful?  Yes

MCBod

  • Mar 31st, 2010
 

Overloading basically refers to the provision of second of subsequent version of the same function or operator based on different parameters.

In terms of functions each different version of the function takes different parameters. Thus allowing for the same logic to be executed in different contexts

In terms of operators, overloading refers to the redefinition of the affect of an operators on different class types. So that we could define a sence of addition or greater than for a handcoded class

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions