使用运算符重载,能够使得代码更加简洁 。
所谓运算符重载,本质上也还是函数的重载,能够实现“一符多用”,将原本只是针对于基本数据类型的一些运算符,赋予类对象之间的运算,使得类对象之间操作更加优雅。
重载的方法:函数名的统称:operator# (#表示要被重载的运算符,例如:加减乘除)
关系运算符的重载
1)>的重载
bool operator>(const my_string & R,const my_string & L)
{
int res = 0;
int rlen =(R.len>L.len)? R.len:L.len; //取出两字符串较长的长度
for(int i=0; i<rlen;i++) //依次取出两字符串的字符相比较
{
res=R.str[i]-L.str[i]; //以ascll码判断大小
if(res>0)
{
return true;
}
else if(res<0)
{
return false;
}
}
return false;
}
2)<的重载
bool operator<(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res<0)
{
return true;
}
else if(res>0)
{
return false;
}
}
return false;
}
3)==的重载
bool operator==(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res<0 || res >0)
{
return false;
}
}
return true;
}
4)>=的重载
bool operator>=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0)
{
return true;
}
else if(res<0)
{
return false;
}
}
return true;
}
5)<=的重载
bool operator<=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0)
{
return false;
}
else if(res<0)
{
return true;
}
}
return true;
}
6)!=的重载
bool operator!=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0 || res<0)
{
return true;
}
}
return false;
}
加号运算符的重载(+)
my_string operator+(my_string & R,my_string & L)
{
my_string temp;
temp.len = R.len+L.len;
temp.str = new char[temp.len+1];
for(int i=0; i<R.len; i++)
{
temp.str[i]=R.str[i];
}
for(int j=0; j<L.len; j++)
{
temp.str[R.len+j]=L.str[j];
}
return temp;
}
取成员运算符[]重载
char & operator [](int i)
{
return str[i];
}
完整代码
#include <iostream>
#include <cstring>
using namespace std;
class my_string
{
public:
//无参构造
my_string()
{
str=new char[1];
*str='\0';
len = 0;
}
//有参构造
my_string(const char *s)
{
len = strlen(s); //获取字符串长度
str = new char[len+1]; //申请足够的空间存放字符串
strcpy(str,s); //拷贝内容
}
my_string(int a, char c)
{
str = new char[a+1];
for(int i=0;i<a;i++)
{
str[i]= c;
}
len = a;
}
//拷贝构造
my_string(const my_string& S) //深拷贝
{
str = new char[len+1];
strcpy(str,S.str);
this->len = S.len;
}
//拷贝赋值
my_string& operator=(const my_string& R)
{
if(this!=&R)
{
//this->str = R.str; //浅拷贝
this->str = new char[1];
strcpy(this->str,R.str);
this->len = R.len;
}
return *this;
}
//将运算符重载
//>
friend bool operator>(const my_string&,const my_string&);
//<
friend bool operator<(const my_string&,const my_string&);
//==
friend bool operator==(const my_string&,const my_string&);
//>=
friend bool operator>=(const my_string&,const my_string&);
//<=
friend bool operator<=(const my_string&,const my_string&);
/*!=*/
friend bool operator!=(const my_string&,const my_string&);
//+
friend my_string operator+(my_string&,my_string&);
//将取成员运算符[]重载
char & operator [](int i)
{
return str[i];
}
//判空
bool my_empty()
{
if(*str == 0)
return true;
else
return false;
}
//求长度
int my_size()
{
int size = 0;
char *p = str;
while(*p++ != '\0'){ size++; }
return size;
}
//转化为c风格字符串
char *my_str()
{
return str;
}
void display()
{
cout<<"str:"<<str<<'\t'<<"len:"<<len<<endl;
}
private:
char *str;
int len;
};
//关系运算符>的重载
bool operator>(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0)
{
return true;
}
else if(res<0)
{
return false;
}
}
return false;
}
//关系运算符<的重载
bool operator<(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res<0)
{
return true;
}
else if(res>0)
{
return false;
}
}
return false;
}
//关系运算符==的重载
bool operator==(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res<0 || res >0)
{
return false;
}
}
return true;
}
//关系运算符>=的重载
bool operator>=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0)
{
return true;
}
else if(res<0)
{
return false;
}
}
return true;
}
//关系运算符<=的重载
bool operator<=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0)
{
return false;
}
else if(res<0)
{
return true;
}
}
return true;
}
//关系运算符!=的重载
bool operator!=(const my_string & R,const my_string & L)
{
int res=0;
int rlen =(R.len>L.len)? R.len:L.len;
for(int i=0; i<rlen;i++)
{
res=R.str[i]-L.str[i];
if(res>0 || res<0)
{
return true;
}
}
return false;
}
//加号运算符(实现的效果类似于c语言中的strcat函数)
my_string operator+(my_string & R,my_string & L)
{
my_string temp;
temp.len = R.len+L.len;
temp.str = new char[temp.len+1];
for(int i=0; i<R.len; i++)
{
temp.str[i]=R.str[i];
}
for(int j=0; j<L.len; j++)
{
temp.str[R.len+j]=L.str[j];
}
return temp;
}
int main()
{
my_string s1("hqyj");
my_string s2;
s2 = s1;
cout<<(s1==s2)<<endl;
my_string s4("hqyja");
cout<<(s1==s4)<<endl;
s4.display();
my_string s5=s1+s4;
s5.display();
cout<<s1[2]<<endl;
return 0;
}