answersLogoWhite

0


Best Answer

Overloading refers to use of something for different purposes.

Function Overloading - It refers to the design of family of functions with one function name but with different argument list. The correct function will be invoked by checking the number and type of the arguments.

Operator Overloading - The mechanism of giving the additional property to the operator is known as operator overloading. We can overload all the operators except the following:

1.)Class members access operators

2.)Scope resolution operator

3.)Sizeof operator

4.)Conditional operator

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Function overloading means to provide different implementations of the same function. To differentiate the implementations, each function must have a different signature. A function's signature is defined by the number and type of its arguments, including the return type. However, overloaded functions cannot differ by return type alone.

An example of an overloaded function:

int GetSum( int x, int y ) { return( x+y ); }

int GetSum( int x, int y, int z ) { return( x + y + z ); }

float GetSum( float x, int y ) { return( x + y ); }

Some function overloads may have the exact same implementation and the same number of arguments, the only difference being the type of argument. In these cases, a function template can be used with a generic type. The compiler will generate the actual overloads on an as-required basis. This is more efficient and easier to maintain than coding all the required overloads by hand.

Example template function (same implementation regardless of type):

template <class T> // generic type.

T GetMax(T x, T y ) { return( x>y?x:y ); }

Template functions can also be overloaded with standard overloads, however there must be no ambiguity regarding which version of a function is being called. Every instance of the function must have a unique signature.

You can also achieve the same effect of template functions using function-style macros, however macros are not type-safe and may have hidden side effects that cannot be resolved at compile time. Also, macros are inline expanded whether you like it or not, which is not always beneficial to overall performance. However, macros handle type promotion more easily so there is no need to write specific overloads when dealing with mixed types.

#define GetMax( x, y ) (x) > (y) : x ? y

This macro can handle any type and in any order, such as GetMax(int, float) or GetMax(float, int). However, there's no guarantee over the return type. If the return value is x, the return type is determined by the type of x. This is not type-safe and the compiler cannot determine if the call is safe or not, which may lead to unexpected side-effects at runtime.

To make the calls type safe, it is better to use a function template:

#include <iostream>

template< class T, class U >

T GetMax(T x, U y ) { return( x > y ? x : (T) y ); }

// returns the same type as the first parameter.

int main()

{

int a = 5;

int b = 6;

double c = 10.1;

int r = GetMax( a, c ); // legal.

double s = GetMax( a, b ); // legal - implicit cast from int to double.

int t = GetMax( c, a ); // warning C4244

// warning C4244:

// 'initialising' : conversion from 'double' to 'int', possible loss of data

return( 0 );

}

In the above example, the compiler is warning you of a potential problem. To resolve the warning, either use an explicit cast:

int t = ( int ) GetMax( c, a );

Or switch the arguments around:

int t = GetMax( a, c );

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Operator overloading means redefining an operator so that it means different things in different contexts. Many languages already have some built-in overloading; notably, the "+" operator does addition when used on integers or floating-point numbers; but when used on strings, the strings are concatenated. For example, the result of "George" + "Washington" = "GeorgeWashington". Also in some languages, the same "+" operator can be used to add a date to a number - the result is another date, a specified number of days after the specified date. Those are all quite different calculations, done with the same operator.

Some languages, like C++, allow the programmer to give additional meanings to an operator, meanings that are not initially part of the language.

Operator overloading means redefining an operator so that it means different things in different contexts. Many languages already have some built-in overloading; notably, the "+" operator does addition when used on integers or floating-point numbers; but when used on strings, the strings are concatenated. For example, the result of "George" + "Washington" = "GeorgeWashington". Also in some languages, the same "+" operator can be used to add a date to a number - the result is another date, a specified number of days after the specified date. Those are all quite different calculations, done with the same operator.

Some languages, like C++, allow the programmer to give additional meanings to an operator, meanings that are not initially part of the language.

Operator overloading means redefining an operator so that it means different things in different contexts. Many languages already have some built-in overloading; notably, the "+" operator does addition when used on integers or floating-point numbers; but when used on strings, the strings are concatenated. For example, the result of "George" + "Washington" = "GeorgeWashington". Also in some languages, the same "+" operator can be used to add a date to a number - the result is another date, a specified number of days after the specified date. Those are all quite different calculations, done with the same operator.

Some languages, like C++, allow the programmer to give additional meanings to an operator, meanings that are not initially part of the language.

Operator overloading means redefining an operator so that it means different things in different contexts. Many languages already have some built-in overloading; notably, the "+" operator does addition when used on integers or floating-point numbers; but when used on strings, the strings are concatenated. For example, the result of "George" + "Washington" = "GeorgeWashington". Also in some languages, the same "+" operator can be used to add a date to a number - the result is another date, a specified number of days after the specified date. Those are all quite different calculations, done with the same operator.

Some languages, like C++, allow the programmer to give additional meanings to an operator, meanings that are not initially part of the language.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

You overload operators by changing the type of the operand(s). In the following example, the assignment operator has been overloaded to accept a constant foo reference and a constant integer, which gives us two ways to assign foo objects.

class foo

{

public:

foo():m_val(0){}

foo & operator= (const foo & rhs ){m_val = rhs.m_val; return *this;}

foo & operator= (const int rhs ){m_val = rhs; return *this;}

private:

int m_val;

};

void main()

{

foo X, Y;

int i = 5;

X = i; // Assign by integer.

Y = X; // Assign by foo reference.

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Operator overloading means redefining an operator so that it means different things in different contexts. Many languages already have some built-in overloading; notably, the "+" operator does addition when used on integers or floating-point numbers; but when used on strings, the strings are concatenated. For example, the result of "George" + "Washington" = "GeorgeWashington". Also in some languages, the same "+" operator can be used to add a date to a number - the result is another date, a specified number of days after the specified date. Those are all quite different calculations, done with the same operator.

Some languages, like C++, allow the programmer to give additional meanings to an operator, meanings that are not initially part of the language.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Operator overloading involving vector types is not supported.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Giving an example explain what is Operator Overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


What do you men by precipitation explain by giving an example?

what do u mean by precipitation? explain by giving an example


What do you mean by precipitation reaction explain by giving example?

creation of solid in a solution


Explain test marketing giving examples?

process of test marketing with the help of an example


How do you perform any operation by giving any operator?

You cannot perform any operation by giving any operator. The operation must be valid in the domain and range.For example, you often cannot perform the square rootoperation using the square root operator if your domain and range are integers. At the level of maths that I guess you are at (from this question), the square root of a negative number is not a operation that is defined.


Explain the following tearm in the context of object oriented programming Also explain how these concept are implement in c by given an example program for each?

Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.


Is the word explain a verb?

Yes, the word explain is a verb; the act of explaining, to make clear by providing information; to account for, giving a reason or justification for something. Example sentence: Now you can explain why the word explain is a verb.


When do we use an address operator in c'?

In C we use &amp; operator while giving address of some variable to some pointer variable. &amp; operator is also used in scanf().


How do you simplify an equation and then solve it?

Collect like terms is a good starting point. Giving an example would have nade it easier to explain.


Explain the Abstraction terms in the context of object oriented programming Also explain how these concepts are implemented in C by giving an example program for each?

g terms in the context of object oriented programming


Can an object be accelerated if it is moving with constant speed if yes explain giving example?

Yes, it can. Perhaps the simplest example is when an object moves at constant speed, in a circle. In this case, the speed doesn't change; the velocity does.


What is the difference between deacribe and explain?

Describe refers to giving a representation in words while to explain refers to giving details, defining the structure and scope.