## Question

o Overload the following operators:

- <<, >>, +, -, *, /, ==, !=

o How to use classes.

• Related SLO

o Develop properly structured multifile programs with automatic compilation.

o Use classes and operator overloading in C++.

• Instructions

o Create a makefile named makefile that will automicatlly compile, link, and create an executable for this assignment.

- IMPORTANT: Be sure to use g++ as the compiler NOT gcc as we are now programming in C++.

- Do NOT submit a makefile with any other name.

- There are no extra .h or .cpp needed for this assignment.

o Create a program named LastnameFirstname22.cpp, that does the following:

- Modify the print() member function so that it add parenthesis around the entire complex number

- Example: (0 + 0i)

- Add the following to the Complex class definition:

- Overloaded >> for cin

- Signature: friend istream &operator>>(istream &in, Complex &c)

- Reads in a double, then another double, then a character (for 'i').

- You are essentially reading in 3 things, but only 2 of those things (the first and second) will get stored in the real and imaginary data members. The third thing to read in is to get rid of the 'i' from the input stream.

- Overloaded << for cout

- Signature: friend ostream &operator<<(ostream &out, const Complex &c)

- A Complex object should be output in the following way: (a+bi)

- However, you will need to ensure that if b is positive, print the plus '+', and if b is negative, print the minus '-'.

- Overloaded +

- Signature: friend Complex operator+(const Complex &c1, const Complex &c2)

- Formula: (a + bi) + (c + di) = (a + c) + (b + d)i

- Overloaded -

- Signature: Complex operator-(const Complex &c)

- Formula: (a + bi) - (c + di) = (a - c) + (b - d)i

- Overloaded /

- Signature: Complex operator/(const Complex &c)

- Formula: (a + bi) / (c + di) = (a*c + b*d)/(c*c + b*d) + (b*c - a*d)/(c*c + d*d)i

- Overloaded *

- Signature: Complex operator*(const Complex &c)

- Formula: (a + bi) * (c + di) = (a*c - b*d) + (b*c + a*d)i

- Overloaded ==

- Signature: friend bool operator==(const Complex &c1, const Complex &c2)

- Compares the real parts and the imaginary parts. If the real parts are the same and the imaginary parts are the same, then return true, otherwise return false.

- Overloaded =!

- Signature: friend bool operator!=(const Complex &c1, const Complex &c2)

- Return the negation of the ==

- return !(c1 == c2);

- Copy and paste this main() function into your own program to test out your working Complex class. You should get the same output as in the example output.

o Be sure to have a program description at the top and in-line comments.

- Be clear with your comments and output to the user, so I can understand what you're program is doing.

Example Output

% make

% ./program

Enter a Complex number in the form 'a+bi': 3+4i

Enter another Complex number in the form 'a+bi': 3+5i

Test the + operator:

Destructor for: (6 + 9i)

(3+4i) + (3+5i) = (6+9i)

Test the - operator:

Destructor for: (0 + -1i)

(3+4i) - (3+5i) = (0-1i)

• Test the / operator:

Destructor for: (0.852941 + -0.0882353i)

(3+4i) / (3+5i) = (0.852941-0.0882353i)

Test the * operator:

Destructor for: (-11 + 27i)

(3+4i) * (3+5i) = (-11+27i)

Test the == operator:

(3+4i) == (3+5i) = false

Test the != operator:

(3+4i) != (3+5i) = true

Destructor for: (-11 + 27i)

Destructor for: (3 + 5i)

Destructor for: (3 + 4i)

% ./program

Enter a Complex number in the form 'a+bi': 5+3i

Enter another Complex number in the form 'a+bi': -5-3i

Test the + operator:

Destructor for: (0 + 0i)

(5+3i) + (-5-3i) = (0+0i)

Test the - operator:

Destructor for: (10 + 6i)

(5+3i) - (-5-3i) = (10+6i)

Test the / operator:

Destructor for: (-1 + 0i)

(5+3i) / (-5-3i) = (-1+0i)

Test the * operator:

Destructor for: (-16 + -30i)

(5+3i) * (-5-3i) = (-16-30i)

Test the == operator:

(5+3i) == (-5-3i) = false

Test the != operator:

(5+3i) != (-5-3i) = true

Destructor for: (-16 + -30i)

Destructor for: (-5 + -3i)

Destructor for: (5 + 3i)

% ./program

Enter a Complex number in the form 'a+bi': 3+4i

Enter another Complex number in the form 'a+bi': 3+4i

Test the + operator:

Destructor for: (6 + 8i)

(3+4i) + (3+4i) = (6+8i)

Test the - operator:

Destructor for: (0 + 0i)

(3+4i) - (3+4i) = (0+0i)

Test the / operator:

Destructor for: (1 + 0i)

(3+4i) / (3+4i) = (1+0i)

Test the * operator:

Destructor for: (-7 + 24i)

(3+4i) * (3+4i) = (-7+24i)

Test the == operator:

(3+4i) == (3+4i) = true

Test the != operator:

(3+4i) != (3+4i) = false

Destructor for: (-7 + 24i)

Destructor for: (3 + 4i)

Destructor for: (3 + 4i)

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <iostream>using namespace std;

class Complex

{

private:

double real;

double imaginary;

public:

Complex();

Complex(double realParam, double imaginaryParam);

Complex(const Complex& copyComplex);

~Complex();

void print() const;

void set(double a, double b);

double getReal() const;

double getImaginary() const;

Complex add(const Complex& addComplex) const;

Complex subtract(const Complex& subtractComplex) const;

Complex divide(const Complex& divideComplex) const;

Complex multiply(const Complex& multiplyComplex) const;

friend istream& operator >> (istream& in, Complex& c);

friend ostream& operator << (ostream& out, const Complex& c);

friend Complex operator + (const Complex& c1, const Complex& c2);

friend Complex operator - (const Complex& c1, const Complex& c2);

friend Complex operator / (const Complex& c1, const Complex& c2);

friend Complex operator * (const Complex& c1, const Complex& c2);

friend bool operator == (const Complex& c1, const Complex& c2);

friend bool operator != (const Complex& c1, const Complex &c2);

};

/*

* Constructor, sets the number to 0 + 0i

*/

Complex::Complex()

{

real = 0;

imaginary = 0;

}

/*

* Constructor, sets the number to realParam + imaginaryParam*i

*/

Complex::Complex(double realParam, double imaginaryParam)

{

real = realParam;

imaginary = imaginaryParam;

}

/*

* Copy constructor, copies copyComplex to object

*/

Complex::Complex(const Complex& copyComplex)

{

*this = copyComplex;

}...

By purchasing this solution you'll be able to access the following files:

Solution.cpp.