Question:
Complex numbers are of the form where x and y are real numbers and i is the imaginary x + iy, unit equal to
√− 1 and i2 =− 1 . Very often a complex number is represented by a single letter, z = x + iy.
The complex conjugate of a complex number z = x + iy is defined to be: z = x − iy.
Operations on Complex Numbers:
Consider, z1 = x1 + iy1 and
z iy . 2 = x2 + 2
Addition: z1 + z2 = (x1 + x2) + i(y1 + y2)
Subtraction: z1 − z2 = (x1 + x2) − i(y1 + y2)
Multiplication: z1. z2 = (x1x2 − y1y2) + i(x1y2 + x2y1)
Division:
The Complex Class:
Implement the class named Complex. The class should contain two data members, real and imag, both of
which are doubles. It should also have the following member functions:
1. Complex() : the default constructor that sets both real and imag to zero.
2. Complex( double r ) : an overloaded constructor that sets real to r and imag to zero.
3. Complex( double r, double i ) : another overloaded constructor that sets real to r and imag to i.
4. Complex( Complex c ) : the copy constructor
5. Complex add( Complex c ) : this function adds the complex number invoking the function to c (the
parameter to this function), and returns a new complex number that represents their sum.
6. Complex subtract( Complex c ) : this function subtracts the complex number c from the complex
number invoking the function, and returns a new complex number that represents their difference.
7. Complex multiply( Complex c ) : this function multiplies the complex number invoking the function
to c, and returns a new complex number that represents their product.
8. Complex divide( Complex c ) : this function divides the complex number invoking the function to c,
and returns a new complex number that represents the quotient.
9. void print() : the function that prints the complex number object. For eg., if, for the given object, the
value of real is 2.4 and that of imag is 3.7, this function should print 2.4 + 3.7i on the screen.
Apart from these functions, you should also define the getter and setter functions: getReal, getImag, setReal
and setImag.
Test the program by writing the main() function. Inside this function, you should prompt the user to enter real
and imaginary parts of two complex numbers ( and ). Use the operations defined in the z1 z2 class to perform the addition ( z1 + z2 ), subtraction ( z1 − z2 ),multiplication ( z1. z2 ), and division z2.
Solution: