第四章运算符重载

#ifndef _STRING_
#define _STRING_
#include "iostream"

using std::ostream;
using std::istream;


class String
{
private:
	char *s;

public:
	String();
	String(int n);
	String(const char *str);
	String(const String& s2);
	~String();
	String& operator = (const char *str);
	String& operator = (const String& s1);
	friend String& operator + (const String& s1,const String& s2);
	friend bool operator == (const String& s1,const String& s2);
	friend bool operator != (const String& s1,const String& s2);
	friend bool operator >= (const String& s1,const String& s2);
	friend bool operator <= (const String& s1,const String& s2);
	friend bool operator >  (const String& s1,const String& s2);
	friend bool operator <  (const String& s1,const String& s2);
	friend ostream& operator << (ostream& output,const String& s1);
	friend istream& operator >> (istream& input ,String& s1);
};



#endif

#include "String.h"
#include "cstring"
#include "iostream"

using std::ostream;
using std::istream;
using std::endl;
using std::cout;

String s3;

String::String()
{
	s=new char[100];
	s[0]='\0';
}

String::String(int n)
{
	s=new char[n];
	s[0]='\0';
}

String::String(const char *str)
{
	s=new char[100];
	strcpy(s,str);
}

String::String(const String& s2)
{
	strcpy(s,s2.s);
}

String::~String()
{
	if(s!=NULL)
		delete []s;
}

String& String::operator = (const char *str)
{
	strcpy(s,str);
	return *this; 
}

String& String::operator = (const String& s1)
{
	strcpy(s,s1.s);
	return *this;
}

String& operator + (const String& s1,const String& s2)
{
	extern String s3;
	strcat(s3.s,s1.s);
	strcat(s3.s,s2.s);

	return s3;
}

bool operator == (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool operator != (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)==0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

bool operator >= (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)>=0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator <= (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)<=0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator >  (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator <  (const String& s1,const String& s2)
{
	if(strcmp(s1.s,s2.s)<0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

ostream& operator << (ostream& output,const String& s1)
{
	output<<s1.s;
	return output;
}

istream& operator >> (istream& input ,String& s1)
{
	input>>s1.s;
	return input;
}

#include "iostream"
#include "String.h"

using std::cin;
using std::cout;
using std::endl;


int main(int argc, char const *argv[])
{
	String A("Poi");
	cout<<A<<endl;
	String B("ioP");
	cout<<B<<endl;
	String C;
	String D(A);
	cout<<D<<endl;
	C=A+B;
	cout<<C<<endl;
	String E("A");
	
	
	if(A==D) cout<<"A==D"<<endl;
	if(A<=B) cout<<"A<=B"<<endl;
	if(A>=E) cout<<"A>=E"<<endl;
	if(A<B) cout<<"A<B"<<endl;
	if(A>E) cout<<"A>E"<<endl;
	if(A!=B) cout<<"A!=B"<<endl;
	return 0;
}
<img src="https://img-blog.csdn.net/20150622204634859?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ3hpdWh1YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值