c++ string 函数的重载

来源:互联网 发布:wiley数据库 编辑:程序博客网 时间:2024/06/06 04:01

用C++实现一个String类,它具有比较,连接,输入,输出功能,并且提供一些测试实例说明如何使用这个类。注意不要使用MFC,STL以及其他库。

解析:

要实现这些功能,需要重载的运算符:

1 < >,==和 != 比较运算符。

2 +=连接运算符以及赋值运算符。

3 输出,输入运算符。

 

mystring.h  文件

[cpp] view plain copy
  1. #ifndef STRING_H  
  2. #define STRING_H  
  3. #include <ostream>  
  4. #include <istream>  
  5. using namespace std;  
  6. class mystring{  
  7. public:  
  8.     mystring();//默认构造函数  
  9.     mystring(int n,char c);//普通构造函数  
  10.     mystring(const char* source); //普通构造函数  
  11.     mystring(const mystring &s); //复制构造函数  
  12.     mystring& operator =(const mystring &s); //重载"="实现对象赋值  
  13.     mystring& operator =(const char *s);//重载"="实现字符串赋值  
  14.     ~mystring();//析构函数  
  15.     char & operator [](int i);//重载[],实现数组运算  
  16.     const char & operator [](int i) const;//重载[],实现数组运算(对象为常量)  
  17.     mystring & operator +=(const mystring & s);//重载+=,实现与对象相加  
  18.     mystring & operator +=(const char *s);//重载+=,实现与字符串相加  
  19.     friend ostream& operator <<(ostream &out,mystring &s);//重载<<,实现输出流  
  20.     friend istream& operator >>(istream &in,mystring &s);//重载>>,实现输入流  
  21.     friend bool operator <(const mystring &left,const mystring &right);//重载<  
  22.     friend bool operator >(const mystring &left,const mystring &right);//重载>  
  23.     friend bool operator ==(const mystring &left,const mystring &right);//重载==  
  24.     friend bool operator !=(const mystring &left,const mystring &right);//重载!=  
  25.     const char *getdata() const;//获取data指针  
  26.     int getlength() const;//  
  27. protected:  
  28. private:  
  29.     int size;//data表示的字符串长度  
  30.     char *data;//指向字符串数据  
  31. };  
  32.   
  33. #endif  


 

mystring.cpp文件

