C++ <complex> - imag() Function
The C++ complex::imag function is used to access the imaginary part of the complex number. It can be used for two purpose:
- to return the imaginary part of the complex number.
- to set the imaginary part of the complex number.
Syntax
//get the imaginary part of complex number T imag() const;
//version 1 - get the imaginary //part of complex number T imag() const; //version 2 - set the imaginary //part of complex number void imag (T val);
Parameters
val |
Specify the value to set the imaginary part of the complex number. |
Return Value
Returns the imaginary part of the complex number in version 1 and nothing in version 2.
Example:
The example below shows the usage of complex::imag function.
#include <iostream> #include <complex> using namespace std; int main (){ complex<int> z (5, 10); //display the imaginary part of //the complex number cout<<"Imaginary part of z: "<< z.imag()<<"\n"; //changing the imaginary part z.imag(100); //display the imaginary part of //the complex number cout<<"Imaginary part of z: "<< z.imag()<<"\n"; return 0; }
The output of the above code will be:
Imaginary part of z: 10 Imaginary part of z: 100
❮ C++ <complex> Library