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;
}
实现效果
My_string 添加逻辑判断运算符重载
代码
#include <iostream>
#include <cstring>using namespace std;
char c = '\0';
class My_string
{char *str; //记录C风格的字符串int size; //记录字符串长度
public://无参构造My_string():str(new char('\0')),size(0){}//有参构造My_string(const char *str):size(strlen(str)+1),str(new char [strlen(str)+1]){strcpy(this->str,str);}//拷贝构造My_string(const My_string &other):size(other.size),str(new char [other.size+1]){strcpy(str,other.str);}//拷贝赋值My_string &operator=(const My_string &other){delete this->str;this->size=other.size;str=new char[other.size+1];strcpy(str,other.str);return *this;}//析构函数~My_string (){delete []str;}//at函数char &my_at(int num);bool operator==(const My_string other);bool operator>(const My_string other);bool operator<(const My_string other);bool operator!();My_string operator+(const My_string other);void show();
};
void My_string::show()
{cout <<this->str<<endl;
}
My_string My_string::operator+(const My_string other)
{My_string temp;delete []temp.str;temp.str=new char[this->size+other.size+1];temp.str=strcat(this->str,other.str);return temp;
}
bool My_string::operator==(const My_string other)
{return (strcmp(this->str,other.str)==0);
}bool My_string::operator>(const My_string other)
{return (strcmp(this->str,other.str)>0);
}bool My_string::operator<(const My_string other)
{return (strcmp(this->str,other.str)<0);
}bool My_string::operator!()
{return !(str);
}
char &My_string::my_at(int num)
{if(num<0||num>=size){cout<<"访问越界"<<endl;return c;}return str[num];
}
int main()
{My_string s1("zzz");My_string s2("www");My_string s3=s1;cout <<(s1==s2)<<endl;cout <<(s1==s3)<<endl;cout <<(s1<s2)<<endl;cout <<(s1>s2)<<endl;cout <<(!s1)<<endl;My_string s4=s1+s2;s4.show();return 0;
}