Step Twenty-Four Complex Numbers: Objects and Operator Overloading
Complex Numbers
The complex number system objectives:
- The imaginary unit i = sqrt(-1) where i2 = -1
- The standard form of a complex number: a + bi where a
and b are real numbers,a is called the real part
and bi is called the imaginary part. If b = 0, then the result
is a real number and if a = 0 then it is called a pure imaginary number.
- Two complex numbers a + bi and c + di are equal iff a = c and b = d.
- addition: (a + bi) + (c + di) = (a + c) + (b + d)i
- subtraction: (a + bi) - (c + di) = (a - c) + (b - d)i
- multiplication: (a + bi) x (c + di) = (ac - bd) + (bc + ad)i
- complex conjugates: a + bi and a - bi.
Notice that (a + bi)(a - bi) = a2 + b2 which is a real number.
- division: (a + bi) / (c + di) = [(a + bi) x (c - di)] / [(c + di) x (c - di)] = . . .
- The rules for complex arithmetic extend real number arithmetic. A real
number a can be written a + 0i so the reals are a subset of
the complex numbers and complex arithmetic used with real numbers, give
the usual real number results.
Great, now that we have reviewed all of that fun stuff, let's make some complex numbers. We
can't just do this:
complex num1;
so we need to do some background work, so let's create a simple complex class:
#include <iostream>
using namespace std;
class Complex {
public:
Complex(float realpart=0, float imaginarypart=0);
Complex Add(const Complex &c) const;
float GetReal() const;
float GetImaginary() const;
void SetReal(float r);
void SetImaginary(float i);
private:
float real;
float imaginary;
};
Complex::Complex(float realpart, float imaginarypart) {
SetReal(realpart);
SetImaginary(imaginarypart);
}
void Complex::SetReal(float r) {
real = r;
}
void Complex::SetImaginary(float i) {
imaginary = i;
}
float Complex::GetReal() const {
return real;
}
float Complex::GetImaginary() const {
return imaginary;
}
Complex Complex::Add(const Complex &c) const {
return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}
Complex operator+(const Complex &c1, const Complex &c2) {
return c1.Add(c2);
}
int main(void) {
Complex x(-1,3);
Complex y(5,4);
Complex z = x + y;
cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
return 0;
}
If you want to see if you understand this, get the program working
and then extend it to include subtraction, and multiplication. Before
you try division, you might create a new program called fraction.cpp
modeled after this one and make all of the changes needed to implement
a fraction class (this time with a numerator and denominator instead of a real
and imaginary part). The arithmetic operations with fractions are a little
more involved because you may have to reduce the resulting fraction. A
good approach might be to create a gcd (greatest common divisor) function
and use it to reduce your fractions if necessary (i.e. the gcd is not 1).
Of course, you want to make sure the denominator never gets set to zero.
Advance to Step Twenty-Five
Return to the Table of Contents