字符串类封装
MyString.h
#define _CRT_SECURE_NO_WARNINGS #pragma once #include <iostream> using namespace std; class MyString { friend ostream& operator<< (ostream & out, MyString& str); friend istream& operator>>(istream& in, MyString& str); public: MyString(const char *); MyString(const MyString&); ~MyString(); char& operator[](int index); //[]重载 //=号重载 MyString& operator=(const char * str); MyString& operator=(const MyString& str); //字符串拼接 重载+号 MyString operator+(const char * str ); MyString operator+(const MyString& str); //字符串比较 bool operator== (const char * str); bool operator== (const MyString& str); private: char * pString; //指向堆区空间 int m_Size; //字符串长度 不算'\0' };
MyString.cpp
#include "MyString.h" //左移运算符 ostream& operator<< (ostream & out, MyString& str) { out << str.pString; return out; } //右移运算符 istream& operator>>(istream& in, MyString& str) { //先将原有的数据释放 if (str.pString != NULL) { delete[] str.pString; str.pString = NULL; } char buf[1024]; //开辟临时的字符数组,保存用户输入内容 in >> buf; str.pString = new char[strlen(buf) + 1]; strcpy(str.pString, buf); str.m_Size = strlen(buf); return in; } //构造函数 MyString::MyString(const char * str) {
if(str == nullptr)
{
this->pString = new char[1];
*(this->pString)='\0';
}
else
{ this->pString = new char[strlen(str) + 1]; strcpy(this->pString, str); this->m_Size = strlen(str);
} } //拷贝构造 MyString::MyString(const MyString& str) { this->pString = new char[strlen(str.pString) + 1]; strcpy(this->pString, str.pString); this->m_Size = str.m_Size; } //析构函数 MyString::~MyString() { if (this->pString!=NULL) { delete[]this->pString; this->pString = NULL; } } char& MyString::operator[](int index) { return this->pString[index]; } MyString& MyString::operator=(const char * str) { if (this->pString != NULL){ delete[] this->pString; this->pString = NULL; } this->pString = new char[strlen(str) + 1]; strcpy(this->pString, str); this->m_Size = strlen(str); return *this; } MyString& MyString::operator=(const MyString& str) {
if(this ==&str)
{
return *this;
}
if (this->pString != NULL){ delete[] this->pString; this->pString = NULL; } this->pString = new char[strlen(str.pString) + 1]; strcpy(this->pString, str.pString); this->m_Size = str.m_Size; return *this; } MyString MyString::operator+(const char * str) { int newsize = this->m_Size + strlen(str) + 1; char *temp = new char[newsize]; memset(temp, 0, newsize); strcat(temp, this->pString); strcat(temp, str); MyString newstring(temp); delete[] temp; return newstring; } MyString MyString::operator+(const MyString& str) { int newsize = this->m_Size + str.m_Size + 1; char *temp = new char[newsize]; memset(temp, 0, newsize); strcat(temp, this->pString); strcat(temp, str.pString); MyString newstring(temp); delete[] temp; return newstring; } bool MyString::operator==(const char * str) { if (strcmp(this->pString, str) == 0 && strlen(str) == this->m_Size){ return true; } return false; } bool MyString::operator==(const MyString& str) { if (strcmp(this->pString, str.pString) == 0 && str.m_Size == this->m_Size){ return true; } return false; }
TestMyString.cpp
void test01() { MyString str("hello World"); cout << str << endl; //cout << "请输入MyString类型字符串:" << endl; //cin >> str; //cout << "字符串为: " << str << endl; //测试[] cout << "MyString的第一个字符为:" << str[0] << endl; //测试 = MyString str2 = "^_^"; MyString str3 = ""; str3 = "aaaa"; str3 = str2; cout << "str2 = " << str2 << endl; cout << "str3 = " << str3 << endl; //测试 + MyString str4 = "我爱"; MyString str5 = "北京"; MyString str6 = str4 + str5; MyString str7 = str6 + "天安门"; cout << str7 << endl; //测试 == if (str6 == str7) { cout << "s6 与 s7相等" << endl; } else { cout << "s6 与 s7不相等" << endl; } }