Question: Design a class for complex numbers called Complex. It must have two private variablesone for the real part and one for the imaginary part of
Design a class for complex numbers called Complex. It must have two private variablesone for the real part and one for the imaginary part of the complex number. Write appropriate constructor, accessor and mutator functions to create, get and set the objects and member variables. Implement the following for the class.
a) A friend function that adds two Complex objects and returns the sum as a Complex object to be assigned to a different Complex object.
b) A member function that accepts another Complex object as an argument, computes the sum, and returns the sum as a Complex object to be assigned to a different Complex object.
c) Write operator overloading functions for + (plus), -(minus) and *(multiply) to perform addition, subtraction and multiplication of complex numbers. Your functions must accept two constant Complex objects passed by reference.
d) Write operator overloading functions for >> and << to read and write Complex objects from the keyboard and on the screen respectively. While displaying the object, you need to make sure, they are displayed in the format of Re+Imi, where Re is the real part, Im is the imaginary part and i indicates the imaginary part of the complex number.
For example, if real part is 6 and imaginary part is -3, display 6-3i.
e) Create two objects in your main function and assign values to their member variables. Use any method you see fit. Demonstrate the functions you wrote in (a) through (c) using the objects you have just created. Youll need to use appropriate objects to store the results of each operation. Use the accessor function to show the result of each operation.
f) Declare a new object of type Complex. Use your overloaded operators >> and << with cin and cout to get the values for the object and then display the object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
