編寫類String的構造函數、析構函數和賦值函數。
重載了輸入(>>),輸出(<<),大於(>),小於(<),賦值(=),等於(==)運算符。
因為篇幅原因,沒有寫關於strlen(),strcpy()函數的實現.
//String 類 //用到了strlen、strcpy函數 #include#include using namespace std; class String { public: String(const char *str=NULL);//構造普通函數 String(const String &other);//拷貝構造函數 ~String(void); String &operator=(const String &other);//重載= friend bool operator==(const String &s1,const String &s2);//重載== friend bool operator>(const String &s1,const String &s2);//重載> friend bool operator<(const String &s1,const String &s2);//重載< friend ostream &operator<<(ostream &,const String &other);//重載<< friend istream &operator>>(istream &,String &other);//重載>> private: char *m_data; }; String::String(const char *str) { if(str==NULL) { m_data=new char[1]; m_data='\0'; } else { int length=strlen(str); m_data=new char[length+1]; strcpy(m_data,str); } } String::~String(void) { delete []m_data; } String::String(const String &other) { int length=strlen(other.m_data); m_data=new char[length+1]; strcpy(m_data,other.m_data); } String & String::operator=(const String &other) { if(&other==this) return *this; delete []m_data; int length=strlen(other.m_data); m_data=new char[length+1]; strcpy(m_data,other.m_data); return *this; } ostream& operator<<(ostream&output,const String &other) { output< >(istream&input,String &other) { char p[256]; input>>p; int length=strlen(p); other.m_data=new char [length+1]; strcpy(other.m_data,p); return input; } bool operator==(const String&s1,const String&s2) { int len_s1=strlen(s1.m_data); int len_s2=strlen(s2.m_data); if(len_s1!=len_s2) return false; int i=0; while(s1.m_data[i]==s2.m_data[i]) { i++; if(i==len_s1) return true; } return false; } bool operator>(const String&s1,const String&s2) { int len=strlen(s1.m_data)>strlen(s2.m_data)?strlen(s2.m_data):strlen(s1.m_data); for(int i=0;i s2.m_data[i]) { return true; } else if(s1.m_data[i]==s2.m_data[i]) { continue; } else { return false; } } if(strlen(s1.m_data)>strlen(s2.m_data)) return true; else return false; } bool operator<(const String&s1,const String&s2) { int len=strlen(s1.m_data)>strlen(s2.m_data)?strlen(s2.m_data):strlen(s1.m_data); for(int i=0;i s2.m_data[i]) { return false; } else if(s1.m_data[i]==s2.m_data[i]) { continue; } else { return true; } } if(strlen(s1.m_data) >s; cout<<"s:"< b) cout<<"s>b"<