C++基础复习笔记(四)

c中没有字符串 字符串类(c风格的字符串)

//Mystring.h
class Mystring
{
	 //重载<<操作符
	friend ostream& operator<<(ostream &out,Mystring &s);
	//重载>>操作符
	friend istream& operator>>(ostream &in,Mystring &s);
public:
	Mystring();
	Mystring(const char* p);
	Mystring(const Mystring& s);
	~Mystring();
	//重载等号操作符
	Mystring& operator=(const char *p);
	Mystring& operator=(const Mystring &s);
	//重载[]操作符
	char & operator[](int index);
	//重载== !=
	bool operator==(const char *p);
	bool operator!=(const char *p);
	bool operator==(const Mystring &s);
	bool operator!=(const Mystring &s);
	//重载< >
	int operator<(const Mystring *p);
	int operator>(const Mystring *p);
	int opretor<(const Mystring &s);
	int opretor>(const Mystring &s);
	
	
private:
	char *m_p;
	int m_len;
public:
	//把类的指针取出来
	const char* c_str()
	{
		return m_p;
	}
}
//Mystring.cpp
Mystring::Mystring()
{
	m_len=0;
	m_p=new char[m_len+1];
	strcpy(m_p,"");
}
Mystring::Mystring(const char* p)
{
	if(p==NULL)
	{
		m_len=0;
		m_p=new char[m_len+1];
		strcpy(m_p."");
	}
	else
	{
		m_len=strlen(p);
		m_p=new char[m_len+1];
		strcpy(m_p,p);
	}
}
Mystring::Mystring(Mystring &s)
{
	m_len=s.m_len;
	m_p=new char[m_len+1];
	strcpy(m_p,s.m_p);
}
Mystring::~Mystring()
{
	if(m_p!=NULL)
	{
		delete [] m_p;
		m_len=0;
		m_p=NULL;
	}
}
Mystring& Mystring::operator=(const char *p)
{
	if(m_p!=NULL)
	{
		delete [] m_p;
		m_len=0;
	}
	if(p=NULL)
	{
		m_len=0;
		m_p=new char[m_len+1];
		strcpy(m_p."");
	}
	else
	{
		m_len=strlen(p);
		m_p=new char[m_len+1];
		strcpy(m_p,p);
	}
}
Mystring& Mystring::operator=(const Mystring &s)
{
	if(m_p!=NULL)
	{
		delete [] m_p;
		m_len=0;
	}
	m_len=s.m_len;
	m_p=new char[m_len+1];
	strcpy(m_p,s.m_p);	
}
//重载[]
char& Mystring::operator[](int index)
{
	return m_p[index];
}
//重载<<
ostream& Mystring::operator<<(ostream &out,Mystring &s)
{
	out<<s.mp;
	return out;
}
//重载>>
 istream& Mystring::operator>>(ostream &in,Mystring &s) {
 	cin>>s.mp;
 	return in;
}
//重载== !==
bool Mystring::operator==(const char *p)
{
    if(p==NULL)
    {
		if(m_len==0)
		{return ture;}
		else
		{return false;}
	}
	else
	{
		if (m_len!=strlen(p)}
		{
			return false;
		}
		else
		{	
			return !strcmp(m_p,p);
		}
	}
}
bool Mystring::operator!=(const char *p);
{
	return !(*this==p);
}
bool Mystring::operator==(const Mystring &s);
{
	if(m_len!=s.m_len) return false;
	if(!strcmp(m_p,s.m_p))
	{	
		return turn;
	}
	return false;
}
bool Mystring::operator!=(const Mystring &s);
{
	return !(*this=s);
}
//重载< >
int Mystring::operator<(const Mystring *p){
	return(strcmp(*this-m_p,p);
}
int Mystring::operator>(const Mystring *p){
	return(strcmp(p,*this->m_p);
}
int Mystring::opretor<(const Mystring &s){
	return(strcmp(*this-m_p,s.m_p);
}
int Mystring::opretor>(const Mystring &s){
	return(strcmp(s.m_p,*this->m_p);
}

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值