C++ Operator overloading
Operator overloading is one of the many exciting features of C++ programming language. It is an important technique that has enhanced or increase the power of extensibility of c++. We have stated more than once that c++ language tries to make the user defined data types behaves in much the same way as the built-in-types. For instance, c++ language permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. This means that c++ has the ability to provide the operators with a special meanings to an operator is known as operator overloading.
Operator overloading provides a flexible option for the creation of new definitions for most of the c++ programming operators. We can almost create a new language of our own by creative use of the function and operator overloading techniques.
We can overload(give additional meaning to) all the c++ operators except the following:
● Class member access operators ( ., . *).
● Scope resolution operator (: :).
● Size operator (sizeof).
● Conditional operator (?:).
The excluded operators are very few when compared to the large number of operators which qualify for the operator overloading definition.
Although the semantics of an operator can be extended, we can’t change its syntax, the grammatical rules that govern its use such as the number of operands, precedence and associativity. example manipulation operator will enjoy higher precedence then the addition operator. Remember always, when an operator is overloaded, its original meaning is not lost. For instance, the operator +, which has been overloaded two add two vectors, can still be used to add two integers.
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 describes the task. The general form of an operator function is:
classname: : operator (op –arglist)
{
Function body
}
|
Where return type is the type of value returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword operator. Operator op is the function name.
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 function has no arguments for unary operators and only one for binary operators. it is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or 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) |
|
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 defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public Area 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
for unary operators and
for binary operators. Op x(or x op) would be interpreted as
for friend functions. Similarly the expression x op y would be interpreted as either
in case of friend functions. When both the forms are declared, standard argument matching is applied to resolve any ambiguity.