#include #include "complex.h" // The values for the default arguments only appear in the class declaration Complex::Complex(double x, double y) : real_(x), imag_(y) {} // Copy constructor Complex::Complex(Complex const& old) : real_(old.real_), imag_(old.imag_) {} // Assignment operator Complex& Complex::operator= (Complex const& rhs) { real_ = rhs.real_; imag_ = rhs.imag_; return *this; } // Compute and return the magnitude of the complex number. double Complex::Magnitude() const { return sqrt(real_*real_ + imag_*imag_); } // Addition operator as a member function. Complex Complex::operator+ (Complex const& rhs) const { double re = real_ + rhs.real_; double im = imag_ + rhs.imag_; return Complex(re, im); } // Subtraction operator as a non-member function. Complex operator- (Complex const& lhs, Complex const& rhs) { return Complex(lhs.Real()-rhs.Real(), lhs.Imaginary()-rhs.Imaginary()); } // Unary negation operator. Note that there are no arguments. Complex Complex::operator- () const { return Complex(-real_, -imag_); } // Input stream operator as a friend function istream& operator>> (istream & istr, Complex & c) { istr >> c.real_ >> c.imag_; return istr; } // Output stream operator as an ordinary non-member function ostream& operator<< (ostream & ostr, Complex const& c) { if (c.Imaginary() < 0) ostr << c.Real() << " - " << -c.Imaginary() << " i "; else ostr << c.Real() << " + " << c.Imaginary() << " i "; return ostr; }