C++ overloading binary operators

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] # include <iostream>
using namespace std;
class complex
{
float x; // real part
float y; // imaginary part
public : complex() { } // constructor 1
complex(float real, float imag) // constructor 2
{
x = real;
y = imag;
}
complex operator + (complex);
void display(void);
};
complex complex :: operator+(complex c)
{
complex temp; //temporary
temp.x = x + c.x; // these are
temp.y = y + c.y; // statement for float additions
return(temp);
}
void complex :: display(void)
{
std::cout << x << " + j" << y << "\n";
}
int main()
{ complex c1, c2, c3; // invokes constructor 1
c1 = complex (2.5, 3.5); // invokes constructor 2
c2 = complex (1.6, 2.7);
c3 = c1 + c2;
std::cout << "c1 = "; c1.display();
std::cout << "c2 = "; c2.display();
std::cout << "c3 = "; c3.display();
return 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;
return(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:

C3 = C1 + C2; // invokes operator+() 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

C3 = C1. operator+(C2); // usual function call syntax

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

temp.x = x + c.x;

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:

return complex((x+c.x) , (y+c.y)); // invokes constructor 2

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.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise