-
Complexes
I am totally stack on where to start on this. I have these directions:
Develop a class called complex. This class enables operations on so called complex numbers. These are of the form: realPart+imaginaryPart*i
a. Overload the addition operator (+) to add two complex numbers
b. Overload the subtraction operator (-) to subtract.
c. Overload the multiplication operator (*) to multiply
d. Overload the division operator (/) to divide
e. Overload the == and != operators for comparisons
f. Overload the operator (<<) to display the complex numbers.
After hours and hours, this is what I got so far:
#include <iostream.h>
class Complex
{
public:
...
Complex operator +(const Complex &op)
{
double real = _real + op._real, imag = _imag + op._imag;
return(Complex(real, imag));
}
...
};
Your help is appreciated, thanks.
-
What didn't you now????
You should say, where do tou need help.... Nowbody here will do your work... try fisrt, and then put your realy questions, for get help..
ok???
-
Sorry my English... ;) I just have some dificulties to write well in english...
-
i would do this for you, but 2 things
1) people on this board wouldnt like me very much
2) i have alot of homework tonight but i can get you a head start
Code:
// some odds and ends
class Complex
{
public://private:
double m_Real;
double m_Img;
public:
Complex(double Real = 1.0, double Img = 1.0);
Complex& operator=(const Complex& A);
//e. Overload the == and != operators for comparisons
//f. Overload the operator (<< ) to display the complex numbers.
};
// make these yourself
Complex operator-(const Complex& A, const Complex& B);
Complex operator/(const Complex& A, const Complex& B);
Complex operator*(const Complex& A, const Complex& B);
// constructor
Complex::Complex(double Real, double Img)
{
m_Real = Real;
m_Img = Img;
}
Complex operator+(const Complex& A, const Complex& B)
{
return Complex(A.m_Real + B.m_Real, A.m_Img + B.m_Img);
}
Complex& Complex::operator=(const Complex& A)
{
m_Real = A.m_Real;
m_Img = A.m_Img;
return *this; // A + B; also can be made another way
}
remember there is many ways to think of how to overload...many many ways!!!!!! this was my ugly fast thinking and i might be wrong...but i did this in 1 min...hehe