Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Complete programming project Complex Numbers using C++ Create a class called

ID: 3671210 • Letter: 1

Question

1. Complete programming project Complex Numbers using C++

     Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form

            realPart + imaginaryPart * i

     where i is

                sqrt (-1)

     use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Define public setters and getters to access the private data members. Also, provide functions that perform the following tasks:

A) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together. Implement the addition operation in three ways:

i. Implement a member function of class Complex called add() that must do the following: accept one Complex number rhs for an argument, add rhs to the data members in the object that invokes the add() function, and return the result.

ii. Implement a non-member function called add(). Make sure it has the same name as the one defined in part (i). Place the prototype/declaration for this function outside of the Complex number class’s declaration (below the class declaration). Place its definition inside of the Complex.cpp file. The function must do the following: accept two Complex numbers called lhs and rhs for arguments, add lhs andrhs together, and return the result.

iii. Implement a non-member overloaded addition (+) operator. Place the prototype/declaration for this function outside of the Complex number class’s declaration (below the class declaration). Place its definition inside of the Complex.cpp file. The function must do the following: accept two Complex numbers called lhs and rhs for arguments, add lhs and rhs together, and return the result. Note: the overloaded + is a binary operation, which requires two arguments! Use the following prototype/declaration:

Complex operator+ (const Complex &lhs, const Complex &rhs);

     Note: overloading the + operator allows for us to use statements such    

as: c3 = c1 + c2, where c1, c2, and c3 are Complex numbers. Another interpretation is: c3 = c1.operator+(c2).

Now place the following statements in a test driver, and use the debugger to watch each of the Complex numbers:

          Complex c1(3.5, 2), c2(5.5, 7), c3;

          c3 = c1.add(c2);  // member add () function

          c3 = add(c1, c2); // non-member add () function

          c3 = c1 + c2;         // overloaded + operator

B) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the operand. Solve this problem by overloading the subtraction (–) operator.

C) Printing Complex numbers in the form a + bi, where a is the real part and b is the imaginary part. Implement the print operation in two ways:

i. Implement a member function of class Complex called print() that must do the following: accept no arguments, insert the real and imaginary parts of the number into the output stream in the form a + bi, and return nothing.

ii. Implement a non-member overloaded stream insertion (<<) operator. Place the prototype/declaration for this function outside of the Complexnumber class’s declaration (below the class declaration). Place its definition inside of the Complex.cpp file. The function must do the following: accept one ostream object called lhs and one Complex number called rhs for arguments, insert the real and imaginary parts of the number into the output stream in the form a + bi, and return the ostream object (so we can chain << together!). Use the following prototype/declaration:

ostream & operator<< (ostream &output, const Complex &rhs);

          Assuming that you still have the same Complex numbers instantiated: c1, c2, and c3. Place the following statements in a test driver:

                              c1.print();

c2.print();

                              c3.print();

                              cout << c1 << “ “ << c2 << “ “ << c3 << endl;

Explanation / Answer

__________________________________

_____________________________________