string 类的仿写

目录

1.基础框架

2.写入运算符重载函数

(1) operator=

(2)operator+

(3)operator<<

(4)operator>>

3完整代码


1.基础框架

头文件,命名空间声明,构造函数基础框架,测试函数,主函数。

class Mystring 
{
public:
	Mystring()
	{
		m_buf = NULL;
		m_size = 0;
		cout<<"Mystring() construct"<<endl;
	}

	Mystring(const char *p)
	{
		if (p == NULL)
		{
			return;
		}
		m_size = strlen(p);

		m_buf = new char[m_size+1];

		strcpy(m_buf,p);

	}

	Mystring(const Mystring &r)//拷贝构造函数
	{
		if (r.m_buf == NULL)//对远对象内的变量进行判空
		{
			return;
		}
		m_buf = new char[m_size+1];
		strcpy(m_buf , r.m_buf);
		m_size = r.m_size;

	}

	void show()
	{

		cout<<m_buf<<endl;
		cout<<m_size<<endl;
		//for (int i = 0;i<m_size;i+)
		//{
		//       cout<<m_buf[i];
		//}
		//cout<<endl;
	}

	~Mystring()
	{
		if (m_buf != NULL)
		{
			delete[] m_buf;

		}
		cout<<"~Mystring() deconstruct"<<endl;
		

	}

	private:

	char *m_buf;
	int m_size;
};


void test()
{
	Mystring str1();
	Mystring str2 = "hello world";
	str2.show();

	Mystring str3 = Mystring(str2);
	str3.show();
}

int main(int argc, const char *argv[])
{
	test();
	return 0;
}

2.写入运算符重载函数

string类中的重载运算符有:>>,<<,+,=。

(1) operator=

对于赋值运算符后接字符串或者string类对象进行两种函数重载

Mystring &operator=(const Mystring &s)
		{
			if (m_buf == s.m_buf)
			{
				return *this;
			}
			if (s.m_buf != NULL)
			{ 
				m_buf = new char[strlen(s.m_buf)+1];
				strcpy(m_buf,s.m_buf);
				m_size = strlen(m_buf);
			}
			else 
			{
				m_buf == NULL;
			}

			return *this;
		}

		Mystring &operator=(const char *s)
		{
			if (m_buf == NULL)
			{
				delete[] m_buf;
			}
			if (s != NULL)
			{
				m_buf = new char[strlen(s)+1];
				strcpy(m_buf,s);
			}
			else
			{
				m_buf = NULL;
			}

			return *this;
		}

(2)operator+

对于string类对象相加时候,字符串实现拼接同strcat效果

	Mystring operator+(Mystring &r)
		{
			Mystring tmpd;
			tmpd.m_buf = strcat(m_buf,r.m_buf);
			tmpd.m_size = m_size + r.m_size;
			return tmpd;
		}

(3)operator<<

cout<<str1————对左移运算符进行重载实现能够使用cout对string类的对象直接进行输出

//在类中进行友元函数声明
friend ostream &operator<<(ostream &out,const Mystring &r);

ostream &operator<<(ostream &out,const Mystring &r)
{
	out << r.m_buf;

	return out;

}

(4)operator>>

在类中友元函数声明

friend istream &operator>>(istream &in,Mystring &r);
istream &operator>>(istream &in,Mystring &r)
{
	r.m_buf = new char[1024];//需要分配空间,不分配空间会段错误
	in >> r.m_buf;
	r.m_size = strlen(r.m_buf);
	r.m_buf[r.m_size] = '\n';
	return in;
}

这样写是因为之前写的cin之后不会自动回车,故我在写入字符串后面直接加一个回车字符;该字符存入了m_buf中导致cout时候自动回车,感觉没多大问题,跳了。

3完整代码

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

class Mystring 
{
	public:
		Mystring()
		{
			m_buf = NULL;
			m_size = 0;
		}

		Mystring(const char *p)
		{
			if (p == NULL)
			{
				return;
			}
			m_size = strlen(p);

			m_buf = new char[m_size+1];

			strcpy(m_buf,p);

		}

		Mystring(const Mystring &r)//拷贝构造函数
		{
			if (r.m_buf != NULL)//对远对象内的变量进行判空
			{

				m_buf = new char[m_size+1];
				strcpy(m_buf , r.m_buf);
				m_size = r.m_size;
			}
		}

		Mystring operator+(Mystring &r)
		{
			Mystring tmpd;
			tmpd.m_buf = strcat(m_buf,r.m_buf);
			tmpd.m_size = m_size + r.m_size;
			return tmpd;
		}

		Mystring &operator=(const Mystring &s)
		{
			if (m_buf == s.m_buf)
			{
				return *this;
			}
			if (s.m_buf != NULL)
			{ 
				m_buf = new char[strlen(s.m_buf)+1];
				strcpy(m_buf,s.m_buf);
				m_size = strlen(m_buf);
			}
			else 
			{
				m_buf == NULL;
			}

			return *this;
		}

		Mystring &operator=(const char *s)
		{
			if (m_buf == NULL)
			{
				delete[] m_buf;
			}
			if (s != NULL)
			{
				m_buf = new char[strlen(s)+1];
				strcpy(m_buf,s);
			}
			else
			{
				m_buf = NULL;
			}

			return *this;
		}

		friend ostream &operator<<(ostream & out,const Mystring &r);
		friend istream &operator>>(istream & in,Mystring &r);

		void show()
		{

			cout<<m_buf<<endl;
			cout<<m_size<<endl; 
			//for (int i = 0;i<m_size;i+)
			//{
			//       cout<<m_buf[i];
			//}
			//cout<<endl;
		}

		~Mystring()
		{
			if (m_buf == NULL)//m_buf为空时候,释放空间
			{
				delete[] m_buf;

			}
		}

	private:

		char *m_buf;
		int m_size;
};


ostream &operator<<(ostream &out,const Mystring &r)
{
	out << r.m_buf;

	return out;

}

istream &operator>>(istream &in,Mystring &r)
{
	r.m_buf = new char[1024];//需要分配空间,不分配空间会段错误
	in >> r.m_buf;
	r.m_size = strlen(r.m_buf);
	r.m_buf[r.m_size] = '\n';
	return in;
}


void test()
{
	Mystring str1 = "hello";
	Mystring str2 = " world";
	str1.show();
	str2.show();
	cout << str1 << endl;
	cout << str2 << endl;

	Mystring str3;
	str3 = str1 + str2;
	str3.show();

	Mystring str4;
	cin >> str4;
	cout << str4;
	str4.show();
}

int main(int argc, const char *argv[])
{
	test();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yh_lhn_20

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值