C++语言程序设计中,有关运算符重载的相关规则与方法请参考:https://blog.csdn.net/PingBryant/article/details/123089725?spm=1001.2014.3001.5501
史上最全C++复数运算符重载代码如下:
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double x=0.0, double y=0.0); //构造函数
Complex(const Complex &c); //复制构造函数
~Complex(); //析构函数
Complex operator +(const Complex &c) const; //复数与复数相加
Complex operator -(const Complex &c) const; //复数与复数相减
//实数与复数相加,或者:Complex operator+(const double &number) const;
friend Complex operator +(const Complex &c, const double &number);
friend Complex operator +(const double &number, const Complex &c); //复数与实数相加
friend ostream& operator <<(ostream& out, const Complex &c); //重载<<
friend istream& operator >>(istream& in, Complex &c); //重载>>
Complex& operator ++(); //重载前置++(返回左值,用引用)
Complex operator ++(int); //重载后置++(返回右值,不用引用)
Complex& operator --(); //重载前置--
Complex operator --(int); //重载后置--
bool operator ==(const Complex &c); //重载==
bool operator !=(const Complex &c); //重载!=
//赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用
Complex& operator =(const Complex &c);
Complex operator +=(const Complex &c); //重载+=
private:
double real;
double imag;
};
Complex::Complex(double x, double y): real(x), imag(y){} //构造函数
Complex::Complex(const Complex &c) //复制构造函数
{
real = c.real;
imag = c.imag;
}
Complex::~Complex(){} //析构函数
Complex Complex::operator +(const Complex &c) const //复数与复数相加
{
return Complex(real+c.real, imag+c.imag);
}
Complex Complex::operator -(const Complex &c) const //复数与复数相减
{
Complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
//实数与复数相加,或者:Complex operator+(const double &number) const;
Complex operator +(const Complex &c, const double &number)
{
return Complex(c.real+number, c.imag);
}
Complex operator +(const double &number, const Complex &c) //复数与实数相加
{
return Complex(number+c.real, c.imag);
}
ostream& operator <<(ostream& out, const Complex &c) //重载<<
{
if(c.imag > 0)
{
out<<c.real<<"+"<<c.imag<<"i";
}
else
{
out<<c.real<<c.imag<<"i";
}
return out;
}
istream& operator >>(istream& in, Complex &c) //重载>>
{
in>>c.real>>c.imag;
return in;
}
Complex& Complex::operator ++() //重载前置++
{
real += 1;
imag += 1;
return *this;
}
Complex Complex::operator ++(int) //重载后置++
{
Complex old(*this); //调用复制构造函数
real += 1;
imag += 1;
return old;
}
Complex& Complex::operator --() //重载前置--
{
real -= 1;
imag -= 1;
return *this;
}
Complex Complex::operator --(int) //重载后置--
{
Complex old = *this; //这里和上面一样,同样是调用复制构造函数
real -= 1;
imag -= 1;
return old;
}
bool Complex::operator ==(const Complex &c) //重载==
{
if(real==c.real && imag==c.imag)
{
return true;
}
return false;
}
bool Complex::operator !=(const Complex &c) //重载!=
{
if(*this == c) //调用上面重载的==
{
return false;
}
return true;
}
//赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用
Complex& Complex::operator =(const Complex &c) //重载复制运算符=
{
if(*this != c) //调用上面重载的!=
{
real = c.real;
imag = c.imag;
}
return *this;
}
Complex Complex::operator +=(const Complex &c) //重载+=
{
real += c.real;
imag += c.imag;
return *this;
}
int main()
{
Complex c1(1.1, 2.2);
cout<<"c1 = "<<c1<<endl;
Complex c2(3.3, 4.4);
cout<<"c2 = "<<c2<<endl;
Complex c3;
cout<<"c3 = "<<c3<<endl;
c3 = c2;
cout<<"c3 = "<<c3<<endl;
cout<<"c3 != c1 : "<<(c3 != c1)<<endl;
cout<<"c3 == c1 : "<<(c3 == c1)<<endl;
cout<<"c1 + c2 : "<<c1+c2<<endl;
cout<<"c1 - c2 : "<<c1-c2<<endl;
cout<<endl;
Complex c4(5.5, 6.6);
cout<<"c4 = "<<c4<<endl;
cout<<"c1 += c4 : "<<(c1+=c4)<<endl;
cout<<endl;
cout<<"c1++ : "<<c1++<<endl;
cout<<"c2-- : "<<c2--<<endl;
cout<<"++c3 : "<<++c3<<endl;
cout<<"--c4 : "<<--c4<<endl;
Complex c5(6.6, 8.8);
cout<<"c5 : "<<c5<<endl;
cout<<"Please input c5: ";
cin>>c5;
cout<<"c5 : "<<c5<<endl;
return 0;
}
运行结果:
c1 = 1.1+2.2i
c2 = 3.3+4.4i
c3 = 00i
c3 = 3.3+4.4i
c3 != c1 : 1
c3 == c1 : 0
c1 + c2 : 4.4+6.6i
c1 - c2 : -2.2-2.2i
c4 = 5.5+6.6i
c1 += c4 : 6.6+8.8i
c1++ : 6.6+8.8i
c2-- : 3.3+4.4i
++c3 : 4.3+5.4i
--c4 : 4.5+5.6i
c5 : 6.6+8.8i
Please input c5: 5.6 7.3
c5 : 5.6+7.3i