Breaking News

Operator Overloading In C++

This means giving additional meaning to normal operators when they are applied to user defined data types.
 
The compiler identifies that it is an overloaded operator by looking at the data type of operand (i.e. if it is a user defined data type then the compiler knows it is an overloaded operator).
 
You have to remember that objects are defined by you (We mean they are defined by the programmer). The data that comes under an object is chosen by you.
 
The data type that you use will be in-built data type like integer or character but the object as a whole is user defined. Hence it is not possible to add two objects (that belong to the same class) using the + sign.
 
But using operator overloading you could use the same + sign to add two objects.
 
Syntax:
 
return data type keyword operator sign of operator to be overloaded (arguments)
 
{
body of the function;
}
 
Example
 
void operator ++ ( )
{
Body of the function;
}
 
Let us illustrate overloading by overloading an unary operator ( ++ )
 
class counter
 
{
private:
int count;
public
counter ( ) : count (0) // Constructor that initializes count to zero
{ }
 
int display( )
{
return count;
}
 
void operator ++ ( ) // ++ is overloaded operator. When compiler comes across ++
 
{ // anywhere in the program, then if the operand is user defined,
++ count; // the compiler will do whatever is written within the body of the operator function
}
};
 
int main( )
{
counter c1, c2; // c1 and c2 are two objects belonging to class counter
cout<<c1.display( ); // count of c1 is 0
cout<<c2.display( ); // count of c2 is 0
++c1; // ++ is an overloaded operator
++c2; // Compiler knows this because c1 and c2 are objects-they are user defined
++c2;
cout<<c1.display( ); // count of c1 is 1
Cout<<c2.display( );         // count of c2 is 2
return;
}
 
When compiler comes across ++c1 it does the following. It knows that c1 is an object which a user defined data type.
 
But ++ can operate only on in-built data types. Now it thinks. It remembers that ++ was already overloaded using the operator function within the class counter.
 
So it goes back to the operator function and reads through what is written in the body. No return data type and no arguments. This makes it work a lot easier.
 
++count;
 
count is an integer data type. The compiler increases the value of count by one and goes back to the original program. All this was done when it saw ++c1. Hence the count of c1 only is increased by one.
 
When the compiler comes to ++c2 it increases the count of c2 by one.
 
with return data type
 
Consider the same example as in the previous section. This time we shall make use of the return data type in the operator function to return something to the main program.
 
 
In the above program we overload the same unary operator but use a return data type counter. Counter is the name of the class itself.
 
This means the function is returning a class. But a class is just a template and means nothing without objects. Hence the function actually can be assumed to return an object
 
++count;
return counter (count);
 
As in the earlier program, the overloaded operator just increases the count by one. It does the same here. We know that counter is the name of the class.
 
Within the braces we have written count. In the above example count of c1 was 0 and then it became 1 because of the ++count statement. Now count is 1. Hence the return statement will actually be counter (1).
 
This line is just an object without a name (an unnamed object). The compiler remembers having seen the one argument constructor and goes to it. The count value of the unnamed object is initialised to one.
 
The compiler remembers that all this is supposed to be returned. It goes back to the main program.
 
c2 = ++c1;
 
c2 is an object and hence the value of count of the unnamed object is stored in c2. So c2’s count becomes 1.
 
The above program illustrates the return data type.
 
Suppose that you are using an enumeration and you wish to output its value:
 
enum E {e = 37};
cout << e;
 
37 will indeed be output, by virtue of the enumerator value being promoted to an int and then output using the operator<<(int) function found in iostream.h.
 
But what if you’re interested in actually seeing the enumerator values in symbolic form? One approach to this would be as follows:
 
 
Out put of the program
 
 
In the last output statement, we created an invalid enumerator value and then output it.
 
Operator overloading in C++ is very powerful but can be abused. It’s quite possible to create a system of operators such that it is difficult to know what is going on with a particular piece of code.
 
Some uses of overloaded operators, such as [] for array indexing with subscript checking, -> for smart pointers, or + – * / for doing arithmetic on complex numbers, can make sense, while other uses may not.

Check Also

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion …