Complex Number Class
A complex number is a number that has two components; the real component and the imaginary component.
a + bi
Arithmetic is defined as follows:
(a + bi) + (c + di) = (a + c) + (b + d)i
(a + bi) - (c + di) = (a - c) + (b - d)i
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
(a + bi) / (c + di) = (ac + bd) / (c**2+d**2) + [ (bc -ad) /(c**2+d**2)]i
Class Declaration
class complex
{
public:
complex();
complex(double,double);
double getReal() const;
void setReal(double);
complex operator+(complex) const;
complex operator-(complex) const;
complex operator*(complex) const;
complex operator/(complex) const;
private:
double real, imag;
};
complex::complex():real(0),y(0)
{ //default constructor
}
complex :: complex(double r, double im)
{
real = r;
imag = im;
}
complex complex::operator+(complex c) const
{
complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
complex complex::operator/(complex c) const
{
complex temp;
temp.real = (real*c.real + imag*c.imag)/
( pow(c.real,2) + pow(imag,2) );
temp.imag = (imag*c.real - real*c.imag)/
( pow(c.real,2) + pow(imag,2) );
return temp;
}
complex complex::operator*(complex c) const
{
complex temp;
temp.real = real*c.real – imag*c.imag;
temp.imag = real*c.imag + imag*c.real;
return temp;
}
Wednesday, March 31, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment