1.各类函数要求
1:析构函数,释放buf指向的堆空间
2:编写 append(const mystring r) 为当前字符串尾部,拼接新的字符串r
3:编写 isEqual(const mystring r) 判断当前字符串和 字符串 r是否相等
4.为mystring类补充深拷贝功能的拷贝构造函数
5.为mystring类补充: swap(另一个对象) 的函数,实现交换2个对象的字符串 mystring str = "hello" mystring ptr = "world" str.swap(ptr) str == "world",ptr == "hello"
2.各类运算符重载要求
为之前写的mystring类,添加 + 功能
:本质就是append str1 = "hello" str2 = "world" str3 = str1 + str2 == "helloworld"
+= 功能
= 深拷贝功能
== 功能
!= 功能
++ 功能,字符串中所有字符的ASCII自增1
\] 下表运算符 operator\[\](int index) 返回该下标上的字符 str = "hello" cout \<\< str\[0\] ;终端输出 h
cout 输出mystring功能
cin 输入mystring功能
```cpp
using namespace std;
class mystring
{
char * buf; //基本数据类型
public:
mystring(const char * str); //绑定一个构造函数
mystring(); //绑定另外一个构造函数
~mystring(); //析构函数
void show(); //绑定显示方法
void setmystring(const mystring r); // 设置字符串
const char * getmystring()const; //获取当前字符串
void append(const mystring r); //为当前字符串尾部拼接字符串r
bool isEqual(const mystring r); //字符串比较否相等
void swapmystring(mystring& r); //字符串交换
mystring(const mystring& r); //深拷贝构造函数
friend mystring operator+(mystring& ,const mystring& ); //加法运算符
friend mystring operator+=(mystring& ,const mystring &); //复合运算
mystring operator=(const mystring & r) //深拷贝功能 由于等号=不能放在外面 参数不和 需要写成类函数方法
{
int len = strlen(r.buf);
this->buf = new char[len+1];
strcpy(buf,r.buf);
return *this;
}
friend bool operator==(const mystring& ,const mystring& ); //字符串相等函数
friend bool operator!=(const mystring& ,const mystring& ); //字符串不等
//++ 自增功能重载成字符串中的所有字符ASKII加1
mystring& operator++()//前++
{
for(int i =0;buf[i] != '\0';i++)
{
buf[i]++;
}
return *this; //返回本身
}
mystring operator++(int)//后++
{
mystring temp = *this;
++(*this);
return temp; //返回未加前
}
char& operator[](int index) //下标运算符[]
{
return buf[index];
}
friend ostream& operator<<(ostream& ,const mystring&); //cout输出mystring
friend istream& operator>>(istream& ,mystring&); //cin输入mtstring
};
//+运算符重载成字符串拼接函数
mystring operator+(mystring& l,const mystring& r)
{
mystring temp;
temp.buf = new char[strlen(l.buf)+strlen(r.buf)];
strcpy(temp.buf,l.buf);
temp.append(r);
return temp;
}
//+=运算符重载成字符串追加函数
mystring operator+=(mystring& l,const mystring &r)
{
l.append(r);
return l;
}
// == 运算符重载成字符串相等函数
bool operator==(const mystring& l,const mystring& r)
{
if(strcmp(l.buf,r.buf) == 0)
{
return 1;
}
return 0;
}
//!= 运算符重载成字符串不等函数
bool operator!=(const mystring&l,const mystring& r)
{
if(strcmp(l.buf,r.buf) != 0)
{
return 1;
}
return 0;
}
//<<因为<