C++ Standard Library C++ STL Library

C++ <complex> - real() Function



The C++ complex::real function is used to access the real part of the complex number. It can be used for two purpose:

  • to return the real part of the complex number.
  • to set the real part of the complex number.

Syntax

//get the real part of complex number
T real() const;
//version 1 - get the real 
//part of complex number
T real() const; 

//version 2 - set the real 
//part of complex number
void real (T val);                       

Parameters

val Specify the value to set the real part of the complex number.

Return Value

Returns the real part of the complex number in version 1 and nothing in version 2.

Example:

The example below shows the usage of complex::real function.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<int> z (5, 10);

  //display the real part of 
  //the complex number
  cout<<"Real part of z: "<<
      z.real()<<"\n";

  //changing the real part
  z.real(50);
  
  //display the real part of 
  //the complex number
  cout<<"Real part of z: "<<
      z.real()<<"\n";

  return 0;
}

The output of the above code will be:

Real part of z: 5
Real part of z: 50

❮ C++ <complex> Library