Overloading the Unary '-' Operator
We will overload the unary minus operator such that it will negate a complex object. A unary minus operator - has only one operand. As there is only one operand, they are overloaded as member functions and we will do the same here. The prototype of the operator function for overloading unary minus operator when implemented as a member function is:const Complex operator- () const;
The operator- function is implemented in the following manner:
- The function will perform the negation as C3 = -C1
- There is only one operand (C1) which is passed implicitly. Hence we do not need to pass anything to operator-.
- Create a Complex object in this function (C3 in above example) and assign the negation of real parts of C1 to the real part of C3. Similarly, assign the negation of imaginary part of C1 to C3.
- Return the object C3. Note that the return type is by const value.