C++类的运算符重载
目标
让自定义的类直接使用运算符运算
代码
头文件及类定义
#include <iostream>using namespace std;
class Complex
{int rel;int vir;
public:void show(){cout <<"("<<this->rel<<","<<this->vir<<"i"<<")"<<endl;}Complex(int rel,int vir):rel(rel),vir(vir){}Complex(){}Complex operator+(const Complex other);Complex operator-(const Complex other);Complex operator/(const Complex other);Complex operator*(const Complex other);Complex operator%(const Complex other);bool operator==(const Complex other);bool operator>(const Complex other);bool operator<(const Complex other);bool operator&&(const Complex other);bool operator||(const Complex other);bool operator!();Complex operator++();Complex operator--();friend Complex operator++(Complex &c1,int);friend Complex operator--(Complex &c1,int);};
自增自减运算符
//后自增
Complex operator++(Complex &c1,int)
{Complex temp;temp.rel=(c1.rel)++;temp.vir=(c1.vir)++;cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//后自减
Complex operator--(Complex &c1,int)
{Complex temp;temp.rel=c1.rel--;temp.vir=(c1.vir)--;cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//前自增
Complex Complex::operator++()
{Complex temp;temp.rel=++(this->rel);temp.vir=++(this->vir);cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//前自减
Complex Complex::operator--()
{Complex temp;temp.rel=--(this->rel);temp.vir=--(this->vir);cout <<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
逻辑判断运算符
//等于判断
bool Complex::operator==(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"=="<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel==other.rel)&&(this->vir==other.vir))<<endl;return (this->rel==other.rel)&&(this->vir==other.vir);
}
//大于判断
bool Complex::operator>(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<">"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel>other.rel)&&(this->vir>other.vir))<<endl;return (this->rel>other.rel)&&(this->vir>other.vir);
}//小于判断
bool Complex::operator<(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"<"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel<other.rel)&&(this->vir<other.vir))<<endl;return (this->rel<other.rel)&&(this->vir<other.vir);
}
逻辑与或非
//逻辑与
bool Complex::operator&&(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"&&"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel||this->vir)&&(other.rel||other.vir))<<endl;return (this->rel||this->vir)&&(other.rel||other.vir);
}
//逻辑或
bool Complex::operator||(const Complex other)
{cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"||"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout<<boolalpha<<((this->rel||this->vir)||(other.rel||other.vir))<<endl;return (this->rel||this->vir)||(other.rel||other.vir);
}
//逻辑非
bool Complex::operator!()
{cout <<"!"<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<":";cout<<noboolalpha<<(!(this->rel||this->vir))<<endl;return !(this->rel||this->vir);
}
算术运算符
//成员函数版本的加法运算符重载
Complex Complex::operator+(const Complex other)
{Complex temp;temp.rel = this->rel+other.rel;temp.vir = this->vir+other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"+"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的减法运算符重载
Complex Complex::operator-(const Complex other)
{Complex temp;temp.rel = this->rel-other.rel;temp.vir = this->vir-other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"-"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的除法运算符重载
Complex Complex::operator/(const Complex other)
{Complex temp;temp.rel = this->rel/other.rel;temp.vir = this->vir/other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"/"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的乘法运算符重载
Complex Complex::operator*(const Complex other)
{Complex temp;temp.rel = this->rel*other.rel;temp.vir = this->vir*other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"*"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
//成员函数版本的除余运算符重载
Complex Complex::operator%(const Complex other)
{Complex temp;temp.rel = this->rel%other.rel;temp.vir = this->vir%other.vir;cout<<"("<<this->rel<<","<<this->vir<<"i"<<")"<<"%"<<"("<<other.rel<<","<<other.vir<<"i"<<")";cout <<"="<<"("<<temp.rel<<","<<temp.vir<<"i"<<")"<<endl;return temp;
}
主函数
int main()
{Complex c1(9,25);Complex c2(3,5);c1.operator+(c2);c1.operator-(c2);c1.operator*(c2);c1.operator/(c2);c1.operator%(c2);c1.operator==(c2);c1.operator<(c2);c1.operator>(c2);c1.operator&&(c2);c1.operator||(c2);!(c1);++(c1);--(c1);(c1)++;(c1)--;c1.show();return 0;
}