C++:字符串封装
编译器:vs2013
1、头文件代码(MyString.h)
// file name:MyString.h
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class MyString
{
// << operator overloaded
friend ostream& operator<<(ostream& cout, const MyString& str);
// >> operator overloaded
friend istream& operator>>(istream& cin, MyString& str);
private:
char* pstr; // used to save a string.
int size; // used to count the number of the char in a string.
public:
// default constructor
MyString();
// parameter constructor
MyString(const char* str);
// copy constructor
MyString(const MyString& str);
// destructor
~MyString();
// assignment operator overloaded
MyString& operator=(const char* str);
MyString& operator=(const MyString& str);
// [] operator overloaded
char& operator[](const int& index);
// + operator overloaded
MyString operator+(const char* str);
MyString operator+(const MyString& str);
// == operator overloaded
bool operator==(const char* str);
bool operator==(const MyString& str);
};
2、源文件代码(MyString.cpp)
// file name: MyString.cpp
#include "MyString.h"
// default constructor
MyString::MyString()
{
this->pstr = NULL;
this->size = 0;
}
// parameter constructor
MyString::MyString(const char* str)
{
if (NULL == str)
{
this->pstr = NULL;
this->size = 0;
return;
}
this->size = strlen(str);
this->pstr = new char[this->size+1];
strcpy(this->pstr, str);
}
// copy constructor
MyString::MyString(const MyString& str)
{
if (NULL == str.pstr)
{
this->pstr = NULL;
this->size = 0;
return;
}
this->size = str.size;
this->pstr = new char[this->size+1];
strcpy(this->pstr, str.pstr);
}
// destructor
MyString::~MyString()
{
if (NULL != this->pstr)
{
delete[] this->pstr;
this->pstr = NULL;
}
this->size = 0;
}
// assignment operator overloaded
MyString& MyString::operator=(const char* str)
{
if (NULL != this->pstr)
{
delete[] this->pstr;
this->pstr = NULL;
}
if (NULL == str)
{
this->size = 0;
return *this;
}
this->size = strlen(str);
this->pstr = new char[this->size+1];
strcpy(this->pstr, str);
return *this;
}
MyString& MyString::operator=(const MyString& str)
{
if (NULL != this->pstr)
{
delete[] this->pstr;
this->pstr = NULL;
}
if (NULL == str.pstr)
{
this->size = 0;
return *this;
}
this->size = str.size;
this->pstr = new char[this->size + 1];
strcpy(this->pstr, str.pstr);
return *this;
}
// [] operator overloaded
char& MyString::operator[](const int& index)
{
assert(NULL != this->pstr);
return this->pstr[index];
}
// + operator overloaded
MyString MyString::operator+(const char* str)
{
MyString tmp;
if (NULL == this->pstr)
{
if (NULL == str)
{
return tmp;
}
else
{
tmp.size = strlen(str);
tmp.pstr = new char[tmp.size + 1];
strcpy(tmp.pstr, str);
return tmp;
}
}
else
{
if (NULL == str)
{
tmp.size = this->size;
tmp.pstr = new char[tmp.size + 1];
strcpy(tmp.pstr, this->pstr);
return tmp;
}
else
{
tmp.size = this->size + strlen(str);
tmp.pstr = new char[tmp.size + 1];
memset(tmp.pstr, 0, tmp.size + 1);
strcat(tmp.pstr, this->pstr);
strcat(tmp.pstr, str);
return tmp;
}
}
}
MyString MyString::operator+(const MyString& str)
{
MyString tmp;
if (NULL == this->pstr)
{
if (NULL == str.pstr)
{
return tmp;
}
else
{
tmp.size = str.size;
tmp.pstr = new char[tmp.size + 1];
strcpy(tmp.pstr, str.pstr);
return tmp;
}
}
else
{
if (NULL == str.pstr)
{
tmp.size = this->size;
tmp.pstr = new char[tmp.size + 1];
strcpy(tmp.pstr, this->pstr);
return tmp;
}
else
{
tmp.size = this->size + str.size;
tmp.pstr = new char[tmp.size + 1];
memset(tmp.pstr, 0, tmp.size + 1);
strcat(tmp.pstr, this->pstr);
strcat(tmp.pstr, str.pstr);
return tmp;
}
}
}
// == operator overloaded
bool MyString::operator==(const char* str)
{
if (NULL == str && NULL==this->pstr)
{
return true;
}
if (NULL != str && NULL != this->pstr)
{
return 0==strcmp(this->pstr, str);
}
return false;
}
bool MyString::operator==(const MyString& str)
{
if (NULL == str.pstr && NULL == this->pstr)
{
return true;
}
if (NULL != str.pstr && NULL != this->pstr)
{
return 0 == strcmp(this->pstr, str.pstr);
}
return false;
}
// << operator overloaded
ostream& operator<<(ostream& cout, const MyString& str)
{
cout << str.pstr;
return cout;
}
// >> operator overloaded
istream& operator>>(istream& cin, MyString& str)
{
if (NULL != str.pstr)
{
delete[] str.pstr;
str.pstr = NULL;
}
char buf[1024];
cin >> buf;
str.size = strlen(buf);
str.pstr = new char[str.size+1];
strcpy(str.pstr, buf);
return cin;
}
3、测试代码(Test.cpp)
// file name: Test.cpp
#include "MyString.h"
void Test01(){
MyString str1 = "Hello"; // MyString(const char* str)
cout << "str1 = " << str1 << endl;
str1 = "new one"; // MyString& operator=(const char* str)
cout << "new str1 = " << str1 << endl;
MyString str2 = str1; // MyString(const MyString& str)
cout << "str2 = "<<str2 << endl;
cout << "Please enter a new string: ";
cin >> str2; // istream& operator>>(istream& cin, MyString& str)
cout << "new str2 = " << str2 << endl;
MyString str3; // MyString()
str3 = str2; // MyString& operator=(const MyString& str)
cout << "str3 = " << str3 << endl; // ostream& operator<<(ostream& cout, const MyString& str)
cout << "str3[0] = " << str3[0] << endl; // char& operator[](const int& index)
str3[0] = '4'; // char& operator[](const int& index)
cout << "new str3 = " << str3 << endl; // ostream& operator<<(ostream& cout, const MyString& str)
MyString str4; // MyString()
str4 = str3 + " this is a test."; // MyString operator+(const char* str)
cout << "str4 = " << str4 << endl;
if (str4 == "hello world") // bool operator==(const char* str)
{
cout << "str4 == abcqwert" << endl;
}
else
{
cout << "str4 != abcqwert" << endl;
}
}
int main()
{
Test01();
system("pause");
return 0;
}