/* * The MyComplex template class header (MyComplex.h) * All template codes are kept in the header, to be included in program * (Follow, modified and simplified from GNU GCC complex template class.) */ #ifndef MY_COMPLEX_H #define MY_COMPLEX_H #include // Forward declaration template class MyComplex; template std::ostream & operator<< (std::ostream & out, const MyComplex & c); template std::istream & operator>> (std::istream & in, MyComplex & c); // MyComplex template class declaration template class MyComplex { private: T real, imag; public: // Constructor explicit MyComplex (T real = 0, T imag = 0) : real(real), imag(imag) { } // Overload += operator for c1 += c2 MyComplex & operator+= (const MyComplex & rhs) { real += rhs.real; imag += rhs.imag; return *this; } // Overload += operator for c1 += value MyComplex & operator+= (T value) { real += value; return *this; } // Overload comparison == operator for c1 == c2 bool operator== (const MyComplex & rhs) const { return (real == rhs.real && imag == rhs.imag); } // Overload comparison != operator for c1 != c2 bool operator!= (const MyComplex & rhs) const { return !(*this == rhs); } // Overload prefix increment operator ++c // (Separate implementation for illustration) MyComplex & operator++ (); // Overload postfix increment operator c++ const MyComplex operator++ (int dummy); /* friends */ // (Separate implementation for illustration) friend std::ostream & operator<< <>(std::ostream & out, const MyComplex & c); // out << c friend std::istream & operator>> <>(std::istream & in, MyComplex & c); // in >> c // Overloading + operator for c1 + c2 // (inline implementation for illustration) friend const MyComplex operator+ (const MyComplex & lhs, const MyComplex & rhs) { MyComplex result(lhs); result += rhs; // uses overload += return result; } // Overloading + operator for c + double friend const MyComplex operator+ (const MyComplex & lhs, T value) { MyComplex result(lhs); result += value; // uses overload += return result; } // Overloading + operator for double + c friend const MyComplex operator+ (T value, const MyComplex & rhs) { return rhs + value; // swap and use above function } }; // Overload prefix increment operator ++c template MyComplex & MyComplex::operator++ () { ++real; // increment real part only return *this; } // Overload postfix increment operator c++ template const MyComplex MyComplex::operator++ (int dummy) { MyComplex saved(*this); ++real; // increment real part only return saved; } /* Definition of friend functions */ // Overload stream insertion operator out << c (friend) template std::ostream & operator<< (std::ostream & out, const MyComplex & c) { out << '(' << c.real << ',' << c.imag << ')'; return out; } // Overload stream extraction operator in >> c (friend) template std::istream & operator>> (std::istream & in, MyComplex & c) { T inReal, inImag; char inChar; bool validInput = false; // Input shall be in the format "(real,imag)" in >> inChar; if (inChar == '(') { in >> inReal >> inChar; if (inChar == ',') { in >> inImag >> inChar; if (inChar == ')') { c = MyComplex(inReal, inImag); validInput = true; } } } if (!validInput) in.setstate(std::ios_base::failbit); return in; } #endif