Question: c++ problem 1) Complex Number Class Extend the Complex class that you completed in last week's Lab Exercises. Recall that a complex number is oftheforma+b

c++ problem

1) Complex Number Class

Extend the Complex class that you completed in last week's Lab Exercises. Recall that a complex number is oftheforma+biwhereaandbarerealnumbersandi2 =-1. Forexample,2.4+5.2iand5.73-6.9iare complex numbers. a is referred to as the real part of the complex number and bi the imaginary part.

Make the following changes to the Complex class definition:

a) Add a constructor that will take two double arguments and initialize the real and imaginary components respectively.

b) Replacetheinputandoutputmethodswithoverloaded>>and<friend functions of the class. The output should be realValue + imaginaryValue i e.g., :

9.3 + 5.7i 12.4 - 8.4i

If the imaginaryValue component is zero, simply output the realValue as a 'regular' floating-point value. Also, if the imaginaryValue is negative, replace the '+' symbol with '-'. You may input the complex value in any format you wish.

c) Provide an overloaded addition operator that will return the sum of two Complex objects. For complex numbers a + bi and c + di, addition is defined as

(a+c) + (b+d)i.

4) Provide an overloaded negation operator ( unary - ) that will negate both the real and imaginary parts of a complex number. For example if z is the complex number 3 - 2i, then -z should return a complex number -3 + 2i.

Thoroughly test your Complex number class by writing a test program that constructs various Complex values and displays the results until you are convinced that it is operating correctly. Include the following statements in your test program and show the results to a TA:

 Complex c1,c2,c3; cout << "Enter two complex values: "; cin >> c1 >> c2; c3 = c1+c2; cout << "The sum is: " << c3 << endl; cout << "and negating the sum yields: " << -c3 << endl; 

here is the previous code.

#include #include using namespace std; class Complex { private: double real; double imag; public: Complex(); void input(istream& in); void output(ostream& out); double getReal(); double getImag(); void setReal(double r); void setImag(double i);

};

Complex::Complex() { real = imag = 0; }

void Complex::input(istream &in) { in>>real; in>>imag; } void Complex::output(ostream &out) { out<

int main() { Complex c1, c2; cout<<"Enter values for real and imaginary coefficients: "; c1.input(cin); cout<<"Enter values for real and imaginary coefficients: "; c2.input(cin); c1.output(cout); c2.output(cout); double temp; cout<<"Enter a new value for the real coefficient: "; cin>>temp; c2.setReal(temp); cout<<"The new real coefficient is "<

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!