自己实现string<char>类

本文介绍了一个自定义C++字符串类的实现细节,包括构造函数、析构函数、赋值运算符重载及加法运算符重载等关键成员函数。特别关注了不同类型的字符串连接操作,并演示了如何通过友元函数来优化这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string.h>
using namespace std;
enum{
	ARRLEN=100
};

class String
{
	public:
	public:
	String(const char *str = NULL);
	String(const String &other);
	~ String(void);

	String&  operator=(const String &other);
	String   operator+(const String &other);//优先级高与 friend 系列加法重载
	String   operator+(const char *str);//优先级高与 friend 系列加法重载


	friend String operator+(const String &st,const char *str);
	friend String operator+(const char *str,const String &st);
	friend String operator+(const String &st,const String &st2);
	
	friend ostream & operator <<(ostream&,String &str);
    friend istream & operator >>(istream&,String &str);
	void show(){
		cout<<"m_data="<<m_data<<endl;
	}
	private:
	char *m_data;
};

String::String(const char *str)
{
	if(NULL == str)
	{
		m_data = new char[1];
		*m_data = '\0';
	}
	else
	{
		int len = strlen(str)+1;
		m_data=new char[len];
		stpcpy(m_data,str);
	}
	cout<<"\t\tString::String(const char *str)"<<endl;
}
String::String(const String &other)
{
	int len = strlen(other.m_data)+1;
	m_data=new char[len];
	stpcpy(this->m_data,other.m_data);
	cout<<"\t\tString::String(const String &other)"<<endl;
}
String::~ String()
{
	delete [] m_data;
	cout<<"\t\tString::~ String()"<<endl;
}
String& String::operator=(const String &other)
{
	if(this == &other)
	{
		return *this;
	}
	delete [] m_data;
	int len = strlen(other.m_data)+1;
	m_data=new char[len];
	stpcpy(this->m_data,other.m_data);
	cout<<"\t\tString & String::operate =(const String &other)"<<endl;
	return *this;
}
String String::operator+(const String &other)
{
	String ret; //The addition can not modify the object itself
	int ori_len = strlen(this->m_data);
	int oth_len = strlen(other.m_data);
	char* new_data = new char[ori_len + oth_len + 1 ];
	memcpy(new_data,m_data,ori_len);
	memcpy(new_data+ori_len,other.m_data,oth_len+1);
	ret.m_data= new_data;
	cout<<"\t\tString  String::operate+(const String &other)"<<endl;
	return ret;
}
String String::operator+(const char *str)
{
	String ret;
	if( NULL == str)
		return *this;
	int ori_len = strlen(this->m_data);
	int oth_len = strlen(str);
	char* new_data = new char[ori_len + oth_len + 1 ];
	memcpy(new_data,m_data,ori_len);
	memcpy(new_data+ori_len,str,oth_len+1);
	ret.m_data = new_data;
	cout<<"\t\tString  String::operate+(const char *str)"<<endl;
	return ret;
}
String operator+(const String &st,const char *str)
{
	String ret;
	int s_len = strlen(st.m_data);
	int c_len = strlen(str);
	char* new_data = new char[s_len + c_len + 1 ];
	memcpy(new_data,st.m_data,s_len);
	memcpy(new_data+s_len,str,c_len+1);
	ret.m_data = new_data;	
	cout<<"\t\tString operator+(const String &st,const char *str)"<<endl;
	return ret;
}
String operator+(const String &st1,const String &st2)
{
	String ret;
	int s_len1 = strlen(st1.m_data);
	int s_len2 = strlen(st2.m_data);
	char* new_data = new char[s_len1 + s_len2 + 1 ];
	memcpy(new_data,st1.m_data,s_len1);
	memcpy(new_data+s_len1,st2.m_data,s_len2+1);	
	ret.m_data = new_data;
	cout<<"\t\tString operator+(const String &st,const String &st2)"<<endl;
	return ret;
}
String operator+(const char *str,const String &st)
{
	String ret;
	int s_len = strlen(st.m_data);
	int c_len = strlen(str);
	char* new_data = new char[s_len + c_len + 1 ];
	memcpy(new_data,str,c_len);
	memcpy(new_data+c_len,st.m_data,s_len+1);	
	ret.m_data = new_data;	
	cout<<"\t\tString operator+(const char *str,const String &st)"<<endl;
	return ret;
}

ostream & operator <<(ostream& output,String &str)
{
	output<<str.m_data;
    return output;
}
istream & operator >>(istream& input,String &str)//Do not know how much space to allocate
{
	delete [] str.m_data;
	str.m_data = new char[ARRLEN];
	input>>str.m_data;
	return input; 
}
int main()
{
	String *s1 = new String("abc");
	String *s2 = new String("123");
	String *s3 = new String();
	String *s4 = new String();

	*s3 = *s2+*s1;
	//s3->show();
	cout<<*s3<<endl;
	cout <<"========================================"<<endl;

	*s4 = *s3 +"friend";
	//s4->show();
	cout<<*s4<<endl;
	cout <<"========================================"<<endl;

	*s4 = *s3 +"friend"+*s1;
	//s4->show();
	cout<<*s4<<endl;
	cout <<"========================================"<<endl;

	*s4 = "friend"+*s1;
	//s4->show();
	cout<<*s4<<endl;
	cout <<"========================================"<<endl;

	//s1->show();
	//s2->show();
	//s3->show();
	//s4->show();
	cin>> *s1;
	cout <<"========================================"<<endl;
	cout<<*s1<<endl;
	cout<<*s2<<endl;
	cout<<*s3<<endl;
	cout<<*s4<<endl;
	return 0;
}




                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值