my_string的代码
#include <iostream>
#include<cstring>
using namespace std;
class my_string
{
private:
char *cstr;
int len;
public:
my_string():cstr(NULL),len(0)
{
// cout << "是无参构造函数" <<endl;
} //无参构造函数
my_string(const char *cstr)
{
this->len = strlen(cstr);
this->cstr= new char [this->len+1];
strcpy(this->cstr,cstr);
// cout << "执行有参构造函数"<< endl;
} //有参构造函数
my_string(const my_string &other)
{
this->len= other.len;
this->cstr=new char [this->len+1];
strcpy(this->cstr ,other.cstr);
// cout <<"执行拷贝构造函数" <<endl;
} //拷贝构造函数
~my_string()
{
delete []this->cstr;
// cout << "解析的地址" <<this <<endl;
// cout <<"执行解析构造函数"<< endl;
}
char* show() //返回cstr
{
return this->cstr;
}
//判空
bool enpty()
{
return this->cstr==NULL?0:1;
}
//返回字符串长度
int size()
{
return this->len;
}
char &at(int index)
{
if(index<0||index>this->len)
{
cout << "定位错误" <<endl;
}
return *(this->cstr+index);
}
//赋值运算符'='函数重载
my_string &operator=(const my_string &R) //给this对象= R
{
if(this->cstr==NULL)
{
//先申请空间
this->cstr=new char[R.len+1];
strcpy(this->cstr,R.cstr);
this->len=R.len;
}
else
{
//不管两个对象的cstr哪个空间大,统一先释放空间在申请
delete this->cstr;
this->len =R.len;
this->cstr=new char[this->len+1];
strcpy(this->cstr,R.cstr);
}
//cout <<"执行赋值=函数重载" <<endl;
return *this;
}
//赋值运算符‘+’函数重载
my_string operator+(const my_string &R)const
{
//不管两个对象的cstr哪个空间大,先申请一片空间用来存两个字符串的拼接
my_string temp;
temp.len=this->len+R.len;
temp.cstr =new char[temp.len];
strcpy(temp.cstr,this->cstr);
strcat(temp.cstr,R.cstr);
return temp;
}
//赋值运算符 ‘+=’函数重载
const my_string &operator+=(const my_string &R)
{
my_string temp;
temp.len= this->len+R.len+1;
temp.cstr =new char [temp.len];
strcpy(temp.cstr,this->cstr);
strcat(temp.cstr,R.cstr);
delete this->cstr;
this->cstr=new char[temp.len];
strcpy(this->cstr,temp.cstr);
delete temp.cstr;
return *this;
}
//字符串相等,关系运算符重载
bool operator==(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)==0)
{
return 1;
}
else
{
return 0;
}
}
//,关系运算符'!='重载
bool operator!=(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)!=0)
{
return 1;
}
else
{
return 0;
}
}
//关系运算符'<'重载
bool operator<(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)<0)
{
return 1;
}
else
{
return 0;
}
}
//关系运算符‘>’重载
bool operator>(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)>0)
{
return 1;
}
else
{
return 0;
}
}
//关系运算符‘<=’函数重载
bool operator<=(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)<=0)
{
return 1;
}
else
{
return 0;
}
}
//关系运算符‘>=’函数重载
bool operator>=(const my_string &R)const
{
if(strcmp(this->cstr,R.cstr)>=0)
{
return 1;
}
else
{
return 0;
}
}
friend const ostream &operator<<(ostream &O,const my_string &R);
friend const istream &operator>>(istream &I, my_string &R);
friend const istream &getline(istream &i, my_string &R);
};
//输出
const ostream &operator<<( ostream &O,const my_string &R)
{
O <<strlen(R.cstr) << R.cstr<<endl;
return O;
}
//输入
const istream &operator>>(istream &i, my_string &R)
{
i>> R.cstr;
R.len=strlen(R.cstr);
return i;
}
int main()
{
my_string a="zhangsan";
my_string b("lisi");
my_string c;
my_string d=a;
my_string r;
r="liside";
cout <<"r=" <<r;
c=a;
cout <<c.show() <<endl;
cout <<"****************" <<endl;
my_string e;
//算术运算符重载
e=a+b;
cout << e.show() <<endl;
//关系运算符'=='重载
if(a==a)
{
cout <<"相同" <<endl;
}
else
{
cout <<"不相同"<< endl;
}
cout<<a;
cin >>a;
cout <<a;
b= a+b;
cout <<b;
string s;
s="shang";
cout <<s<<s.size() <<endl;
return 0;
}
现象