#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
char *str;
int len;
public:
My_string(){this->str=NULL;this->len=0;cout<<"无参"<<endl;} //无参构造
My_string(const char *str)
{
this->len=strlen(str);
this->str=new char[this->len+1];
strcpy(this->str,str);
cout<<"拷贝构造"<<endl;
}
My_string(const My_string &s)
{
this->len=s.len;
this->str=new char[this->len+1];
strcpy(this->str,s.str);
cout<<"拷贝构造"<<endl;
}
My_string &operator=(My_string &other) //拷贝赋值
{
this->len=other.len;
if(this->str==NULL){
this->str=new char[this->len+1];
}
else if(this->len<other.len){ //判断之前字符串申请的空间是否小于要赋值字符串的大小
delete []this->str;
this->str=NULL;
this->str=new char[this->len+1]; //小于赋值字符串空间重新申请
}
strcpy(this->str,other.str);
return *this;
}
My_string &operator+(My_string &other)const
{
static My_string temp; //申请并延长临时变量生命周期存储拼接后的字符串
temp.len=this->len+other.len;
temp.str=new char[temp.len+1]; //申请新的存储空间
strcpy(temp.str,this->str); //将左操作数复制给新空间
strcat(temp.str,other.str); //将右操作数拼接在左操作数后面
return temp;
}
~My_string() //析构函数
{
if(this->str!=NULL){
delete this->str;
this->str=NULL;
}
cout<<"析构"<<endl;
}
void show()
{
cout<<"字符串:"<<this->str<<endl;
cout<<"size:"<<this->len<<endl;
}
//operator[]函数返回字符
char operator[](int index)
{
if(index<0||index>this->len)
cout<<"下标输入有误\n"<<endl;
else
return this->str[index];
return 0;
}
//定义全局友好函数
friend bool operator==(const My_string &L,const My_string &R);
friend bool operator!=(const My_string &L,const My_string &R);
friend bool operator<(const My_string &L,const My_string &R);
friend bool operator>(const My_string &L,const My_string &R);
friend bool operator<=(const My_string &L,const My_string &R);
friend bool operator>=(const My_string &L,const My_string &R);
friend ostream &operator<<(ostream &L,const My_string &R);
friend istream &operator>>(istream &L,My_string &R);
};
bool operator==(const My_string &L,const My_string &R)
{
return ~strcmp(L.str,R.str); //strcmp比较一样返回真
}
bool operator!=(const My_string &L,const My_string &R)
{
return strcmp(L.str,R.str); //strcmp比较不一样返回真
}
bool operator<(const My_string &L,const My_string &R)
{
return strcmp(L.str,R.str)<0?1:0; //strcmp比较,左操作数小于右操作数返回真
}
bool operator>(const My_string &L,const My_string &R)
{
return strcmp(L.str,R.str)>0?1:0; //strcmp比较,左操作数大于右操作数返回真
}
bool operator<=(const My_string &L,const My_string &R)
{
return strcmp(L.str,R.str)>0?0:1; //strcmp比较,左操作数大于右操作数返回假,小于等于则返回真
}
bool operator>=(const My_string &L,const My_string &R)
{
return strcmp(L.str,R.str)<0?0:1; //strcmp比较,左操作数小于右操作数返回假,大于等于则返回真
}
ostream &operator<<(ostream &L,const My_string &R)
{
L<<"字符串:"<<R.str<<" size:"<<R.len<<endl;
return L;
}
istream &operator>>(istream &L,My_string &R)
{
L>>R.str>>R.len;
return L;
}
int main()
{
My_string s1("hello");
s1.show();
My_string s2("world");
s2.show();
My_string s3=s1+s2;
s3.show();
//关系运算
if(s1>s2)
cout<<"s1大"<<endl;
else
cout<<"s2大"<<endl;
if(s3<s2)
cout<<"s2大"<<endl;
else
cout<<"s3大"<<endl;
if(s1==s1)
cout<<"相等"<<endl;
if(s2!=s3)
cout<<"不相等"<<endl;
cout<<s3[5]<<endl; //打印[]操作符返回的字符
cout<<s3<<endl; //<<插入重载运算
return 0;
}
仿照c++string库编写类
最新推荐文章于 2024-10-05 12:07:50 发布