// ---------- // complexx.h // ---------- // Project 2 of Com S 229 // Due at 11:59pm on Monday, Sep 28, 2009 // // Implement the following class. #ifndef COMPLEX_H #define COMPLEX_H #include #include using namespace std; class complex { // friend functions are defined outside the public & private sections. friend ostream& operator<< (ostream& ostr, const complex& x); // see proj2.pdf friend istream& operator>> (istream& istr, complex& x); // for input/output formats. public: complex (double x = 0.0, double y = 0.0); // constructor // access member functions double realPart() const; // return real part double imagPart() const; // return imaginary part double magnitude() const; // return magnitude double phase() const; // return phase complex sqrt() const; // square root with nonnegative real part complex pow(int n) const; // nth power, more efficiently implemented using magnitude and // phase angle. complex operator+ (const complex& rhs); // complex addition complex operator- (const complex& rhs); // complex subtraction complex operator* (const complex& rhs); // complex multiplication complex operator/ (const complex& rhs); // complex division bool operator== (const complex& rhs); // equality complex operator-() const; // negation of a complex number private: double real; // real part double imag; // imaginary part }; // evaluate the polynomial f(z) = z^3 - 3 z^2 + 4 z - 2 complex f(const complex& z); #endif