Defining Operator Overloading
To describe a further task to an operator, we must have to specify what it means in relation to the class to which the operator is applied. This is completed along with the help of a special function, called operator function that describes the task. The common form of an operator function is:
Where return type is the type of value returned through the specified operation and op is the operator being overloaded. The op is preceded through the keyword operator. An Operator op is the function name.
Operator functions must be either member functions or it will be a friend functions. A basic difference among them is that a friend function will have only one argument for unary operators and two for the binary operators. That is because the object used to invoke the member function that is passed implicitly and thus is available for the member function. That is not the case with friend functions. Arguments might be passed either by value or either by reference.
Operator functions are declared in the class using prototypes as follows:
Vector operator+(vector);
Vector operator -();
Friend vector operator+(vector,vector);
Friend vector operator-(vector);
Vector operator-(vector &a);
Int operator= =(vector);
Friend int operator = =(vector,vector)
A Vector is a data type of class and might represent both magnitude and direction or a series of points called elements
The process of overloading includes the following steps:
1. First , it create a class which defines the data types that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class. It might be either a member function or a friend function.
3. Describes the operator function to implement the needs operations. Overloaded operator functions could be invoked through 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, as same, the expression x op y would be interpreted as x.operatro op(y)
in case of member functions, or operator op(x,y)
in case of friend functions. When both the forms are declared the standard argument matching is applied to resolve any ambiguity.