运算符重载

来源:互联网 发布:java短信接口实例源码 编辑:程序博客网 时间:2024/05/18 02:34
设计一个字符串类String,通过运算符重载实现字符串的输入、输出以及+=、==、!=、<、>、>=、[ ]等运算
#include <iostream.h>#include <string.h>class String {private:int length;                     //字符串长度char *sPtr;                     //存放字符串的指针void setString( const char *s2);friend ostream &operator<<(ostream &os, const String &s); friend istream &operator>>(istream &is, String &s);  //重载输入运算符public:String( const char * = "" );            ~String();                             const String &operator=(const String &R); //重载赋值运算符 = const String &operator+=(const String &R);//字符串的连接 +=bool operator==(const String &R);//字符串的相等比较 ==bool operator!=(const String &R);//字符串的不等比较 !=bool operator!() ;               //判定字符串是否为空bool operator<(const String &R) const;     //字符串的小于比较 <bool operator>(const String &R);                //字符串的大于比较 >bool operator>=(const String &R);             //字符串的大于等于比较char &operator[](int);                         //字符串的下标运算};const String &String::operator+=(const String &R) {char *temp = sPtr;              length += R.length;           sPtr = new char[length+1]; strcpy(sPtr,temp );           strcat(sPtr,R.sPtr );         delete [] temp;                 return *this;                         }bool String::operator==(const String &R){return strcmp(sPtr,R.sPtr)==0;}bool String::operator!=(const String & R){return !(*this==R);}bool String::operator!(){return length ==0;}bool String::operator<(const String &R)const{return strcmp(sPtr,R.sPtr)<0;}bool String::operator>(const String &R){return R<*this;}bool String::operator>=(const String &R){return !(*this<R);}char &String::operator[](int subscript){return sPtr[subscript];}ostream &operator<<(ostream &os,const String &s){    os << s.sPtr;    return os;   }istream &operator>>(istream &is,String &s) {    char temp[100];                                     s=temp;                                               return is;                                       }int main(){    String s1("happy"),s2("new year"),s3;    cout << "s1 is " << s1 << "\ns2 is " << s2 << "\ns3 is " << s3       << "\n比较s2和s1:"<< "\ns2 ==s1结果是 " << ( s2 == s1 ? "true" : "false")<< "\ns2 != s1结果是 " << ( s2 != s1 ? "true" : "false")<< "\ns2 >  s1结果是 " << ( s2 >  s1 ? "true" : "false") << "\ns2 <  s1结果是 " << ( s2 <  s1 ? "true" : "false") << "\ns2 >= s1结果是 " << ( s2 >= s1 ? "true" : "false");cout << "\n\n测试s3是否为空: ";                         if (!s3){cout << "s3是空串"<<endl;                           //L3cout<<"把s1赋给s3的结果是:";                           s3 = s1;                                                cout << "s3=" << s3 << "\n";                          //L5    }    cout << "s1 += s2 的结果是:s1=";                      //L6    s1 += s2;                                           cout << s1;                                             //L7                                                            cout << "\ns1 +=  to you 的结果是:";                     //L8    s1 += " to you";                                   cout << "s1 = " << s1 <<endl;                              //L9    s1[0] = 'H';          s1[6] = 'N';    s1[10] = 'Y';    cout << "s1 = " << s1 << "\n";                               //L10    return 0;}