#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class My_string
{
private:
char *cstr;
int len;
public:
My_string(); //无参构造
My_string(const char *str); //有参构造
My_string(const My_string &other); //拷贝构造
My_string &operator=(const My_string &other);//拷贝赋值
My_string &operator=(const char* other);
char &operator[](int); //[]
const My_string operator+(const My_string &other)const; //+
const My_string operator+(const char* other)const;
bool operator==(const My_string &other)const; //==
bool operator!=(const My_string &other)const; //!=
bool operator<(const My_string &other)const; //<
bool operator>(const My_string &other)const; //>
bool operator<=(const My_string &other)const; //<=
bool operator>=(const My_string &other)const; //>=
friend ostream &operator<<(ostream &L,My_string &R); //将插入运算符重载函数设为友元
friend istream &operator>>(istream &L,My_string &R);
~My_string(); //析构函数
};
My_string::My_string():cstr(NULL),len(0){} //无参构造
My_string::My_string(const char *str) //有参构造
{
len=strlen(str);
cstr=new char[len+1];
strcpy(cstr,str);
}
My_string::My_string(const My_string &other) //拷贝构造
{
len=other.len;
cstr=new char[len+1];
strcpy(cstr,other.cstr);
}
My_string& My_string::operator=(const My_string &other)//拷贝赋值
{
if(this->cstr!=NULL)
{
delete this->cstr;
}
len=other.len;
this->cstr=new char[len+1];
strcpy(this->cstr,other.cstr);
return *this;
}
My_string &My_string::operator=(const char *other)
{
if(this->cstr!=NULL)
{
delete []this->cstr;
}
len=strlen(other);
this->cstr=new char[len+1];
strcpy(this->cstr,other);
return *this;
}
char &My_string::operator[](int index) //[]
{
static char c=-1;
if(index<0||index>this->len)
{
cout<<"操作越界"<<endl;
return c;
}
return cstr[index];
}
const My_string My_string::operator+(const My_string &other) const //+
{
My_string temp;
temp.len=this->len+other.len;
temp.cstr=new char(len+1);
strcpy(temp.cstr,this->cstr);
strcat(temp.cstr,other.cstr);
return temp;
}
const My_string My_string::operator+(const char *other) const
{
My_string temp;
temp.len=this->len+strlen(other);
temp.cstr=new char(len+1);
strcpy(temp.cstr,this->cstr);
strcat(temp.cstr,other);
return temp;
}
bool My_string::operator==(const My_string &other) const //==
{
return !strcmp(this->cstr,other.cstr);
}
bool My_string::operator!=(const My_string &other) const //!=
{
return strcmp(this->cstr,other.cstr);
}
bool My_string::operator<(const My_string &other) const //<
{
if(strcmp(this->cstr,other.cstr)<0)
return 1;
return 0;
}
bool My_string::operator>(const My_string &other) const //>
{
if(strcmp(this->cstr,other.cstr)>0)
return 1;
return 0;
}
bool My_string::operator<=(const My_string &other) const //<=
{
if(strcmp(this->cstr,other.cstr)<=0)
return 1;
return 0;
}
bool My_string::operator>=(const My_string &other) const //>=
{
if(strcmp(this->cstr,other.cstr)>=0)
return 1;
return 0;
}
ostream &operator<<(ostream &L,My_string &R)//插入运算符重载
{
L<<R.cstr;
return L;
}
istream &operator>>(istream &L,My_string &R)
{
if(R.cstr!=NULL)
{
delete []R.cstr;
}
R.cstr=new char[128];
L>>R.cstr;
R.len=strlen(R.cstr);
return L;
}
My_string::~My_string() //析构函数
{
delete []cstr;
}
int main()
{
My_string s1="jing";
My_string s2="hao";
My_string s4;
cin>>s4;
if(s1<s2)
{
cout<<"s1<s2"<<endl;
}
else
{
cout<<"s1>s2"<<endl;
}
cout<<s4;
return 0;
}