-
Notifications
You must be signed in to change notification settings - Fork 6
Complex Number
Written by: Raj Ojha
The Complex
class is a C++ class that represents complex numbers with real and imaginary parts. It provides methods for performing various operations on complex numbers.
Complex(double r, double i) Creates a complex number with the given real and imaginary parts.
Complex a(3.0, 4.0);
Complex b(1.0, -2.0);
double getReal() const double getImaginary() const Returns the real and imaginary parts of the complex number, respectively.
Complex a(3.0, 4.0);
a.getReal();
a.getImaginary();
double magnitude() const Calculates and returns the magnitude (absolute value) of the complex number.
Complex a(3.0, 4.0);
a.magnitude();
double phase() const Calculates and returns the phase (argument) of the complex number in radians.
Complex a(3.0, 4.0);
a.phase();
Complex operator+(const Complex& other) const Overloads the + operator to perform complex number addition.
Complex a(3.0, 4.0);
Complex b(1.0, -2.0);
cout<<a+b<<endl;
Complex operator-(const Complex& other) const Overloads the - operator to perform complex number subtraction.
Complex a(3.0, 4.0);
Complex b(1.0, -2.0);
cout<<a-b<<endl;
Complex operator*(const Complex& other) const Overloads the * operator to perform complex number multiplication.
Complex a(3.0, 4.0);
Complex b(1.0, -2.0);
cout<<a*b<<endl;
Complex operator/(const Complex& other) const Overloads the / operator to perform complex number division.
Complex a(3.0, 4.0);
Complex b(1.0, -2.0);
cout<<a/b<<endl;