字符串类String重载操作Word文件下载.docx
《字符串类String重载操作Word文件下载.docx》由会员分享,可在线阅读,更多相关《字符串类String重载操作Word文件下载.docx(3页珍藏版)》请在冰豆网上搜索。
45.定义一个字符串类String,用来存放不定长的字符串,重载运算符"
>
,用于两个字符串的大于的比较运算。
#include<
iostream>
string>
usingstd:
:
string;
classString
{
private:
char*p;
public:
String()
{
p=NULL;
}
String(char*pp)
p=pp;
friendbooloperator==(String&
a,String&
b);
friendbooloperator>
(String&
a,String&
friendbooloperator<
voiddisplay()
std:
cout<
p;
};
booloperator==(String&
b)
if(strcmp(a.p,b.p)==0)
returntrue;
else
returnfalse;
}
booloperator>
if(strcmp(a.p,b.p)>
0)
booloperator<
if(strcmp(a.p,b.p)<
voidcompare(Stringa,Stringb)
if(operator>
(a,b))
a.display();
"
;
b.display();
elseif(operator<
=="
voidmain()
Stringstring("
abc"
);
Stringstring1("
abcs"
compare(string,string1);
system("
pause"