Question: For this assignment, you will make changes to the previous coding to now include the following upgrades: a) Modify the class to enable input and
For this assignment, you will make changes to the previous coding to now include the
following upgrades:
a)
Modify the class to enable input and output of complex numbers via overloaded +
and - operators, respectively (addition and subtraction).
b)
Modify the class to enable input and output of complex numbers via overloaded =
and * operators, respectively (assignment and multiplication).
c)
Modify the class to enable input and output of complex numbers via overloaded >>
and
class). Use friend functions to accomplish this.
d)
Overload the == and != operators to allow comparisons of complex numbers.
e)
Add friend member functions to enable input and output of complex numbers via
overloaded >> and
from the class).
Change the Complex class definition, the Complex class member-function definitions, and
modify the driver program as appropriate. Please use the following code and replace your
main function, or driver function, with the test code included below. Use these test cases
now, and execute the test cases successfully to ensure full credit.
int main()
{
for (double i = 1; i
{
Complex y{i * 2.7, i + 3.2};
Complex z{i * 6, i + 8.3};
Complex x;
Complex k;
std::cout
std::cin >> k; // demonstrating overloaded >>
std::cout
x = y + z; // demonstrating overloaded + and =
std::cout
x = y - z; // demonstrating overloaded - and =
std::cout
x = y * z; // demonstrating overloaded * and =
std::cout
if (x != k) { // demonstrating overloaded !=
std::cout
}
std::cout
x = k;
=================================================================================================
Below is what I have so far but I'm stuck. Please do not rewrite the code, simply fix it! (Code for copy below)



#include
using std::cout; using std::cin; using std::string; using std::endl;
// build your class and member functions here class Complex {
public:
double realPart; double imaginaryPart;
Complex() // default constructor { realPart = 0.0; imaginaryPart = 0.0; }
Complex (double r, double q) { realPart = r; imaginaryPart = q; }
// member function "add" adds two complex numbers // Pre: pass an object from Complex // Post: returns the sum of two complex numbers Complex add(Complex next) { double r = realPart + next.realPart; double q = imaginaryPart + next.imaginaryPart; return Complex(r,q); }
Complex operator+(Complex next) { return add(next); }
// member function "subtract" subtracts two complex numbers // Pre: pass an object from Complex // Post: returns the difference of two complex numbers Complex subtract (Complex next) { double r = realPart - next.realPart; double q = imaginaryPart - next.imaginaryPart; return Complex(r,q); }
Complex operator-(Complex next) { return subtract(next); }
// member function "multiply" multiplies two complex numbers // Pre: pass an object from Complex // Post: returns the product of two complex numbers Complex multiply (Complex next) { double real1 = realPart * next.realPart; // 1st real element * 2nd real element produces a real element double real2 = (imaginaryPart * next.imaginaryPart) * -1; // 1st imag. element * 2nd imag. element produces a real element * i^2 where i^2 = -1 double imaginary1 = realPart * next.imaginaryPart; // 1st real element * 2nd imag. element produces an imag. element double imaginary2 = imaginaryPart * next.realPart; // 1st imag. element * 2nd real element produces an imag. element
double r = real1 + real2; double q = imaginary1 + imaginary2; return Complex(r,q); }
Complex operator*(Complex next) { return multiply(next); }
void setComplexNumber (int a, int b) { realPart = a; imaginaryPart = b; }
};
int main() { for (double i = 1; i > k; // demonstrating overloaded >> std::cout include #include #include iostream
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
