#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Complex
{
public:
void Display()
{
cout<< "_real:"<<_real<<" "<<"_image:"<<_image<<endl;
}
//Complex()//无参数构造函数
//{
//}
//1.全缺省构造函数构造函数
Complex(double real=0.0,double image=0.0)
{
cout<< "Complex(double real=0.0,double image=0.0)"<<endl;
_real = real;
_image = image;
}
//2.拷贝函数
Complex(const Complex& d)//
:_real(d._real)
,_image(d._image)
{
cout<<" Complex(const Complex& d)"<<endl;//注意引用符,若为值传递则可能会引发无穷递归
}
//3.析构函数
~Complex()
{
cout<<"~Complex()"<<endl;
}
public:
Complex& operator=(const Complex& x)//赋值操作符重载
{
if(this!=&x)
{
this->_real = x._real;
this->_image = x._image;
}
return *this;
}
bool operator==(const Complex& x)//比较两个复数是否相等
{
return (this->_image==x._image)&&(this->_real==x._real);
}
//void operator+(const Complex& x)
//{
// this->_real = this->_real+x._real;
// this->_image = this->_image+x._image;
//}
Complex operator+(const Complex& x)
{
Complex tmp;
tmp._real = this->_real + x._real;
tmp._image = this->_image + x._image;
return tmp;
}
Complex& operator+=(const Complex& x)
{
this->_real += x._real;
this->_image += x._image;
return *this;
}
Complex& operator++()//前置++
{
this->_real += 1;
return *this;
}
Complex operator++(int) //后置++
{
Complex tmp(this->_real,this->_image);
this->_real += 1;
return tmp;
}
private:
double _real;
double _image;
};
void Test()
{
//构造函数
//Complex p;
//p.Display();
Complex d1;
Complex d2(2.0,1.0);
d1.Display();
d2.Display();
//拷贝构造函数
Complex d3(d1);
Complex d4 = d1;
d3.Display();
d4.Display();
}
//操作符的重载
void Test1()
{
Complex d1(2.0, 0.0);
Complex d2(2.0, 0.0);
Complex d;
d1.Display();
d2.Display();
d = d1;
d.Display();
bool ret;
ret = d1 == d2;
cout << "---------ret = " << ret << endl;
d = d1+d2;
d.Display();
d1 += d2;
d1.Display();
}
//自增的重载
void Test2()
{
Complex d1(0.0,0.0);
d1.Display();
d1++;
d1.Display();
}
int main()
{
//Test();
//Test1();
Test2();
return 0;
}