欢迎来到天天文库
浏览记录
ID:48995669
大小:18.95 KB
页数:6页
时间:2020-02-26
《运算符重载string运算符.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、运算符重载#include#include"FunnyString.h"usingstd::istream;usingstd::ostream;usingstd::string;usingstd::cout;usingstd::cin;usingstd::endl;intmain(){FunnyStringstring1,string2;cout<<"Pleaseinputs1:";cin>>string1;cout<<"Pleaseinputs2:";cin>>string2;cout<<"未
2、进行s1-=s2运算时的s1*s2is"<3、2is"<usingstd::istream;usingstd::ostream;usingstd::string;usingstd::cout;usingstd::cin;usingstd::endl;#include"FunnyString.h"voidFunnyString::setFunnyString(constchar*a){sPtr=4、newchar[length+1];if(a!=0)strcpy(sPtr,a);elsesPtr[0]=' ';}FunnyString::FunnyString(constchar*first):length((first!=0)?strlen(first):0){setFunnyString(first);}FunnyString::FunnyString(constFunnyString&a){length=a.length;sPtr=newchar[length+1];for(inti=0;i5、th;i++)sPtr[i]=a.sPtr[i];}intFunnyString::getLength()const{returnlength;}FunnyString::~FunnyString(){delete[]sPtr;}ostream&operator<<(ostream&output,constFunnyString&a){for(inti=0;i>(istream&input,6、FunnyString&a){charb[100];input>>b;a.length=strlen(b);delete[]a.sPtr;a.sPtr=newchar[a.length+1];for(inti=0;i7、nnyString(a.sPtr);return*this;}elsereturn*this;}constFunnyString&FunnyString::operator+=(constFunnyString&a){FunnyStringc;c=*this+a;*this=c;return*this;}constFunnyString&FunnyString::operator-=(constFunnyString&a){FunnyStringc;c=*this-a;*this=c;return*this;}co8、nstFunnyStringoperator+(constFunnyString&left,constFunnyString&right){intlen=left.length+right.length;char*a=newchar[len+1];a[1]=left.sPtr[1];for(inti=0;i
3、2is"<usingstd::istream;usingstd::ostream;usingstd::string;usingstd::cout;usingstd::cin;usingstd::endl;#include"FunnyString.h"voidFunnyString::setFunnyString(constchar*a){sPtr=
4、newchar[length+1];if(a!=0)strcpy(sPtr,a);elsesPtr[0]=' ';}FunnyString::FunnyString(constchar*first):length((first!=0)?strlen(first):0){setFunnyString(first);}FunnyString::FunnyString(constFunnyString&a){length=a.length;sPtr=newchar[length+1];for(inti=0;i5、th;i++)sPtr[i]=a.sPtr[i];}intFunnyString::getLength()const{returnlength;}FunnyString::~FunnyString(){delete[]sPtr;}ostream&operator<<(ostream&output,constFunnyString&a){for(inti=0;i>(istream&input,6、FunnyString&a){charb[100];input>>b;a.length=strlen(b);delete[]a.sPtr;a.sPtr=newchar[a.length+1];for(inti=0;i7、nnyString(a.sPtr);return*this;}elsereturn*this;}constFunnyString&FunnyString::operator+=(constFunnyString&a){FunnyStringc;c=*this+a;*this=c;return*this;}constFunnyString&FunnyString::operator-=(constFunnyString&a){FunnyStringc;c=*this-a;*this=c;return*this;}co8、nstFunnyStringoperator+(constFunnyString&left,constFunnyString&right){intlen=left.length+right.length;char*a=newchar[len+1];a[1]=left.sPtr[1];for(inti=0;i
5、th;i++)sPtr[i]=a.sPtr[i];}intFunnyString::getLength()const{returnlength;}FunnyString::~FunnyString(){delete[]sPtr;}ostream&operator<<(ostream&output,constFunnyString&a){for(inti=0;i>(istream&input,
6、FunnyString&a){charb[100];input>>b;a.length=strlen(b);delete[]a.sPtr;a.sPtr=newchar[a.length+1];for(inti=0;i7、nnyString(a.sPtr);return*this;}elsereturn*this;}constFunnyString&FunnyString::operator+=(constFunnyString&a){FunnyStringc;c=*this+a;*this=c;return*this;}constFunnyString&FunnyString::operator-=(constFunnyString&a){FunnyStringc;c=*this-a;*this=c;return*this;}co8、nstFunnyStringoperator+(constFunnyString&left,constFunnyString&right){intlen=left.length+right.length;char*a=newchar[len+1];a[1]=left.sPtr[1];for(inti=0;i
7、nnyString(a.sPtr);return*this;}elsereturn*this;}constFunnyString&FunnyString::operator+=(constFunnyString&a){FunnyStringc;c=*this+a;*this=c;return*this;}constFunnyString&FunnyString::operator-=(constFunnyString&a){FunnyStringc;c=*this-a;*this=c;return*this;}co
8、nstFunnyStringoperator+(constFunnyString&left,constFunnyString&right){intlen=left.length+right.length;char*a=newchar[len+1];a[1]=left.sPtr[1];for(inti=0;i
此文档下载收益归作者所有