C++:字符串封装

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;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值