#include<iostream.h>
#include<string.h>
//using namespace std;
class String{
public:
String(){p=NULL;}//定义默认构造函数
String(char *str);//构造函数重载
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display();
private:
char *p;//char型指针,指向字符串
};
String::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator>(String &string1,String &string2)
{
if(strcmp(string1.p ,string2.p )>0)
{
return true;
}
else
return false;
}
bool operator<(String &string1,String &string2)
{
if(strcmp(string1.p ,string2.p )<0)
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{
if(strcmp(string1.p ,string2.p )==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{
if(operator>(string1,string2)==1)
{
string1.display();
cout<<">";
string2.display();
}
else
if(operator<(string1,string2)==1)
{
string1.display();
cout<<"<";
string2.display();
}
else
if(operator==(string1,string2)==1)
{
string1.display();
cout<<"=";
string2.display();
}
cout<<endl;
}
int main()
{
String string1("hello"),string2("book"),string3("hello"),string4("computer");
compare(string1,string2);
compare(string2,string4);
compare(string1,string3);
return 0;
}
重载双目运算符
最新推荐文章于 2024-05-17 18:30:00 发布