Question
C++ For this assignment, you will make changes to the previous coding to now include the following upgrades: Modify the class to enable input and
C++ 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).
Change the Complex class definition, the Complex class member-function definitions, and modify the driver program as appropriate.
#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 < 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started