Reputation: 245
How can I use a complex variable in C++ code?
I do have the separate real and imaginary parts psi_rl
and psi_im
. Now I have to write psi = psi_rl + i psi_im
. How would I use one variable for this?
Upvotes: 0
Views: 4277
Reputation: 613511
Something like this should give you the basic idea:
class complex
{
public:
double real;
double imag;
complex(double real, double imag): real(real), imag(imag) {};
complex operator+(complex c) { return complex(this->real+c.real, this->imag+c.imag); };
};
int main(int argc, char* argv[])
{
complex a(1,2);
complex b(-3,6);
complex c = a+b;
return 0;
}
Upvotes: 1
Reputation: 283893
You should read the documentation for std::complex
, it will give you answers to these questions and many more.
Upvotes: 4