// ch8-1 a15
#include <iostream>
using namespace std;
class complex {
public:
complex() {
real = 0;
imaginary = 0;
}
complex(double _real) {
real = _real;
imaginary = 0;
}
complex(double _real, double _imaginary) {
real = _real;
imaginary = _imaginary;
}
bool operator==(const complex& c) {
return real == c.real && imaginary == c.imaginary;
}
// complex operator+(const complex& c) {
// complex tmp(real + c.real, imaginary + c.imaginary);
// return tmp;
// }
//
// complex operator-(const complex& c) {
// complex tmp(real + c.real, imaginary + c.imaginary);
// return tmp;
// }
//
// complex operator*(const complex& c) {
// complex tmp(real * c.real - imaginary * c.imaginary, c.real * imaginary + real * c.imaginary);
// return tmp;
// }
//
// ostream& operator<<(ostream& out) {
// out << real << " " << imaginary << endl;
// return out;
// }
//
// istream& operator>>(istream& in) {
// in >> real >> imaginary;
// return in;
// }
double real;
double imaginary;
};
int main()
{
// test constructors
complex x, y(3), z(-3.2, 2.1);
cout <<"x = " << x << " y = " << y << " z = " << z << endl << endl;
x = complex(3, -4);
cout << "testing members and support functions as well as”
<< " output operator:\n"
<< "complex number x = " << x << endl
<< "real part: " << x.real() << endl
<< "real part from friend real(x): " << real(x) << endl
<< "imaginary part: " << x.imag() << endl
<< "imaginary part from friend imag(x) : " << imag(x) << endl
<< "norm: " << norm(x) << endl << endl;
cout << "test complex arithmetic and output routines: \n\n";
y = complex (1, -1);
cout <<"x = " << x << " y = " << y << " z = " << z << endl << endl;
z = x + y;
cout <<"z = x + y = " << z << endl;
z = x * y;
cout <<"z = x * y = " << z << endl;
z = x - y;
cout <<"z = x - y = " << z << endl;
z = x / y;
cout <<"z = x / y = " << z << endl << endl;
//test of automatic conversion double -> complex by the constructor.
double d(2.0);
cout << "d: " << d << " x: " << x <<endl;
cout << "x+d: " ;
z = x + d;
cout << z << endl;
z = x - d;
cout << "x-d: " ;
cout << z << endl;
z = x * d;
cout << "x*d: " ;
cout << z << endl;
z = x / d;
cout << "x/d: " ;
cout << z << endl;
z = d + x;
cout << "d+x: " ;
cout << z << endl;
z = d - x;
cout << "d-x: " ;
cout << z << endl;
z = d * x;
cout << "d*x: " ;;
cout << z << endl;
z = d / x;
cout << "d/x: " ;;
cout << z << endl;
//test whether double/complex and complex/complex give same result:
complex two(2,0);
cout << "two/x: ";
cout << two/x << endl;
cout << "\nGetting data from standard input: \n";
cin >> x >> y;
cout <<"data read is: x = " << x << " y = " << y << endl << endl;
return 0;
}