#include #include #include using namespace std; class Complex { public: double real, imag; Complex () { real = 0.0; imag = 0.0; } Complex (double r, double i) { real = r; imag = i; } double abs () { return sqrt (real*real + imag*imag); } void normalize () { double d = abs(); real = real/d; imag = imag/d; } Complex operator + (const Complex b); const Complex& operator = (const Complex b); }; Complex Complex::operator + (const Complex b) { Complex sum (real + b.real, imag + b.imag); return sum; } const Complex& Complex::operator = (const Complex b) { real = b.real; imag = b.imag; return *this; } ostream& operator << (ostream& os, Complex num) { os << num.real; if (num.imag > 0) { os << " + " << num.imag << "i"; } else if (num.imag < 0) { os << " - " << -num.imag << "i"; } return os; } int main () { Complex y (3.0, 4.0); double result = y.abs (); cout << "|" << y << "| = " << result << endl; Complex num1 (3.0, 2.0), num2 (1.0, 5.0); cout << "Your two numbers are " << num1 << " and " << num2 << endl; Complex num3 (1, -4); cout << num3 << endl; Complex realNum (20,0); cout << "realNum = " << realNum << endl; Complex a, b, c; c.real = 1.0; c.imag = 2.0; a = b = c; cout << "a = " << a << ", b = " << b << ", c = " << c << endl; a.normalize(); cout << "a = " << a << endl; cout << "|a| = " << a.abs() << endl; Complex z (1, 2); cout << y + z << endl; return 0; }