关于visual C++ string字符串相关的运算符重载“+ += == !=”

代码如下

/*输入两个字符串进行测试*/
#include <iostream.h>
#include <string.h>
class mstring
{
private:
	char *s;
	int len;
public:
	mstring(const char *str="sas");
	mstring(const mstring&str);
	friend mstring operator +(const mstring&str1,const mstring&str2);
	mstring& operator+=(const char *str);
	mstring& operator+=(const mstring &str);
    mstring& operator+(const char *str);	
	friend bool operator==(const mstring &, const char *);
	friend bool operator==(const mstring &, const mstring &);
    friend bool operator!=(const mstring &str1, const char *str2);
	friend bool operator!=(const mstring &str1, const mstring&str2);
    friend ostream& operator<<(ostream&out,const mstring&str);	
	friend istream& operator>>(istream&in,const mstring&str);
};
//构造函数
mstring::mstring(const mstring&str)
	{
		len=strlen(str.s);
		s=new char(len+1);
		strcpy(s,str.s);
	}
//构造函数
mstring::mstring(const char *str)
	{
		len=strlen(str);
		s=new char(len+1);
		strcpy(s,str);
	}
//重载运算符“+”
mstring operator +(const mstring&str1,const mstring&str2)
	{
		mstring str;
		str.len=str1.len+str2.len;
		str.s=new char(str.len+1);
		strcpy(str.s,str1.s);
		strcat(str.s,str2.s);
		return str;
	}
//重载运算符“+=”
mstring& mstring::operator+=(const char *str)
	{
		s=strcat(s,str);
	    return *this;
	}
//重载运算符“+=”
mstring& mstring::operator+=(const mstring &str)
	{
		s=strcat(s,str.s);
	    return *this;
	}
//重载运算符“+”
mstring& mstring::operator+(const char *str)
	 {
		 mstring str1;
		 str1.len=len;
		 str1.s=new char(len+1);
		 str1.s=s;
		 s=new char(len+strlen(str)+1);
		 s=strcat(str1.s,str);
		 return *this;
	 }
//重载运算符“<<”
ostream& operator<<(ostream&out,const mstring&str)
	{
		out<<str.s<<endl;
		return out;
	}
//重载运算符“>>”
istream& operator>>(istream&in,const mstring&str)
	{   cout<<"please import :"<<endl;
		in>>str.s;
		return in;
	}
	
//重载运算符“= =”
bool operator==(const mstring &str1, const char *str2)
{
	if(strcmp(str1.s,str2) == 0)
	return true;
	return false;
}
//重载运算符“!=”
bool operator!=(const mstring &str1, const char *str2)
{
	if(strcmp(str1.s,str2) == 0)
	return false;
	return true;
}
//重载运算符“=  =”
bool operator==(const mstring &str1, const mstring &str2)
{
if(strcmp(str1.s,str2.s) == 0)
	return true;
	return false;
}
 //重载运算符“!=”
bool operator!=(const mstring &str1, const mstring &str2)
{
	if(strcmp(str1.s,str2.s) == 0)
	return false;
	return true;
}
void main()
{

   mstring str1("ma");
   
   mstring str2("yun");
   cout<<"str1="<<str1;
   cout<<"str2="<<str2;  
   mstring str3;
   cout<<"str3  ";
   cin>>str3;
   mstring str4;
   cout<<"str4  ";
   cin>>str4;
   mstring str5;
   str5=str3+str1;
   cout<<"str3+str1="<<str5<<endl;
   str4+=str3;
   cout<<"sre4+=str3="<<str4<<endl;
   str2=str2+"sss";
   cout<<"str2+sss="<<str2<<endl;
   str1+="123";
   cout<<"str1 += 123="<<str1<<endl;
	cout<<"str1 == str1 ?";
   if(str1==str1)
   {
	   cout<<" YES"<<endl<<endl;
   }
	cout<<"str1 !=s tr2 ?";
   if(str1!=str2)
   {
	   cout<<" YES"<<endl;
   }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值