歡迎大師前來幫助!
#include <iostream>
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;
};
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) == 1)
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("Computer"), string4("Hello");
compare(string1, string2);
compare(string2, string3);
compare(string1, string4);
return 0;
}
這是一個簡單地C++程式,我想問一下
friend bool operator >(String &string1, String &string2);
friend bool operator <(String &string1, String &string2);
friend bool operator ==(String &string1, String &string2);
void display();
里面的String &string1, String &string2
是什么意思?為什么要這樣?
uj5u.com熱心網友回復:
參考,就有點像在C中傳指標;傳入參考,則不會進行副本拷貝,效率高,而且是真實的比較這兩個數,而如果沒有這個參考,這是這兩個數的副本在比較……uj5u.com熱心網友回復:
參考——http://my.oschina.net/alphajay/blog/5251轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113004.html
標籤:基礎類
上一篇:哎 錯誤總是找不出來
下一篇:求問
