Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::complex

 

std::complex is an STL template class for a complex number.

 

#include <complex>
#include <iostream>

int main()
{
  const double real_part_1 = 3.0;
  const double imaginary_part_1 = 3.0;
  const std::complex<double> c(real_part_1,imaginary_part_1);

  const double real_part_2 = 4.0;
  const double imaginary_part_2 = 4.0;
  const std::complex<double> d(real_part_2,imaginary_part_2);

  const std::complex<double> sum(c + d);
  const std::complex<double> mult(c * d);
  std::cout << c << '\n';
  std::cout << d << '\n';
  std::cout << sum << '\n';
  std::cout << mult << '\n';
}

 

Screen output:

 

(3,3)
(4,4)
(7,7)
(0,24)

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppComplex.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp


 

 

 

 

 

main.cpp

 

#include <complex>
#include <iostream>

int main()
{
  const double real_part_1 = 3.0;
  const double imaginary_part_1 = 3.0;
  const std::complex<double> c(real_part_1,imaginary_part_1);

  const double real_part_2 = 4.0;
  const double imaginary_part_2 = 4.0;
  const std::complex<double> d(real_part_2,imaginary_part_2);

  const std::complex<double> sum(c + d);
  const std::complex<double> mult(c * d);
  std::cout << c << '\n';
  std::cout << d << '\n';
  std::cout << sum << '\n';
  std::cout << mult << '\n';


}

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml