// Compile: // g++ -o Complex Complex.cpp // // Run: // Complex // // This file uses more C++ syntax than you've seen before. That's ok. // Take your best guess at the semantics. #include class Complex { public: float real; float imag; Complex(float a = 0.0f, float b = 0.0f); Complex operator+(const Complex& other) const; Complex operator*(const Complex& other) const; }; Complex::Complex(float a, float b) : real(a), imag(b) {} Complex Complex::operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } Complex Complex::operator*(const Complex& other) const { return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real); } int main(int argc, const char* argv[]) { float x = 7.0f; float y = 1.2f; float z = x * y; printf("z = %f\n", z); Complex q(1.0f, 2.0f); Complex r(0.5f, 6.0f); // Overloaded * operator Complex s = q * r; printf("s = %f + %fi\n", s.real, s.imag); return 0; }