[cpp] view plain copy
  1. // 运算符重载.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "mystring.h"  
  6. #include <iostream>  
  7. using namespace std;  
  8. mystring::mystring(){  
  9.     data=new char[1];//空字符只含有'\0'一个元素  
  10.     *data='\0';  
  11.     size=0;  
  12. }  
  13. mystring::mystring(int n, char c){  
  14.     data=new char[n+1]; //含有n个相同字符的字符串  
  15.     for (int i=0;i<n;i++)  
  16.     {  
  17.         data[i]=c;  
  18.     }  
  19.     data[n]='\0';  
  20.     size=n;  
  21. }  
  22. mystring::mystring(const char *source){//字符串内容与source相同  
  23.     if (source==NULL) //source为NULL  
  24.     {  
  25.         data=new char [1];  
  26.         *data='\0';  
  27.         size=0;  
  28.     }  
  29.     else//source内容不为NULL  
  30.     data=new char[strlen(source)+1];  
  31.     strcpy(data,source);//复制source字符串  
  32.     size=strlen(source);  
  33.     }  
  34. }  
  35. mystring::mystring(const mystring &s){//复制构造函数,字符串内容与对象s的相同  
  36.     data=new char[s.size+1];  
  37.     strcpy(data,s.data);  
  38.     size=s.size;  
  39. }  
  40. mystring& mystring::operator =(const mystring &s){ //赋值函数  
  41.     if (this==&s)//如果对象s就是自己,直接返回  
  42.     {  
  43.         return *this;  
  44.     }  
  45.     if (data!=NULL)//释放data堆内存  
  46.     {  
  47.         delete [] data;  
  48.     }  
  49.     data =new char[s.size+1];//分配堆内存  
  50.     strcpy(data,s.data);//复制对象s的字符串成员  
  51.     return *this;  
  52. }  
  53. mystring& mystring::operator =(const char *s){//=重载  
  54.     if (data!=NULL)  
  55.     {  
  56.         delete [] data;  
  57.     }  
  58.     size=strlen(s);  
  59.     data =new char[size+1];  
  60.     strcpy(data,s);  
  61.     return *this;  
  62. }  
  63. mystring::~mystring(){  
  64.     if (data!=NULL) //data不为NULL,释放堆内存  
  65.     {  
  66.         delete [] data;  
  67.         data=NULL;  
  68.         size=0;  
  69.     }  
  70. }  
  71.   
  72. char & mystring::operator [](int i){ //[]重载  
  73.     return data[i];//取数组下标为i的字符元素  
  74. }  
  75. const char & mystring::operator [](int i) const{  
  76.     return data[i];  
  77. }  
  78. mystring & mystring::operator +=(const mystring &s){ //+=重载 连接对象s的字符串成员  
  79.     char *temp=data;  
  80.     size=s.size+size;  
  81.     data =new char[size+1];  
  82.     strcpy(data,temp);//复制原来的字符串  
  83.     strcat(data,s.data);//连接目标对象内的字符串成员  
  84.     delete [] temp;  
  85.     return *this;  
  86. }  
  87. mystring & mystring::operator +=(const char *s){//+=重载  连接s字符串  
  88.     if (s==NULL)//如果字符串s为NULL,直接返回  
  89.     {  
  90.         return *this;  
  91.     }  
  92.     size=strlen(s)+size;  
  93.     char *temp=data;  
  94.     data =new char[size+1];  
  95.     strcpy(data,temp);//复制原来的字符串  
  96.     strcat(data,s); //连接目标字符串  
  97.     delete [] temp;  
  98.     return *this;  
  99. }  
  100. ostream& operator <<(ostream &out,mystring &s){ //重载<<  
  101.     out<<s.getdata()<<endl;  
  102.     return out;  
  103. }  
  104.   
  105. istream & operator >>(istream &in,mystring &s){ //重载>>  
  106.     char p[50];  
  107.     in.getline(p,50);//从输入流接受最多50个字符  
  108.     s=p;//调用赋值函数  
  109.     return in;  
  110. }  
  111.   
  112. bool operator <(const mystring &left,const mystring &right){ //重载<  
  113.        int i=0;  
  114.     while (left[i]==right[i] && left[i]!='\0' && right[i]!='\0'){  
  115.           i++;  
  116.     }  
  117.     return left[i]-right[i]<0?true:false;  
  118. }  
  119. bool operator >(const mystring &left,const mystring &right){ //重载>  
  120.          int i=0;  
  121.         while (left[i]==right[i] && left[i]!='\0' && right[i]!='\0')  
  122.         {  
  123.             i++;  
  124.         }  
  125.         return left[i]-right[i]>0?true:false;  
  126. }  
  127. bool operator ==(const mystring &left,const mystring &right){  
  128.       int i=0;  
  129.     while (left[i]==right[i] && left[i]!='\0' && right[i]!='\0')  
  130.     {  
  131.         i++;  
  132.     }  
  133.     return left[i]==right[i]?true:false;  
  134. }  
  135. bool operator !=(const mystring &left,const mystring &right){  
  136.     int i=0;  
  137.     while (left[i]==right[i] && left[i]!='\0' && right[i]!='\0')  
  138.     {  
  139.         i++;  
  140.     }  
  141.     return left[i]-right[i]!=0?true:false;  
  142. }  
  143. const char * mystring::getdata() const{  
  144.     return data;  
  145. }  
  146. int mystring::getlength() const{//字符串长度  
  147.     return size;  
  148. }  
  149. int _tmain(int argc, _TCHAR* argv[])  
  150. {  
  151.     mystring str(3,'a'); //普通构造函数测试  
  152.     mystring str1(str);  //复制构造函数测试  
  153.     mystring str2("asdf"); //普通构造函数测试  
  154.     mystring str3; //默认构造函数测试  
  155.     cout<<"str:"<<str<<endl;  
  156.     cout<<"str1:"<<str1<<endl;  
  157.     cout<<"str2:"<<str2<<endl;  
  158.     cout<<"str3:"<<str3<<endl;  
  159.     str3 =str2;//赋值函数测试  
  160.     cout<<"str3:"<<str3<<endl;  
  161.     str3="12ab";//赋值函数测试  
  162.     cout<<"str3:"<<str3<<endl;  
  163.     cout<<"str3[2]:"<<str3[2]<<endl; //[]重载函数测试  
  164.     str3+="111"//+=重载函数测试  
  165.     cout<<"str3:"<<str3<<endl;  
  166.     str3+=str1;//+=重载函数测试  
  167.     cout<<"str3:"<<str3<<endl;  
  168.   
  169.     cin>>str1; //>>重载函数测试  
  170.     cout<<"str1:"<<str1<<endl;  
  171.   
  172.   
  173.     mystring t1="1234";  
  174.     mystring t2="1234";  
  175.     mystring t3="12345";  
  176.     mystring t4="12335";  
  177.    cout<<"t1==t2?"<<(t1==t2)<<endl; //==重载函数测试  
  178.    cout<<"t1<t3?"<<(t1<t3)<<endl;//<重载函数测试  
  179.    cout<<"t1>t4?"<<(t1>t4)<<endl; //>重载函数测试  
  180.    cout<<"t1!=t4?"<<(t1!=t4)<<endl;//!=重载函数测试  
  181.     getchar();  
  182.     return 0;  
  183. }