2023-6-7日作业补
条件运算符重载
#include<iostream>
using namespace std;
class Complex
{
int real;int imag;public:Complex(){}
Complex(int i,int j)
{
real=i;imag=j;
}
void show(){cout<<real<<"+"<<imag<<"i"<<endl;}
//算术运算符的重载,成员函数的方法
Complex&operator-(Complex&other)
{
static Complex temp;
temp.real=this->real-other.real;
temp.imag=this->imag-other.imag;
return temp;
}
//成员函数实现,关系运算符的重载
bool operator>(Complex&other)
{
if(this->real==other.real)
{
return this->imag>other.imag;
}
else
{
return this->real>other.real;
}
}
friend Complex&operator+=(Complex&p1,Complex&p2);
friend bool operator>(Complex&p1,Complex&p2);
};
//全局函数实现赋值运算符重载
Complex &operator+=(Complex&p1,Complex&p2)
{
p1.real+=p2.real;p1.imag+=p2.imag;
return p1;
}
//全局函数实现关系运算符重载
bool operator>(Complex&p1,Complex&p2){return&p1<&p2;}
int main()
{
Complex c1(6,4);
Complex c2(6,4);
Complex c3=c1-c2;
cout<<c1.operator>(c2)<<endl;
return 0;
}