A complex number is of the form x+iy,
where i²=-1. x, y are called real and imaginary part
of the complex number. Complex numbers have vast applications in mathematics,
physics, and engineering. They have an arithematic governed by a series
of rules including the following:
Let u = a + ib,
v = c + id
|u|=sqrt(a²
+
b²) // Magnitude
u + v = (a +
c) + i(b + d)
u - v = (a -
c) + i(b - d)
u * v = (ac
- bd) + i(ad + bc)
u / v = (ac
+ bd)/(c² + d²) + i(bc - ad)/(c² +
d²)
In three different ways implement the class complex whose declaration is
//version 1.
class complex {
private:
double real, imag;
public:
complex(double r, double
i) {real = r; imag = i;}
complex() {real = 0.0,
imag = 0.0;}
void print()const; //print
complex form: x + iy
double magnitude()const;
complex add_c(const complex&
a, const complex& b);
complex mult_c(const
complex& a, const complex& b);
complex div_c(const complex&
a, const complex& b);
};
//version 2.
class complex {
private:
double real, imag;
public:
complex(double r, double
i) {real = r; imag = i;}
complex() {real = 0.0,
imag = 0.0;} //default constructor
void print()const; //print
complex form: x + iy
double magnitude()const;
friend complex operator+
(const complex& a, const complex& b);
friend complex operator-
(const complex& a, const complex& b);
friend complex operator*
(const complex& a, const complex& b);
friend complex operator/
(const complex& a, const complex& b);
};
//version 3.
class complex {
private:
double real, imag;
public:
complex(double r, double
i) {real = r; imag = i;}
complex() {real = 0.0,
imag = 0.0;} //default constructor
friend ostream& operator<<
(ostream& ostr, const complex& c);
double magnitude()const
complex operator+ (const
complex& a) const;
complex operator- (const
complex& a) const;
complex operator* (const
complex& a) const;
complex operator/ (const
complex& a) const;
};
In each version write a simple calculator to test addition, substraction multiplication and division of two complex numbers. Also print out the magnitude of the result of these operations.