




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、/ 我希望利用這個String類和其他少數(shù)類外定義的函數(shù)可以實(shí)現(xiàn)對 String類的對象和C字符串 實(shí)現(xiàn) "無縫操作" , 即對兩者能使用相同的表達(dá)式進(jìn)行類似或相同的操作而無需加以區(qū)分/ 類外面定義類中的函數(shù)(成員或友元)定義順序與他們在類中出現(xiàn)的順序一致,且用注釋編號以便查看/ char * 為0的情況有點(diǎn)煩人,因?yàn)樽址幚砗瘮?shù)不能處理它,而且不能使用cout輸出,使得編程時需要增加很多額外的代碼/ 刪掉了轉(zhuǎn)換函數(shù)const char *,因?yàn)樗沟脺p法和某些邏輯運(yùn)算出錯(overloads have two similar covernsions)/ 類String中
2、的初始化函數(shù)String( const char * )相當(dāng)重要#include <iostream>#include <cstring>using namespace std;class String protected:int Length;char *Sp;public:String() Sp=0; Length=0; String( const String & ); /No.1.0String( const char * s )if (!s) Length=0; Sp=0; return ; Length = strlen( s ) ;Sp = new
3、 char Length + 1 ;strcpy ( Sp, s );void show() if(Sp) cout<< Sp ; /輸出字符串friend ostream & operator << ( ostream & , const String & ); /No.1.1 String() if(Sp) delete Sp; friend bool IsIn( const String &, const char ); /No.2 母字符串為第一個參數(shù),要查找的字符為第二個參數(shù)/能處理(String,char), (char *,
4、char)friend bool IsSubStr( const String &, const String & ); /No.3 母字符串為第一個參數(shù),要查找的子字符串為第二個參數(shù)/能處理(String,String),(String,char*),(char *,String)和(char *,char *)/默認(rèn)空字符串為任意字符串的子字符串int GetLen() return Length; /取長度char * GetString() return Sp; /取字符串/當(dāng)指針值為0時,返回后,在調(diào)用函數(shù)中的操作可能會出事String & operator
5、= ( String & ); /No.4 /能處理 String=String 和String= char */因?yàn)?operator = 只能是成員函數(shù),所以不能處理第一個操作數(shù)是char*類型的情況/下面從No.5到No.7.6重載的操作符的操作數(shù)必須至少有一個是String類的對象,/如果兩個操作數(shù)都是char*類型的,則必須在至少其中一個之前加上強(qiáng)制轉(zhuǎn)換符String,/這可以說是一個比較大的缺陷friend String operator + ( const String &, const String & ); /No.5friend String ope
6、rator - ( const String &, const String & ); /No.6friend bool operator < ( const String &, const String & ); /No.7.1friend bool operator > ( const String &, const String & ); /No.7.2friend bool operator <= ( const String &, const String & ); /No.7.3friend bool
7、 operator >= ( const String &, const String & ); /No.7.4friend bool operator = ( const String &, const String & ); /No.7.5friend bool operator != ( const String &, const String & ); /No.7.6String operator += ( const String &) ; /No.8/能處理 String+=String 和 String+=char*,
8、下同/對于char*+=String 和 char*+=char*,必須在char*前加上強(qiáng)制轉(zhuǎn)換符String,下同String operator -= ( const String & ); /No.9; / Start defining functions in class String/No.1.0String:String( const String &s )Length = s.Length;if ( s.Sp ) Sp = new char Length + 1 ;strcpy( Sp, s.Sp );else Sp=0;/No.1.1ostream &
9、operator << ( ostream &stream, const String &str )if(str.Sp) stream << str.Sp ;return stream;/No.2bool IsIn ( const String &str, const char c )if ( !str.Sp ) return 0;const char *p = str.Sp ; while( *p ) if( *p + = c ) return 1;return 0;/No.3bool IsSubStr ( const String &
10、str1, const String &str2 )if ( !str2.Sp ) return 1;else if ( !str1.Sp ) return 0;else if ( strstr( str1.Sp, str2.Sp ) ) return 1;else return 0;/No.4String & String:operator = ( String &str )if (Sp) delete Sp;Length = str.Length;if( str.Sp ) Sp = new char Length + 1 ;strcpy( Sp, str.Sp );
11、else Sp=0;return *this;/No.5String operator + ( const String &s1, const String &s2 )String temp;temp.Length = s1.Length + s2.Length;if( !temp.Length ) return temp; temp.Sp = new char temp.Length + 1 ;if( s1.Sp ) strcpy( temp.Sp, s1.Sp );if( s2.Sp ) strcat( temp.Sp, s2.Sp );return temp;/No.6S
12、tring operator - ( const String &str1, const String &str2 ) if( (! str1.Length) | (! str2.Length) ) return str1;int i=0;char *p1=str1.Sp, *p2=0;String temp;if( p2=strstr( str1.Sp, str2.Sp ) ) temp.Length = str1.Length - str2.Length;temp.Sp = new char temp.Length + 1 ;while( p1<p2 ) temp.S
13、pi+=*p1+ ;p1 += str2.Length;while( temp.Spi+=*p1+ ) ;elsetemp.Length = str1.Length;temp.Sp = new char str1.Length + 1 ;strcpy( temp.Sp, str1.Sp );return temp;/No.7.1bool operator < ( const String &str1, const String &str2 )if( str1.Sp && str2.Sp ) return ( strcmp( str1.Sp, str2.Sp
14、 ) < 0 );else if ( str2.Sp ) return true;else return false;/No.7.2bool operator > ( const String &str1, const String &str2 )if ( str1.Sp && str2.Sp ) return ( strcmp( str1.Sp, str2.Sp ) > 0 );else if ( str1.Sp ) return true;else return false;/No.7.3bool operator <= ( cons
15、t String &str1, const String &str2 )return !( str1 > str2 );/No.7.4bool operator >= ( const String &str1, const String &str2 )return !( str1 < str2 );/No.7.5bool operator = ( const String &str1, const String &str2 )if( str1.Sp && str2.Sp ) return !( strcmp( s
16、tr1.Sp, str2.Sp ) );else if ( (!str1.Sp) && (!str2.Sp) ) return true;else return false;/No.7.6bool operator != ( const String &str1, const String &str2 )return !( str1=str2 );/No.8String String:operator += ( const String &str )(*this) = (*this) + str;return *this;/No.9String Stri
17、ng:operator -= ( const String &str )(*this) = (*this) - str;return *this;int main()String str1("I love you"), str2(str1);str1.show();cout << 'n' << str2 << 'n'cout <<"n-n"cout.setf( ios:boolalpha );char ch='o'String str3("y
18、ou");cout<< IsIn( str1, ch ) <<endl;cout<< IsIn( "I love you", ch ) <<endl;cout<< IsSubStr( str1, str3 ) << endl;cout<< IsSubStr( str1, "you" ) << endl;cout<< IsSubStr( "I love you", str3 ) << endl;cout<
19、;< IsSubStr( "I love you", "you" ) << endl;cout<< "n-n"cout<< "The length of str1 is " << str1.GetLen() <<endl;char *s1 = str1.GetString();cout<< "The string of str1 is : " << s1 << endl;cout<<
20、 "n-n"String str4("I love you"), str5(", Jesus Christ"), str6, str7("Christ"), str8;str6 = str4 + str5;cout<< str6 <<endl;cout<< str4 + ", Jesus Christ" <<endl;cout<< "I love you" + str5 <<endl;cout<&l
21、t; "I love you" + (String)", Jesus Christ" <<endl;cout<< str6 - str7 <<endl;cout<< str6 - "Christ" <<endl;cout<< "I love you, Jesus Christ" - str7 <<endl;cout<< "I love you, Jesus Christ" - (String)"Christ" <<endl;str8 = str4 + str5 - str7 - " you," - "I "cout<< str8 <<endl;cout<<"n-n"String str9("Good"), str
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 如何做護(hù)理新技術(shù)
- 人版小學(xué)英語三年級(下冊)(全冊)教(學(xué))案教(學(xué))案
- 肝癌介入治療護(hù)理常規(guī)
- 家庭教育與家風(fēng)協(xié)同育人的策略探索
- 訂購設(shè)備的合同范本
- 10kV配電項目實(shí)施策略與優(yōu)化方案
- 小兒腦性癱瘓護(hù)理查房
- 2024年中央民族大學(xué)輔導(dǎo)員京外生源崗位招聘筆試真題
- 深圳航空招聘考試真題2024
- 妊娠合并心臟病常見護(hù)理診斷
- 生物質(zhì)能源綜合利用項目可行性分析報告
- 《印度文化與歷史:大學(xué)人文課程教案》
- 老年防詐騙知識講座課件
- 湖北省部分高中聯(lián)考協(xié)作體2023-2024學(xué)年高二下學(xué)期期中考試物理試卷(含答案)
- DB33-T 1411-2024 水利工程文化融合導(dǎo)則
- 2024湖南省新華書店有限責(zé)任公司招聘10人筆試參考題庫附帶答案詳解
- 檔案管理制度培訓(xùn)宣貫
- 農(nóng)機(jī)質(zhì)量跟蹤調(diào)查表
- 刑民交叉案件的司法認(rèn)定
- 2025年度股權(quán)合作協(xié)議書新版:跨境電商平臺股權(quán)合作協(xié)議
- GB/T 33136-2024信息技術(shù)服務(wù)數(shù)據(jù)中心服務(wù)能力成熟度模型
評論
0/150
提交評論