operators: + - * / == != () = << >> - C complex.h

C examples for complex.h:operators

type

complex operators

From


<complex>
<complex.h>

std::complex operators Prototype

complex& operator= (const T& val);
complex& operator+= (const T& val);
complex& operator-= (const T& val);
complex& operator*= (const T& val);
complex& operator/= (const T& val);

complex& operator= (const complex& rhs);

template<class X> complex& operator= (const complex<X>& rhs);
template<class X> complex& operator+= (const complex<X>& rhs);
template<class X> complex& operator-= (const complex<X>& rhs);
template<class X> complex& operator*= (const complex<X>& rhs);
template<class X> complex& operator/= (const complex<X>& rhs);

template<class T> complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator+(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator+(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator-(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator*(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator*(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator/(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator/(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator+(const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& rhs);

bool operator==(const complex<T>& lhs, const complex<T>& rhs);
bool operator==(const complex<T>& lhs, const T& val);
bool operator==(const T& val, const complex<T>& rhs);

bool operator!=(const complex<T>& lhs, const complex<T>& rhs);
bool operator!=(const complex<T>& lhs, const T& val);
bool operator!=(const T& val, const complex<T>& rhs);

template<class T, class charT, class traits>
basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& istr, complex<T>& rhs);
template<class T, class charT, class traits>
basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& ostr, const complex<T>& rhs);

Description

Complex number operators

Parameters

Parameter Description
val Value representing a real numeric value without imaginary part.
rhs Right-hand side object of a complex type.
lhs Left-hand side object of a complex type (only for non-member functions).
istr Input stream object where the value of a complex object is inserted.
ostr Output stream object from where a complex value is extracted.

Template parameters

Item Description
T The template type (the value type) of the first complex object.
X The template type (the value type) of the second complex object.
charT, traits The template types for the basic_istream or basic_ostream object.

Return value

The left-hand side operand.

Demo Code


#include <iostream>
#include <complex>

int main ()/*from w  w  w. j a va2  s  . c  o  m*/
{
  std::complex<double> mycomplex;

  mycomplex = 10.0;   // 10
  mycomplex += 2.0;   // 12

  mycomplex = std::complex<double>(10.0,1.0);  // 10+i

  mycomplex = mycomplex + 10.0 ;               // 20+i

  if (mycomplex == std::complex<double>(20.0,1.0))
    std::cout << "mycomplex is " << mycomplex << '\n';

  return 0;
}

Related Tutorials