?? p324 重載雙目運算符2.cpp
字號:
# include<iostream>
# include<string>
using namespace std;
class String
{
private:
char*p;
public:
String()
{
p=NULL;
}
String(char*str)
{
p=str;
}
void display();
friend bool operator==(String &s1,String &s2);
friend bool operator<(String &s1,String &s2);
friend bool operator>(String &s1,String &s2);
};
void String::display()
{
cout<<p;
}
bool operator==(String &s1,String &s2)
{
if(strcmp(s1.p,s2.p)==0) return true;
else return false;
}
bool operator<(String &s1,String &s2)
{
if(strcmp(s1.p,s2.p)<0) return true;
else return false;
}
bool operator>(String &s1,String &s2)
{
if(strcmp(s1.p,s2.p)>0) return true;
else return false;
}
void compare(String &s1,String &s2 )
{
if(s1>s2) {s1.display();cout<<">"; s2.display();}
else if(s1==s2) {s1.display();cout<<"="; s2.display();}
else if(s1<s2) {s1.display();cout<<"<"; s2.display();}
}
int main()
{ String s1("songying"),s2("laoda");
compare(s1,s2);
system("pause");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -