c++ 实现String类

#include<iostream>
#include<string.h>
using namespace std;
class String {
public:
	String(const char* str) {
		this->mstr = new char[strlen(str) + 1];
		strcpy_s(this->mstr, strlen(str) + 1, str);
		this->m_size = strlen(mstr);
		cout << "调用构造函数" << endl;
	}
	String(const String& str) {
		if (this->mstr != NULL)
		{
			delete[] mstr;
			this->mstr = NULL;
		}
		mstr = new char[strlen(str.mstr) + 1];
		strcpy_s(this->mstr, strlen(str.mstr) + 1, str.mstr);
		this->m_size = strlen(mstr);
		cout << "调用拷贝构造函数" << endl;
	}
	~String()
	{
		if (this->mstr != NULL)
		{
			delete[] mstr;
			this->mstr = NULL;
		}
		cout << "调用析构函数" << endl;

	}
	friend ostream& operator<<(ostream& out, String& str);
	friend istream& operator>>(istream& in, String& str);
	String operator+(String &str)
	{
		
		int newsize = this->m_size + str.m_size + 1;
		char *temp = new char[newsize];
		memset(temp, 0, newsize);
		strcat(temp, this->mstr);
		strcat(temp, str.mstr);
		String newstring(temp);
		delete[] temp;
		return newstring;
	}
	String& operator+(const char* str)
	{
	/*	char* tem = new char[this->m_size + strlen(str) + 1];
		memset(tem, 0, this->m_size + strlen(str) + 1);
		strcat(tem, this->mstr);
		strcat(tem, str);
		String mystring(tem);
		delete[] tem;
		return mystring;*/
		int newsize = this->m_size + strlen(str) + 1;
		char *temp = new char[newsize];
		memset(temp, 0, newsize);
		strcat(temp, this->mstr);
		strcat(temp, str);
		static String newstring(temp);
		if (temp != NULL)
		{
			delete[] temp;
			temp = NULL;
		}
		delete[] temp;
		return newstring;
		
	}
	bool operator==(String &str)
	{
		if (strcmp(this->mstr, str.mstr) == 0 && this->m_size == str.m_size)
		{
			return true;
		}
		return false;
	}

	bool operator==(const char* str)
	{
		if (strcmp(this->mstr, str) == 0 && this->m_size == strlen(str))
		{
			return true;
		}
		return false;
	}
	char& operator[](int index)
	{

		return this->mstr[index];

	}
	String& operator=(const char* str)
	{
		if (this->mstr != NULL)
		{
			delete[] this->mstr;
			this->mstr = NULL;
		}
		this->mstr = new char[strlen(str) + 1];
		strcpy_s(mstr, strlen(str) + 1, str);
		return *this;
	}
	String& operator=(String& str)
	{
		if (this->mstr != NULL)
		{
			delete[] this->mstr;
			this->mstr = NULL;
		}
		this->mstr = new char[strlen(str.mstr) + 1];
		strcpy_s(mstr, strlen(str.mstr) + 1, str.mstr);
		return *this;
	}

private:
	char* mstr;
	int m_size;
};
ostream& operator<<(ostream& out, String& str)
{
	out << str.mstr << endl;
	return out;
}
istream& operator>>(istream& in, String& str)
{
	if (str.mstr != NULL)
	{
		delete[] str.mstr;
		str.mstr = NULL;
	}
	char tem[1024];
	in >> tem;
	str.mstr = new char[strlen(tem) + 1];
	strcpy_s(str.mstr, strlen(tem) + 1, tem);
	str.m_size = strlen(str.mstr);
	return in;
}
void test()
{
	String str("hello World");
	cout << str << endl;
	//cout << "请输入 MyString 类型字符串:" << endl;
	//cin >> str;
	//cout << "字符串为: " << str << endl;
	//测试[]
	cout << "MyString 的第一个字符为:" << str[0] << endl;
	//测试 =
	String str2 = "^_^";
	String str3 = "";
	str3 = "aaaa";
	str3 = str2;
	cout << "str2 = " << str2 << endl;
	cout << "str3 = " << str3 << endl;
	//测试 +
	String str4 = "我爱";
	String str5 = "玩电脑";
	String str6 = str4 + str5;
	cout << str6 << endl;
	String str7 = str4 + "天安门";
	cout << str7 << endl;
	测试 ==
	if (str6 == str7)
	{
		cout << "s6 与 s7 相等" << endl;
	}
	else
	{
		cout << "s6 与 s7 不相等" << endl;
	}
}
int main()
{
	test();
	return 0;
}

在String类中,对==、+、>>、<<、=进行重载、对构造函数、拷贝构造函数、析构函数进行实现。注意空间的泄露的问题。

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值