代码
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
public:
MyString();
MyString(const char *str);
MyString(const MyString &obj);
//[]重载
char & operator[](int Index);
//=重载
MyString & operator=(const char *str);
MyString & operator=(const MyString &obj);
//字符串拼接 重载+号
MyString operator+(const char *str);
MyString operator+(const MyString &obj);
//字符串比较
bool operator==(const char *str);
bool operator==(const MyString &obj);
void StringShow();
private:
char *pString;
int m_size;
};
MyString::MyString()
{
this->pString = NULL;
this->m_size = 0;
}
MyString::MyString(const char *str)
{
this->m_size = strlen(str) + 1;
this->pString = new char[m_size];
strcpy(this->pString,str);
}
MyString::MyString(const MyString &obj)
{
this->m_size = obj.m_size;
this->pString = new char[m_size];
strcpy(this->pString,obj.pString);
}
//[]重载
char& MyString::operator[](int Index)
{
static char ch = -1;
if(Index >= 0 && Index < this->m_size)
{
return this->pString[Index];
}
else
{
cout<<"越界访问!"<<endl;
return ch;
}
}
//=重
MyString & MyString::operator=(const char *str)
{
this->m_size = strlen(str) + 1;
if(this->pString != NULL)
{
delete []this->pString;
this->pString = new char[m_size];
}
else
{
this->pString = new char[m_size];
}
strcpy(this->pString,str);
}
MyString & MyString::operator=(const MyString &obj)
{
this->m_size = obj.m_size;
if(this->pString != NULL)
{
delete []this->pString;
this->pString = new char[m_size];
}
else
{
this->pString = new char[m_size];
}
strcpy(this->pString,obj.pString);
}
//字符串拼接 重载+号
MyString MyString::operator+(const char *str)
{
if(this->pString != NULL)
{
char *s = new char[this->m_size + 1];
strcpy(s,this->pString);
delete []this->pString;
this->pString = new char[this->m_size + strlen(str) + 1];
strcpy(this->pString,s);
strcat(this->pString,str);
this->m_size += strlen(str);
delete []s;
}
else
{
this->m_size = strlen(str) + 1;
this->pString = new char[m_size];
strcpy(this->pString,str);
}
return *this;
}
MyString MyString::operator+(const MyString &obj)
{
if(this->pString != NULL)
{
char *s = new char[this->m_size + 1];
strcpy(s,this->pString);
delete []this->pString;
this->pString = new char[this->m_size + strlen(obj.pString) + 1];
strcpy(this->pString,s);
strcat(this->pString,obj.pString);
this->m_size += strlen(obj.pString);
delete []s;
}
else
{
this->m_size = strlen(obj.pString) + 1;
this->pString = new char[m_size];
strcpy(this->pString,obj.pString);
}
return *this;
}
//字符串比较
bool MyString::operator==(const char *str)
{
if(strcmp(this->pString,str) == 0)
{
return true;
}
else
{
return false;
}
}
bool MyString::operator==(const MyString &obj)
{
if(strcmp(this->pString,obj.pString) == 0)
{
return true;
}
else
{
return false;
}
}
void MyString::StringShow()
{
cout<<"字符串的长度为:"<<this->m_size - 1<<endl;
cout<<"字符串的内容为:"<<this->pString<<endl;
}
int main(int argc, char const *argv[])
{
MyString s1;
MyString m1("helloworld");
m1.StringShow();
MyString m2(m1);
m2.StringShow();
cout<<m2[0]<<endl;
cout<<m2[3]<<endl;
cout<<m2[30]<<endl;
m2 = "nihao najing";
m2.StringShow();
MyString m3;
//m3 = m2 = m1;
//m1.StringShow();
//m2.StringShow();
//m3.StringShow();
MyString m4;
m4 = m4 + "helloworld";
m4.StringShow();
MyString m5;
m5 = m4 + "hahahhaha";
m4.StringShow();
MyString m6("88888888");
m6 = m6 + m5;
m6.StringShow();
if(m6 == m5)
{
cout<<"m6 = m5"<<endl;
}
else
{
cout<<"m6 != m5 "<<endl;
}
return 0;
}
终端
judy@judy-virtual-machine:~/桌面/2022/0303$ g++ mystring.cpp -o mystring
^[[Ajudy@judy-virtual-machine:~/桌面/2022/0303$ ./mystring
字符串的长度为:10
字符串的内容为:helloworld
字符串的长度为:10
字符串的内容为:helloworld
h
l
越界访问!
�
字符串的长度为:12
字符串的内容为:nihao najing
字符串的长度为:10
字符串的内容为:helloworld
字符串的长度为:19
字符串的内容为:helloworldhahahhaha
字符串的长度为:27
字符串的内容为:88888888helloworldhahahhaha
m6 != m5