GeekInterview.com
Series: Subject: Topic:
Question: 127 of 258

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).  
Asked by: Interview Candidate | Asked on: Mar 6th, 2005
Showing Answers 1 - 3 of 3 Answers
SomGollakota

Answered On : Jun 13th, 2007

The C++ compiler identifies a function by its signature (function signature). The function signature is broken down into the following components in that order.

<function label or name>


The parameter list is further defined as 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Dhiraj Girdhar

Answered On : Jan 1st, 2009

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;
};

  
Login to rate this answer.
MCBod

Answered On : 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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us:
 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, click "Subscribe".