template<class T>
class complex {
public:
typedef T value_type;
T real() const;
T imag() const;
complex(const T& re = 0, const T& im = 0);
complex(const complex& x);
complex& operator=(const complex& rhs);
complex& operator+=(const complex& rhs);
complex& operator-=(const complex& rhs);
complex& operator*=(const complex& rhs);
complex& operator/=(const complex& rhs);
complex& operator=(const T& rhs);
complex& operator=(const T& rhs);
complex& operator+=(const T& rhs);
complex& operator-=(const T& rhs);
complex& operator*=(const T& rhs);
complex& operator/=(const T& rhs);
friend complex<T>
operator+(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator+(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator-(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator-(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator*(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator*(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator/(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator/(const T& lhs, const complex<T>& rhs);
friend bool operator==(const complex<T>& lhs, const T& rhs);
friend bool operator==(const T& lhs, const complex<T>& rhs);
friend bool operator!=(const complex<T>& lhs, const T& rhs);
friend bool operator!=(const T& lhs, const complex<T>& rhs);
};
The template class describes an object that stores two objects of type T
, one that represents the real part of a complex
number and one that represents the imaginary part. An object of class T
:
Explicit specializations of template class complex
exist for the three floating-point types. In this implementation, a value
of any other type T
is type cast to double for actual calculations, with the double result assigned back to the stored
object of type T
.