Question: C++ the complex numbers using classes. Recall the program created a class called Complex for performing arithmetic with complex numbers. The class enables so-called complex
C++ the complex numbers using classes. Recall the program created a class called Complex for performing arithmetic with complex numbers. The class enables so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i
where i is:
For this assignment, you will make changes to the previous coding to now include the following upgrades:
Modify the class to enable input and output of complex numbers via overloaded + and - operators, respectively (addition and subtraction).
Modify the class to enable input and output of complex numbers via overloaded = and * operators, respectively (assignment and multiplication).
Modify the class to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the print function from the class). Use friend functions to accomplish this.
Overload the == and != operators to allow comparisons of complex numbers.
Add friend member functions to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the print function from the class)
#include
using std::string; using std::ostringstream;
class Complex { private: double realPart , imaginaryPart; public: Complex(double real=0 , double imaginary=0) { realPart = real; imaginaryPart = imaginary; }
Complex add(const Complex &c) { double new_real, new_imaginary; new_real = realPart + c.realPart; new_imaginary = imaginaryPart + c.imaginaryPart;
Complex sum(new_real, new_imaginary); return sum; }
Complex subtract(const Complex &c) { return Complex(realPart - c.realPart, imaginaryPart - c.imaginaryPart); }
Complex multiply(const Complex &c) { return Complex(realPart * c.realPart - imaginaryPart * c.imaginaryPart, imaginaryPart * c.realPart + realPart * c.imaginaryPart); }
void setComplexNumber(double real , double imaginary) {
realPart = real; imaginaryPart = imaginary;
}
string toString() { ostringstream convert; convert << realPart; string str1=convert.str();
convert << imaginaryPart; string str2=convert.str();
return str1 + " + " +str2 + "i"; }
}; int main() { for (double i = 1; i < 100; ++ i) { Complex a(i * 2, i + 2); Complex b(i * 3, i + 3); Complex c = a.add(b); // invoke add function and assign to object c std::cout << "Test case for Complex: add " << std::endl; std::cout << a.toString() << " + " << b.toString() << " = " << c.toString() << std::endl; a.setComplexNumber(i * 2, i + 2); // reset realPart and b.setComplexNumber(i * 3, i + 3); // and imaginaryPart std::cout << "Test case for Complex: subtract " << std::endl; c = a.subtract(b); // invoke subtract function and assign to object c std::cout << a.toString() << " - " << b.toString() << " = " << c.toString() << std::endl; std::cout << std::endl; a.setComplexNumber(i * 2, i + 2); // reset realPart and b.setComplexNumber(i * 3, i + 3); // and imaginaryPart std::cout << "Test case for Complex: multiply " << std::endl; c = a.multiply(b); // invoke multiply function and assign to object c std::cout << a.toString() << " * " << b.toString() << " = " << c.toString() << std::endl; std::cout << std::endl; } return 0; }
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 < 10; ++ i)
{
Complex y{i * 2.7, i + 3.2};
Complex z{i * 6, i + 8.3};
Complex x;
Complex k;
std::cout << "Enter a complex number in the form: (a, b) ? ";
std::cin >> k; // demonstrating overloaded >>
std::cout << "x: " << x << " y: " << y << " z: " << z << " k: "
<< k << ' '; // demonstrating overloaded <<
x = y + z; // demonstrating overloaded + and =
std::cout << " x = y + z: " << x << " = " << y << " + " << z << ' ';
x = y - z; // demonstrating overloaded - and =
std::cout << " x = y - z: " << x << " = " << y << " - " << z << ' ';
x = y * z; // demonstrating overloaded * and =
std::cout << " x = y * z: " << x << " = " << y << " * " << z << " ";
if (x != k) { // demonstrating overloaded !=
std::cout << x << " != " << k << ' ';
}
std::cout << ' ';
x = k;
if (x == k) { // demonstrating overloaded ==
std::cout << x << " == " << k << ' ';
}
std::cout << std::endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
