字符串类String重载操作

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

43. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,,用于两个字符串的等于比较运算。初值自拟。44. 定义一个字符串类String,用来存放不定长的字符串,重载运算符"<",用于两个字符串的小于的比较运算。初值自拟。45. 定义一个字符串类String,用来存放不定长的字符串,重载运算符">",用于两个字符串的大于的比较运算。初值自拟。

#include

#include

using std::string;

class String

{

private:

char*p;

public:

String()

{

p=NULL;

}

String(char*pp)

{

p=pp;

}

friend bool operator ==(String&a,String&b);

friend bool operator >(String&a,String&b);

friend bool operator <(String&a,String&b);

void display()

{

std::cout<

}

};

bool operator ==(String&a,String&b)

{

if(strcmp(a.p,b.p)==0)

{

return true;

}

else

{

return false;

}

}

bool operator >(String&a,String&b)

{

if(strcmp(a.p,b.p)>0)

{

return true;

}

else

{

return false;

}

}

bool operator <(String&a,String&b) {

if(strcmp(a.p,b.p)<0)

{

return true;

}

else

{

return false;

}

}

void compare(String a,String b)

{

if(operator>(a,b))

{

a.display();

std::cout<<">";

b.display();

}

else if(operator<(a,b))

{

a.display();

std::cout<<"<";

b.display();

}

else

{

a.display();

std::cout<<"==";

b.display();

}

}

void main()

{

String string("abc");

String string1("abcs");

compare(string,string1);

system("pause");

}

相关文档
最新文档