Skip to content

Complex Number

maruf hasan edited this page Oct 4, 2023 · 1 revision

Complex Number Class

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.

Class Details

Constructor

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);

Getter Functions

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();

Magnitude

double magnitude() const Calculates and returns the magnitude (absolute value) of the complex number.

Complex a(3.0, 4.0);
a.magnitude();

Phase

double phase() const Calculates and returns the phase (argument) of the complex number in radians.

Complex a(3.0, 4.0);
a.phase();

Addition Operator

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;

Subtraction Operator

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;

Multiplication Operator

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;

Division Operator

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;