Defining operator overloading

To define an additional task to an operator,we must specify what it means in relation to the class to which the operator is applied.This is done with the help of a special function,called operator function,which describe the task.The general form of an operator function is:

where return type is the type of value returned by the specified operation and op is the operator being overloaded.operator op is the function name,where operator is a keyword.

operator function must be either member functions(non static) or friend functions.A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators,while a member function has no argument for unary operators and only one for binary operators.

This is because the object used to invoke the member function is passes implicity and therefore is available for the member function.This is not the case with friend functions. Argument may be passed either by value or by reference .operator functions are declared in the class using prototypes as follows:

vector is a data type of class and may represent both magnitude and direction (as in physics and engineering)or a series of points called elements (as in mathematics).

šŸŽ‡The process of overloading involves the following steps:

1.create a class that define the data type that is to be used in the           overloading operation.

2.Declare the operation function operator op() in the public part of     the class.it may be either a member function or a friend function.

3.Define the operator function to implement the required                     operations.

overloaded operator functions can be invoked by expressions such as

op x or x op

for unary operators and

 x op y

 for binary operators.op x(or x op)would be interpreted as

operator op(x)

for friend functions.similarly,the expression x op y would be interpreted as either

x.operator op (y)

in case of member functions,or

operator op(x,y)

in case of friend function. when both the forms are declared,standard argument matching is applied to resolve any ambiguity.



different type of operator overloading:

1)  unary operator       click and go

0 comments:

Copyright © 2013 free coding