字符串类String重载操作.docx
文本预览下载声明
43. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,,用 于两个字符串的等于比较运算。初值自拟。 44. 定义一个字符串类String,用来存放不定长的字符串,重载运算符,用于两个 字符串的小于的比较运算。初值自拟。 45. 定义一个字符串类String,用来存放不定长的字符串,重载运算符,用于两个 字符串的大于的比较运算。初值自拟。
#includeiostream
#includestring
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 p ;
}
};
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);
}
显示全部