i can't quite figure out how the arithmetic operators are supposed to be used, as 2 parameters are being passed in:
Code:
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
private:
double real; // real part
double imag; // imaginary part
public:
complex(double x=0.0, double y=0.0);
double magnitude() const; // return magnitude
double realPart() const; // return real part
double imagPart() const; // return imaginary part
complex operator- ();
friend complex operator+ (const complex& lhs, const complex &rhs);
friend complex operator- (const complex& lhs, const complex &rhs);
friend complex operator* (const complex& lhs, const complex &rhs);
friend complex operator/ (const complex& lhs, const complex &rhs);
friend bool operator== (const complex& lhs, const complex &rhs);
friend ostream& operator << (ostream& ostr, const complex &x);
friend istream& operator >> (istream& istr, complex &x);
};
double complex::magnitude() const
{
double temp = 0.0;
temp = (real * real) + (imag * imag);
temp = sqrt(temp);
}
double complex::realPart() const
{
return real;
}
double complex::imagPart() const
{
return imag;
}
complex operator+ (const complex& lhs, const complex &rhs)
{
return complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
}
complex operator- (const complex& lhs, const complex &rhs)
{
return complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
}
int main()
{
// complex number i= 0 + 1i
complex i(0,1), z1, z2;
// input values
cout << "Enter two complex numbers: ";
cin >> z1 >> z2;
cout << "Test the binary arithmetic operators:" << endl;
cout << " z1 + z2 = " << (z1 + z2) << endl;
cout << " z1 - z2 = " << (z1 - z2) << endl;
cout << " z1 * z2 = " << (z1 * z2) << endl;
cout << " z1 / z2 = " << (z1 / z2) << endl;
// test relational equality
if (z1 == z2)
cout << z1 << " = " << z2 << endl;
else
cout << z1 << " != " << z2 << endl;
// verify that i*i = -1 and that -i*i = 1
cout << "i*i = " << i*i << endl;
cout << "-i*i = " << -i*i << endl;
return 0;
}