C++ Standard Library C++ STL Library

C++ <complex> - operator+ Function



The C++ <complex> operator+ function is used to apply plus operator to two complex numbers or a complex and a scalar.

Syntax

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);

Parameters

lhs Specify left-hand side complex number of matching type.
rhs Specify right-hand side complex number of matching type.
val Specify scalar value of matching type.

Return Value

Returns a new complex number as a result of performing operation.

Example:

The example below shows the usage of operator+ function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z1 (10, 20);
  complex<double> z2 (2, 3);
  complex<double> z3, z4;

  //result of z1 + 3.0 is stored in z3 
  z3 = z1 + 3.0;

  cout<<"z3 : "<<z3<<"\n";

  //result of z1 + z2 is stored in z4 
  z4 = z1 + z2;

  cout<<"z4 : "<<z4<<"\n"; 
    
  return 0;
}

The output of the above code will be:

z3 : (13,20)
z4 : (12,23)

❮ C++ <complex> Library

5