Name

complex class template — Complex number template

Synopsis

template<typename T>
class complex {
public:
  typedef T value_type;
  complex(const T& re = T(  ), const T& im = T(  ));
  complex(const complex& z);
  template<typename X> complex(const complex<X>& z);
  T real(  ) const;
  T imag(  ) const;
  complex& operator= (const T& x);
  complex& operator+=(const T& x);
  complex& operator-=(const T& x);
  complex& operator*=(const T& x);
  complex& operator/=(const T& x);
  complex& operator=(const complex& z);
  template<typename X>
    complex& operator= (const complex<X>& z);
  template<typename X>
    complex& operator+=(const complex<X>& z);
  template<typename X>
    complex& operator-=(const complex<X>& z);
  template<typename X>
    complex& operator*=(const complex<X>& z);
  template<typename X>
    complex& operator/=(const complex<X>& z);
};

The complex class template represents a complex number. The <complex> header specializes the template for the float, double, and long double types. You can instantiate complex<> for any type that behaves in the manner of the fundamental numeric types.

The type definition is a straightforward representation of a complex number. Basic assignment operators are defined as member functions, and arithmetic operators are defined as global functions.

template<typename X> complex (const complex<X>& z)

Constructs a complex<T> object by copying the members from z. Effectively, this converts a complex object instantiated for one type to a complex object of another type.

T real ( ) const

Returns the real part of *this.

T imag ( ) const

Returns the imaginary part of *this.

complex& operator= (const T& x)

Assigns x to the real part of *this and 0 to the imaginary part. Returns *this.

complex& operator+= (const T& x)

Adds x to the real part of *this, leaving the imaginary part alone. Returns *this.

complex& operator-= (const T& x)

Subtracts x from the real part of *this, leaving the imaginary part alone. Returns *this.

complex& operator*= (const T& x)

Multiplies the real and imaginary parts of *this by x. Returns *this.

complex& operator/= (const T& x)

Divides the real and imaginary parts of *this by x. Returns *this.

complex& operator= (const complex& z)

Assigns the real and imaginary parts of z to *this. Returns *this.

template<typename X> complex& operator= (const complex<X>& z)

Assigns the real and imaginary parts of z to *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator+= (const complex<X>& z)

Adds z to *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator-= (const complex<X>& z)

Subtracts z from *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator*=( const complex<X>& z)

Multiplies *this by z. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator/= (const complex<X>& z)

Divides *this by z. Returns *this. Note that z and *this can have different template parameter types.