#include<iostream>
using namespace std;
class Complex
{
friend istream& operator>>(istream& in, Complex& a);
public:
Complex(double real = 8.0, double image = 6.0) //构造函数
:_real(real)
, _image(image)
{
cout <<" Complex(double real, double image)" << endl;
}
Complex(const Complex& d) //拷贝函数
{
cout << "Complex(const Complex& d)" << endl;
_real = d._real;
_image = d._image;
}
~Complex() //析构函数
{
cout << "~Complex() " << endl;
_real = 0.0;
_image = 0.0;
}
Complex& operator=(const Complex& d) //赋值运算符重载
{
cout << "=" << endl;
if (this != &d)
{
_real = d._real;
_image = d._image;
}
return *this;
}
void Display()const //打印复数
{
cout << "_real:"<< _real;
cout << " _image:" << _image << endl;
}
bool operator==(const Complex& d) //==
{
cout << "==" << endl;
return this->_real == d._real
&& this->_image == d._image;
}
bool operator!=(const Complex& d) //!=
{
cout << "!=" << endl;
return this->_real != d._real
|| this->_image == d._image;
}
Complex operator+ (const Complex& d) //+
{
cout << "+" << endl;
Complex ret;
ret._real = (this->_real + d._real);
ret._image = (this->_image + d._image);
return ret;
}
protected:
double _real;
double _image;
};
istream& operator>>(istream& in, Complex& a)
{
return in>>a._real>>a._image;
}
int main(int argc, char *argv[])
{
Complex d1;
Complex d2(4.0, 6.6);
cin>>d1;
d1.Display();
d2.Display();
Complex d3;
d3 = d1 + d2;
d3.Display();
return 0;
}