Basic Structure
In this lab, you will be implementing a class called Complex, to represent complex numbers in mathematics. A complex number consists of a real part and an imaginary part. It is represented as x + yi where x and y are the real and imaginary parts, respectively.
Operator overloading is a powerful feature of C++ that allows us to make user-defined data types behave in a similar way to the built-in data types. Just as we can add two integers using '+', operator overloading allows us to add two objects of our class.
For this lab, you will overload the operators binary +, unary negation - and << such that
- operator + allows us to add two complex numbers
- unary minus - allows us to negate a complex number (both the real and imaginary parts are negated)
- << allows us to display complex numbers
To specify the
task a particular overloaded operator has to perform, we need to
write an operator function. The following is the prototype of such
a function:
returntype operator op
(arguments);
where returntype is type of value
returned and op is the operator being overloaded.
The operator functions can be either member or non-member functions. We will be using both options in this lab.
You may assume that the real and imaginary parts are integers. Note that they can be negative, and your program should handle negative values appropriately.