#include <iostream>
using namespace std;
class String
{
public:
String();
~String();
String(const String&rs);
String(const char*const ch);
char&operator[](unsigned short int length);
char operator[](unsigned short int length)const;
String&operator=(const String&s);
String operator+(const String &);
void operator+=(const String&rs);
//↓重载左移运算符实现字符串输出
friend ostream &operator<<(ostream & o,const String &str)
{
o<<str.str;
return o;
}
//↓重载右移运算符实现字符串输入
friend istream &operator>>(istream & i,const String &str)
{
i>>str.str;
return i;
}
friend bool operator<(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)<0)
return 1;
else
return 0;
}
friend bool operator>(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)>0)
return 1;
else
return 0;
}
friend bool operator==(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)==0)
return 1;
else
return 0;
}
unsigned short int getlen()const{return len;}//获取字符串长度
const char*getstr()const{return str;} //获取字符串内容
private:
String(unsigned short int length);
unsigned short int len; //用作计算字符长度
char*str; // 用于保存字符串
};
String::String(unsigned short int length) //设置字符串长度
{
str=new char[length+1]; //给字符串开辟相应的空间+1是用来保存结束符
int i;
for(i=0;i<=length;i++)
str[i]='\0'; //最好一个元素保存结束符
len=length; //返回字符串长度
}
String::String() //空的字符串
{
len=0; //空的字符串长度应为0
str=new char[1];
str[0]='0';
}
String::~String() //删除字符串
{
len=0;
str=new char[1];
str[0]='0';
}
String::String(const String&rs) //复制字符串对象
{
len=rs.getlen();
str=new char[len+1];
for(int i=0;i<len;i++)
str[i]=rs[i];
str[len]='\0';
}
String::String(const char*const ch) //复制字符串
{
len=strlen(ch);
str=new char[len+1];
for(int i=0;i<len;i++)
str[i]=ch[i];
str[len]='\0';
}
char&String::operator [](unsigned short int length) //限制数组
{
if(length>len)
return str[len-1];
else
return str[length];
}
char String::operator [](unsigned short int length)const //假如参数是一个常量
{
if(length>len)
return str[len-1];
else
return str[length];
}
String&String::operator =(const String&s)
{
if (this==&s)
return *this;
delete[]str;
len=s.getlen();
str=new char[len+1];
for(int i=0;i<len;i++)
{
str[i]=s[i];
}
str[len]='\0';
return *this;
}
String String::operator +(const String&rs)
{
int total=len+rs.getlen();
String temp(total);
int i,j;
for (i=0;i<len;i++)
temp[i]=str[i];
for (j=0;j<rs.getlen();j++,i++)
temp[i]=rs[j];
temp[total]='\0';
return temp;
}
void String::operator +=(const String&rs)
{
int total=len+rs.getlen();
String temp(total);
int i,j;
for (i=0;i<len;i++)
temp[i]=str[i];
for (j=0;j<rs.getlen();j++,i++)
{
temp[i]=rs[j];
temp[total]='\0';
*this=temp;
}
}
int main()
{
return 0;
}
using namespace std;
class String
{
public:
String();
~String();
String(const String&rs);
String(const char*const ch);
char&operator[](unsigned short int length);
char operator[](unsigned short int length)const;
String&operator=(const String&s);
String operator+(const String &);
void operator+=(const String&rs);
//↓重载左移运算符实现字符串输出
friend ostream &operator<<(ostream & o,const String &str)
{
o<<str.str;
return o;
}
//↓重载右移运算符实现字符串输入
friend istream &operator>>(istream & i,const String &str)
{
i>>str.str;
return i;
}
friend bool operator<(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)<0)
return 1;
else
return 0;
}
friend bool operator>(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)>0)
return 1;
else
return 0;
}
friend bool operator==(const String&str1,const String &str2)
{
if(strcmp(str1.str,str2.str)==0)
return 1;
else
return 0;
}
unsigned short int getlen()const{return len;}//获取字符串长度
const char*getstr()const{return str;} //获取字符串内容
private:
String(unsigned short int length);
unsigned short int len; //用作计算字符长度
char*str; // 用于保存字符串
};
String::String(unsigned short int length) //设置字符串长度
{
str=new char[length+1]; //给字符串开辟相应的空间+1是用来保存结束符
int i;
for(i=0;i<=length;i++)
str[i]='\0'; //最好一个元素保存结束符
len=length; //返回字符串长度
}
String::String() //空的字符串
{
len=0; //空的字符串长度应为0
str=new char[1];
str[0]='0';
}
String::~String() //删除字符串
{
len=0;
str=new char[1];
str[0]='0';
}
String::String(const String&rs) //复制字符串对象
{
len=rs.getlen();
str=new char[len+1];
for(int i=0;i<len;i++)
str[i]=rs[i];
str[len]='\0';
}
String::String(const char*const ch) //复制字符串
{
len=strlen(ch);
str=new char[len+1];
for(int i=0;i<len;i++)
str[i]=ch[i];
str[len]='\0';
}
char&String::operator [](unsigned short int length) //限制数组
{
if(length>len)
return str[len-1];
else
return str[length];
}
char String::operator [](unsigned short int length)const //假如参数是一个常量
{
if(length>len)
return str[len-1];
else
return str[length];
}
String&String::operator =(const String&s)
{
if (this==&s)
return *this;
delete[]str;
len=s.getlen();
str=new char[len+1];
for(int i=0;i<len;i++)
{
str[i]=s[i];
}
str[len]='\0';
return *this;
}
String String::operator +(const String&rs)
{
int total=len+rs.getlen();
String temp(total);
int i,j;
for (i=0;i<len;i++)
temp[i]=str[i];
for (j=0;j<rs.getlen();j++,i++)
temp[i]=rs[j];
temp[total]='\0';
return temp;
}
void String::operator +=(const String&rs)
{
int total=len+rs.getlen();
String temp(total);
int i,j;
for (i=0;i<len;i++)
temp[i]=str[i];
for (j=0;j<rs.getlen();j++,i++)
{
temp[i]=rs[j];
temp[total]='\0';
*this=temp;
}
}
int main()
{
return 0;
}