




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/ 我希望利用这个String类和其他少数类外定义的函数可以实现对 String类的对象和C字符串 实现 "无缝操作" , 即对两者能使用相同的表达式进行类似或相同的操作而无需加以区分/ 类外面定义类中的函数(成员或友元)定义顺序与他们在类中出现的顺序一致,且用注释编号以便查看/ char * 为0的情况有点烦人,因为字符串处理函数不能处理它,而且不能使用cout输出,使得编程时需要增加很多额外的代码/ 删掉了转换函数const char *,因为它使得减法和某些逻辑运算出错(overloads have two similar covernsions)/ 类String中
2、的初始化函数String( const char * )相当重要#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 母字符串为第一个参数,要查找的字符为第二个参数/能处理(String,char), (char *,
4、char)friend bool IsSubStr( const String &, const String & ); /No.3 母字符串为第一个参数,要查找的子字符串为第二个参数/能处理(String,String),(String,char*),(char *,String)和(char *,char *)/默认空字符串为任意字符串的子字符串int GetLen() return Length; /取长度char * GetString() return Sp; /取字符串/当指针值为0时,返回后,在调用函数中的操作可能会出事String & operator
5、= ( String & ); /No.4 /能处理 String=String 和String= char */因为 operator = 只能是成员函数,所以不能处理第一个操作数是char*类型的情况/下面从No.5到No.7.6重载的操作符的操作数必须至少有一个是String类的对象,/如果两个操作数都是char*类型的,则必须在至少其中一个之前加上强制转换符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*前加上强制转换符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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 目标明确的信息系统项目管理师试题及答案
- 项目成功因素研究试题及答案
- 公共服务政策的公平性与效率分析试题及答案
- 软件设计师考试定制化复习试题及答案
- 计算机软件测试在环境政策评估中的应用试题及答案
- 计算机软件测试中的常见问题试题及答案
- 公共政策的全球视野与本土化探讨试题及答案
- 软件设计师考试技能提升路线试题及答案
- 现代公共政策理论框架试题及答案
- 如何建立健全公共政策的决策制度试题及答案
- 数字经济学导论-全套课件
- 数字人民币专题分析
- RITTAL威图空调中文说明书
- 马工程教育学项贤明第九章-教师与学生
- 精选最近九年北京高考数学(理)压轴题(含答案)
- XX市救护车管理办法
- GB/T 13460-2008再生橡胶
- 中小学学习《民法典》主题班会图文ppt
- 简明新疆地方史赵阳
- 12.注浆法施工技术(PPT版共60)
- TCVN-2622-越南建筑防火规范(中文版)
评论
0/150
提交评论