Question
Comment the following C++ code where the // symbol appears to explain what the statement means. // ... #include #include // This library is needed
Comment the following C++ code where the // symbol appears to explain what the statement means.
// ... #include
const Complex operator + (Complex& cl, Complex& c2); // const Complex operator - (Complex& c1, Complex& c2); // const Complex operator * (Complex& c1, Complex& c2); // const Complex operator / (Complex& c1, Complex& c2); // bool operator == (Complex& c1, Complex& c2); //
Complex::Complex() // { real = 0; imaginary=0; }
Complex::Complex(double r, double i) // { real = r; imaginary = i; } Complex::Complex(double realPart) // { real=realPart; imaginary = 0; }
double Complex::get_Real() // { return real; }
void Complex::set_Real(double r) // { real = r; }
double Complex::get_Imaginary() // { return imaginary; }
void Complex::set_Imaginary(double i) // { imaginary = i; }
const Complex operator + (Complex& c1, Complex& c2) // { double a = c1.get_Real() + c2.get_Real(); double b = c1.get_Imaginary() + c2.get_Imaginary(); return Complex(a,b); }
const Complex operator - (Complex& c1, Complex& c2) // { double a = c1.get_Real() - c2.get_Real(); double b = c1.get_Imaginary () - c2.get_Imaginary() ; return Complex(a,b); }
const Complex operator * (Complex& c1, Complex& c2) // { double a = (c1.get_Real() * c2.get_Real()) - (c1.get_Imaginary() * c2.get_Imaginary()); double b = (c1.get_Real() * c2.get_Imaginary()) + (c1.get_Imaginary() * c2.get_Real()); return Complex(a,b); }
const Complex operator / (Complex& c1, Complex& c2) // { double b = c1.get_Imaginary() * c2.get_Real(); double a = c1.get_Real() * c2.get_Imaginary(); return Complex(a,b); }
bool operator == (Complex& c1, Complex& c2) // { if ( c1.get_Real() == c2.get_Real() && c1.get_Imaginary() == c2.get_Imaginary() ) return true; else return false; }
ostream& operator << (ostream& out, Complex& r) // { double a = r.get_Real(); double b = r.get_Imaginary() ;
if (a != 0)out << a;
if (b < 0) out << "-" << abs(b) << "i"; if (b > 0 && b!=1 ) out << "+" << b << "i"; if ( b ==0) out << "0"; if (b == 1 && a != 0 ) out << "+" << "i"; if ( b == 1 && a == 0) out << "i"; return out; }
istream& operator >> (istream& in, Complex& r) // { double a,b; char plus, i; in >> a; in >> plus; in >> b; in >> i; if ( plus != '+' && plus != '-' ) { cout << "No plus or minus found " << endl; } if ( i != 'i') { cout << "no i found " << endl; } r.set_Real(a); r.set_Imaginary(b); return in; }
void main() { const Complex i(0,1); Complex c1; Complex c2;
cout << "Enter complex 1 in ths format ---> #+#i " << endl; cin >> c1; cout << "Enter complex2" << endl; cin >> c2;
cout << "\t c1 is: " << c1 << "\t c2 is: " << c2 << endl << endl; cout << "Adding " << c1 << " plus " << c2 << endl;
Complex t = c1 + c2; cout << c1 << " plus" << endl << c2 << endl << "equals \t" << t << endl << endl;
cout << "Subtracting " << c1 << " minus " << c2 << endl; t = c1 - c2; cout << c1 << " minus" << endl << c2 << endl << "equals \t" << t << endl << endl;
cout << "Multiplying " << c1 << " times " << c2 << endl; t = c1*c2; cout << c1 << " times" << endl << c2 << endl << "equals \t" << t << endl << endl;
cout << "Evaluating ..." << endl; if ( c2 == c1) cout << "They are equal" << endl; else cout << "They are not equal" << endl;
return; }
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