C++ OVERLOADING BINARY OPERATORS
We have just seen in previous page how to overload a unary operator. in The same mechanism can be used to overload thebinary operator. In chapter 6, we illustrated , how to add two complex numbers using a friend function. A statement like
C = sum(A, B); // it is functional notation.
was used. The functional notation can be replaced by a natural looking expression
c = A + B; // arithmetic notation
|
by overloading the + operator using an operator+() function. The Program illustrates how this is accomplished.
[overloading + operator]
<iostream>
using namespace std;
class complex
{
x;
y;
:
complex() { }
complex( real, imag)
{
x = real;
y = imag;
}
complex operator + (complex);
display(void);
};
complex complex :: operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
(temp);
}
complex :: display(void)
{
std::cout << x << " + j" << y << "\n";
}
main()
{
complex c1, c2, c3;
c1 = complex (2.5, 3.5);
c2 = complex (1.6, 2.7);
c3 = c1 + c2;
std::cout << "c1 = "; c1.display();
std::cout << "c2 = "; c2.display();
std::cout << "c3 = "; c3.display();
0;
}
|
The output of program would be:
C1 = 2.5 + j3.5
C2 = 1.6 + j2.7
C3 = 4.1 + j6.2
|
Note: Let us have a close look at the function operator+ () and see how the operator overloading is implemented.
Complex complex : : operator+ (complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
(temp) ;
}
|
We should note the following features of this function:
1) It receives only one complex type argument explicitly.
2) It returns a complex type value.
3) It is a member function of complex.
The function is expected to add two complex values and return a complex value as the result but receives only one value as argument. When does the other value come from? Now let us look at the statement that invokes this function:
We know that a member function can be invoked only by an object of the same class. Here, the object c1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function. The above invocation statement is equivalent to
Therefore, in the operator+() function, the data members of C1 are accessed directly and the data members of c2 (that is passed as an argument ) are accessed using the dot operator. Thus, both the objects are available for the function. For example, in the statement
e.x refers to the object c2 and x refers to the object C1.temp.x is the real part of temp that has been created specially to hold the results of addition of C1 and C2. The function returns the complex temp that has been created specially to hold the results of addition of C1 and C2. The function returns the complex temp to be assigned to C3.in Figure show how this is implemented.
As a rule, in overloading of binary operators, the left-hand operand is used to invoke the operator function and the right-hand operand is passed as an argument.
We can avoid the creation of the temp object by replacing the entire function body by the following statement:
complex((x+c.x) , (y+c.y));
|
What does it mean when we use a class name with an argument list? When the complier comes across a statement like this, it invokes an appropriate constructor, initializes an object with no name and returns the contents for copying into an object. Such an object is called a temporary object and goes out of space as soon as soon as the contents are assigned to another object. Using temporary objects can make the code shorter, more efficient and better to read.