本文共 1150 字,大约阅读时间需要 3 分钟。
String类运算符重载函数
#include#include using namespace std;class String{public: String( ){p=NULL;} String(char *str); friend bool operator>(String &s1, String &s2); friend bool operator<(String &s1, String &s2); friend bool operator==(String &s1, String &s2); friend bool operator>=(String &s1, String &s2); friend bool operator<=(String &s1, String &s2); friend bool operator!=(String &s1, String &s2); void display( );private: char *p;};String::String(char *str){ p = new char[strlen(str)+1]; strcpy(p, str);} 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){ return !((s1>s2)||(s1 =(String &s1,String &s2){ return !(s1 s2);} bool operator!=(String &s1,String &s2){ return ((s1>s2)||(s1 s2) { s1.display( ); cout<<" > "; s2.display( ); } else if(s1
转载地址:http://sjsno.baihongyu.com/