1、思维导图

2、类Complex中实现 >, <, != , !, || ,前自增、自减,后自增、自减运算符的重载
cpp
#include <iostream>
using namespace std;
class Complex
{
int rel;
int vir;
public:
Complex(){}
Complex(int rel,int vir):rel(rel),vir(vir)
{
}
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);
friend Complex operator+(const Complex c1,const Complex c2);
friend bool operator>(const Complex c1,const Complex c2);
friend bool operator<(const Complex c1,const Complex c2);
friend bool operator==(const Complex c1,const Complex c2);
friend bool operator!=(const Complex c1,const Complex c2);
friend bool operator&&(const Complex c1,const Complex c2);
friend bool operator||(const Complex c1,const Complex c2);
friend bool operator!(const Complex c2);
Complex operator++();//前自增++
friend Complex operator++(Complex &c1,int);//后自增++重载全局函数
Complex operator--();//前自--
friend Complex operator--(Complex &c1,int);//后自--重载全局函数
void show();
};
//**************************算数运算符************************************
//成员函数版本的加法运算符重载
Complex Complex::operator+(const Complex other)
{
Complex temp;
temp.rel=this->rel+other.rel;
temp.vir=this->vir+other.vir;
return temp;
}
//全局函数的加法运算符重载
Complex operator+(const Complex c1,const Complex c2)
{
Complex temp;
temp.rel=c1.rel*c2.rel;
temp.vir=c1.vir*c2.vir;
return temp;
}
//减法
Complex Complex::operator-(const Complex other)
{
Complex temp;
temp.rel=this->rel-other.rel;
temp.vir=this->vir-other.vir;
return temp;
}
//除法
Complex Complex::operator/(const Complex other)
{
Complex temp;
temp.rel=this->rel/other.rel;
temp.vir=this->vir/other.vir;
return temp;
}
//%
Complex Complex::operator%(const Complex other)
{
Complex temp;
temp.rel=this->rel%other.rel;
temp.vir=this->vir%other.vir;
return temp;
}//*
Complex Complex::operator*(const Complex other)
{
Complex temp;
temp.rel=this->rel*other.rel;
temp.vir=this->vir*other.vir;
return temp;
}
//***********************关系运算符*******************************
//全局函数>运算符重载
bool operator>(const Complex c1,const Complex c2)
{
if(c1.rel>c2.rel)
{
return 1;
}else if(c1.rel==c2.rel)
{
if(c1.vir>c2.vir)
{
return 1;
}
}else
{
return 0;
}
}
//全局函数<运算符重载
bool operator<(const Complex c1,const Complex c2)
{
if(c1.rel<c2.rel)
{
return 1;
}else if(c1.rel==c2.rel)
{
if(c1.vir<c2.vir)
{
return 1;
}
}else
{
return 0;
}
}
//全局函数!=运算符重载
bool operator!=(const Complex c1,const Complex c2)
{
if(c1.rel!=c1.rel||c1.vir!=c2.vir)
{
return 1;
}else
{
return 0;
}
}
//***********************逻辑运算符*******************************
//全局函数的&&运算符
bool operator&&(const Complex c1,const Complex c2)
{
return (c1.rel||c1.vir)&&(c2.rel||c2.vir);
}
//全局函数的||运算符
bool operator||(const Complex c1,const Complex c2)
{
return (c1.rel||c1.vir)||(c2.rel||c2.vir);
}
//全局函数的!运算符
bool operator!(const Complex c2)
{
return !(c2.rel||c2.vir);
}
//***********************自增自减运算符*******************************
//前自++重载函数
Complex Complex::operator++()
{
Complex temp;
temp.rel=++this->rel;
temp.vir=++this->vir;
return temp;
}
//后自增++重载全局函数
Complex temp1;
Complex operator++(Complex &c1,int)
{
temp1=c1;
c1.rel++;
c1.vir++;
return temp1;
}
//前自--重载函数
Complex Complex::operator--()
{
Complex temp;
--this->rel;
--this->vir;
return *this;
}
//后自--重载全局函数
Complex temp2;
Complex operator--(Complex &c1,int)
{
temp2=c1;
c1.rel--;
c1.vir--;
return temp2;
}
void Complex::show()
{
cout<<rel<<"+"<<vir<<"i"<<endl;
}
int main()
{
// Complex s1(1,3);
// Complex s2(9,7);
// Complex s3=s1.operator+(s2);//Complex s3=s1.operator+(s2)
// operator+(s2,s3);
// Complex s3,s4,s5,s6,s7;
// s3=s1.operator+(s2);
// s3.show();
// s4=s1.operator*(s2);
// s4.show();
Complex s1(11,30);
Complex s2(9,7);
Complex s3,s4;
++s1;
s1.show();
--s1;
s1.show();
cout<<endl;
s3=s2++;
s3.show();
s4=s2--;
s4.show();
cout<<endl;
cout<<boolalpha<<"s2>s1 -->"<<(s2>s1)<<endl;
cout<<boolalpha<<"s2!=s1 -->"<<(s2!=s1)<<endl;
cout<<boolalpha<<"s2||s1 -->"<<(s2||s1)<<endl;
cout<<boolalpha<<"!s1 -->"<<!s1<<endl;
return 0;
}
3、My_string类中实现+ ,== ,>运算符重载
cpp
#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class My_string
{
char *str; //记录C风格的字符串
int size; //记录字符串长度
public:
//无参构造
My_string():str(new char[20]{0}),size(20)
{
cout<<"无参构造"<<endl;
}
//有参构造
My_string(const char *str)
{
size=strlen(str);
this->str=new char[size+1];
strcpy(this->str,str);
// cout<<"有参构造"<<endl;
}
//拷贝构造
My_string(const My_string &other)
{
this->size=other.size;
delete []this->str;
this->str=new char [other.size+1];
strcpy(this->str,other.str);
//cout<<"拷贝构造"<<endl;
}
//拷贝赋值
My_string &operator=(const My_string &other)
{
this->size=other.size;
delete []this->str;
this->str=new char [other.size+1];
strcpy(this->str,other.str);
cout<<"拷贝赋值"<<endl;
return *this;
}
//析构函数
~My_string()
{
delete []str;
}
//at函数
char &my_at(int num);
//重构运算符+
My_string operator+(const My_string other);
//重构运算符>
bool operator>(const My_string other);
//重构运算符==
bool operator==(const My_string other);
void show();
};
char &My_string::my_at(int num)
{
return *(str+num-1);
}
//重构运算符+
My_string My_string::operator+(const My_string other)
{
My_string s2;
s2.size=this->size+other.size;
strcpy(s2.str,this->str);
strcat(s2.str,other.str);
return s2;
}
//重构运算符>
bool My_string::operator>(const My_string other)
{
int i=0;
while(this->str[i]!='\0'&&other.str[i]!='\0')
{
if(this->str[i]>other.str[i])
{
return 1;
}
if(this->str[i]<other.str[i])
{
return 0;
}
i++;
}
if(this->size>other.size)
{
return 1;
}else
{
return 0;
}
}
//重构运算符==
bool My_string::operator==(const My_string other)
{
int i=0;
while(this->str[i]!='\0'&&other.str[i]!='\0')
{
if(this->str[i]>other.str[i])
{
return 0;
}
if(this->str[i]<other.str[i])
{
return 0;
}
i++;
}
if(this->size==other.size)
{
return 1;
}else
{
return 0;
}
}
void My_string::show()
{
cout<<"str="<<str<<endl;
}
int main()
{
My_string s1("helloworld");
s1.show();
cout<<endl;
cout<<s1.my_at(6)<<endl;
My_string s2("ertyui");
My_string s3;
s3=s1+s2;
s3.show();
My_string s4="helloworld";
cout<<"s1>s2 -->"<<boolalpha<<(s1>s2)<<endl;
cout<<"s1==s4 -->"<<boolalpha<<(s1==s4)<<endl;
}
