#include <iostream>
#include <cstring>
using namespace std;
class MyString{
// 重载运算符==
friend bool operator==(const MyString& str1, const MyString& str2);
// 重载运算符!=
friend bool operator!=(const MyString& str1, const MyString& str2);
// 重载运算符<<
friend ostream& operator<<(ostream& os,const MyString& str);
// 重载运算符>>
friend istream& operator>>(istream& is, MyString& str);
public:
// 构造函数
MyString(const char* m_data = 0);
// 拷贝构造函数
MyString(const MyString& str);
// 拷贝赋值运算符
MyString& operator=(const MyString& str);
// 移动构造函数
MyString(MyString&& str) noexcept;
// 移动赋值运算符
MyString& operator=(MyString&& str) noexcept;
// 重载运算符+
MyString operator+(const MyString& str);
// 重载运算符+=
MyString& operator+=(const MyString& str);
// 重载运算符[]
char& operator[](int index);
// 获得长度
int getSize();
// 获得原始指针
char* get();
// 析构函数
~MyString();
private:
char *data;
};
// 构造函数
MyString::MyString(const char* m_data)
{
if(m_data){
data = new char[strlen(m_data)+1];
strcpy(data,m_data);
}else{
data = new char[1];
*data = '\0';
}
}
// 拷贝构造函数
MyString::MyString(const MyString& str)
{
data = new char[strlen(str.data)+1];
strcpy(data, str.data);
}
// 拷贝赋值运算符
MyString& MyString::operator=(const MyString& str)
{
if(this != &str){
delete []data;
data = new char[strlen(str.data)+1];
strcpy(data, str.data);
}
return *this;
}
// 移动构造
MyString::MyString(MyString&& str) noexcept:data(str.data)
{
str.data = nullptr;
}
// 移动赋值运算符
MyString& MyString::operator=(MyString&& str) noexcept
{
if(this != &str){
delete []data;
data = str.data;
str.data = nullptr;
}
return *this;
}
// 重载运算符+
MyString MyString::operator+(const MyString& str){
auto len = strlen(data) + strlen(str.data) + 1;
MyString temp;
temp.data = new char[len];
strcpy(temp.data,this->data);
strcat(temp.data,str.data);
return temp;
}
// 重载运算符+=
MyString& MyString::operator+=(const MyString& str)
{
*this = *this + str;
return *this;
}
// 重载运算符==
bool operator==(const MyString& str1, const MyString& str2)
{
int ret = strcmp(str1.data, str2.data);
if(ret == 0)
return true;
return false;
}
// 重载运算符!=
bool operator!=(const MyString& str1, const MyString& str2)
{
return !(str1 == str2);
}
// 重载运算符<<
ostream& operator<<(ostream& os,const MyString& str)
{
os << str.data;
return os;
}
// 重载运算符>>
istream& operator>>(istream& is, MyString& str)
{
if(str.data){
delete str.data;
str.data = nullptr;
}
char temp[2048];
is >> temp;
str.data = new char[strlen(temp)+1];
strcpy(str.data, temp);
return is;
}
// 重载运算符[]
char& MyString::operator[](int index)
{
return data[index];
}
// 获得长度
int MyString::getSize()
{
return strlen(data);
}
// 获得原始指针
char* MyString::get()
{
return data;
}
// 析构函数
MyString::~MyString()
{
if(data)
delete []data;
}
int main(void)
{
// 测试<<和>>
cout << "测试<<和>>,请输入字符串:" << endl;
MyString s0;
cin >> s0;
cout << s0 << endl ;
cout << endl << "测试其他功能:" << endl;
// 测试构造函数
MyString s1("hello");
cout << s1 << endl;
char s[] = {"hi"};
MyString s2(s);
cout << s2 << endl;
// 测试拷贝构造函数
MyString s3 = s2;
cout << s3 << endl;
// 测试拷贝赋值运算符
s3 = s1;
cout << s3 << endl;
// 测试== 和 !=
if(s1 == s2)
cout << "s1与s2相等" << endl;
else
cout << "s1与s2不相等" << endl;
if(s1 != s2)
cout << "s1与s2不相等" << endl;
else
cout << "s1与s2相等" << endl;
// 测试+和+=
s1 = s1 + s2;
cout << s1 << endl;
s1 += s2;
cout << s1 << endl;
// 测试[]运算符
cout << s1[4] << endl;
return 0;
}
结果
注:还在完善中,如有错误,还请指正。