自定义的String类

一、简介

       C++标准库中包含一个string类,提供了一套封装好的数据以及处理这些数据的函数。为了熟悉这个类的内存管理情况,实现一下自定义的String类,涉及构造函数、复制构造函数、析构函数、重载运算符的操作,主要关注其内部指针的内存的重分配。

二、详解

1、代码实现:

(1)代码String.h:

[html]  view plain  copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. //自定义String类,系统是string类  
  5. class String  
  6. {  
  7.     public:  
  8.         String();                              //默认构造函数,用于创建空字符串  
  9.         ~String();                             //析构函数  
  10.         String(const char*const cstr);           //构造函数,带一个参数用来初始化字符串  
  11.         String(const String &rs);              //复制构造函数,默认是浅层复制,需重载  
  12.         char& operator[](unsigned int length); //重载下标运算符[]  
  13.     char operator[](unsigned int length)const; //重载下标运算符[](const版本)    
  14.         String &operator=(const String &rs);     //重载复制运算符=,用于两个字符串之间的赋值  
  15.     String operator+(const String &rs);    //重载加法运算符+  
  16.     String operator+=(const String &rs);     //重载组合运算符+=  
  17.         friend ostream &operator<<(ostream &output, const String &str); //重载输出流运算符<<  
  18.         friend istream &operator>>(istream &input, String &str);        //重载输入流运算符>>  
  19.         friend bool operator<(const String&str1,const String &str2);    //重载小于运算符<  
  20.         friend bool operator>(const String&str1,const String &str2);    //重载大于运算符>  
  21.         friend bool operator==(const String&str1,const String &str2);   //重载等于运算符==  
  22.   
  23.         unsigned int getlen()const;            //获取字符串长度  
  24.     const char*getstr()const;              //获取字符串  
  25.       
  26.     //int operator++(){cout<<"++i\n";tmp++;return tmp;}    //int的重载自加运算符  
  27.     //int operator++(int){cout<<"i++\n";int tmp=rs;rs++;return tmp;}  
  28.     protected:  
  29.     private:  
  30.         unsigned int len;  
  31.     char *str;  
  32. };  

(2)代码String.cpp:

[html]  view plain  copy
  1. #include <string.h>  
  2. #include "String.h"  
  3. #include <stdio.h>  
  4. //默认构造函数,用于创建空字符串  
  5. String::String()  
  6. {  
  7.     cout<<"---String constructor---"<<endl;  
  8.     len = 0;  
  9.     str = new char[1];  
  10.     str[0] = '\0';  
  11. }  
  12. //析构函数  
  13. String::~String()  
  14. {  
  15.       //cout<<"---String destructor---"<<endl;  
  16.     delete []str;  
  17.     len=0;  
  18. }  
  19. //构造函数,带一个参数用来初始化字符串  
  20. String::String(const char*const cstr)  
  21. {  
  22.     /*len = strlen(cstr);  
  23.     str = new char[len+1];  
  24.     for (unsigned int i =0; i < len; i++) {  
  25.       str[i] = cstr[i];  
  26.     }  
  27.     str[len]='\0';*/  
  28.     cout<<"---String constructor:char*---"<<endl;  
  29.     if (cstr == NULL) {  
  30.         len = 0;  
  31.     str = new char[1];  
  32.     memset(str, 0, len+1);  
  33.     if (str == NULL) return;  
  34.   }  
  35.   else {  
  36.      len = strlen(cstr);  
  37.      str = new char[len + 1];  
  38.      memset(str, 0, len+1);  
  39.      if (str == NULL) return;  
  40.      strncpy(str, cstr, len);  
  41.    }  
  42. }  
  43. //复制构造函数,默认是浅层复制,需重载  
  44. String::String(const String &rs)  
  45. {  
  46.     cout<<"---String copy constructor---"<<endl;  
  47.     len = rs.getlen();  
  48.   str = new char[len + 1];  
  49.   for(unsigned int i = 0 ;i < len; i++) {  
  50.     str[i] = rs.str[i];  
  51.   }  
  52.   str[len] = '\0';  
  53.   /*len = rs.getlen();  
  54.   str = new char[len+1];  
  55.   if (str == NULL) return;  
  56.   strcpy(str, rs.str);*/  
  57. }  
  58. //重载下标运算符[]  
  59. char&String::operator[](unsigned int length)  
  60. {  
  61.   if(length>len) return str[len-1];  
  62.   else return str[length];  
  63. }  
  64. //重载下标运算符[](const版本)  
  65. char String::operator[](unsigned int length)const  
  66. {  
  67.     //cout<<"String::operator[] const"<<endl;  
  68.   if(length>len) return str[len-1];  
  69.   else return str[length];  
  70. }  
  71. //重载复制运算符=,用于两个字符串之间的赋值  
  72. String &String::operator=(const String &rs)  
  73. {  
  74.     cout<<"String::operator="<<endl;  
  75.     if (this == &rs) return *this;  
  76.   delete []str;  
  77.   len = rs.getlen();  
  78.   str = new char[len + 1];  
  79.   for(int i = 0; i < len; i++) {  
  80.     str[i] = rs[i];     //重载下标运算符[]才可使用  
  81.     //str[i] = rs.str[i];  
  82.   }  
  83.   str[len] = '\0';  
  84.   return *this;  
  85. }  
  86. //重载加法运算符+  
  87. String String::operator+(const String &rs)  
  88. {  
  89.     /*char *stnew char[len + rs.getlen() + 1];  
  90.   memset(st, 0, len + rs.getlen() + 1);  
  91.   strcat(st, str);  
  92.   strcat(st, rs.str);  
  93.   return String(st);*/  
  94.   cout<<"String::operator+"<<endl;   
  95.   unsigned int total = len + rs.getlen();  
  96.   char *tmpstr = new char[total + 1];  
  97.   unsigned int i, j;  
  98.   for (i = 0; i < len; i++) {  
  99.     tmpstr[i] = str[i];  
  100.   }  
  101.   for( j = 0; j < rs.getlen(); j++, i++) {  
  102.     tmpstr[i] = rs[j];  
  103.   }  
  104.   tmpstr[total] ='\0';  
  105.   String st(tmpstr);  
  106.   delete tmpstr;  
  107.   tmpstr = NULL;  
  108.   return st;   
  109. }  
  110. //重载组合运算符+=  
  111. String String::operator+=(const String &rs)  
  112. {  
  113.   cout<<"String::operator+="<<endl;   
  114.     int total = len + rs.getlen();  
  115.   //delete []str;  
  116.   char *tmpstr = new char[total + 1];     
  117.   unsigned int i,j;  
  118.   for(i = 0; i < len; i++) {  
  119.     tmpstr[i] = str[i];  
  120.   }  
  121.   for(j = 0; j < rs.getlen(); j++,i++) {  
  122.     tmpstr[i] = rs[j];  
  123.   }  
  124.   tmpstr[total]='\0';  
  125.   delete []str;  
  126.   str = new char[total + 1];  
  127.   strncpy(str, tmpstr, total + 1);  
  128.   return *this;  
  129.   /*  
  130.   char *temp = str;    
  131.   len = len + rs.getlen();   
  132.   str = new char[len + 1];    
  133.   strcpy(str, temp);    
  134.   strcat(_str, rs.getstr());    
  135.   delete[] temp;    
  136.   return *this;*/  
  137. }  
  138. //重载输出流运算符<<  
  139. ostream &operator<<(ostream &output, const String &str)  
  140. {  
  141.     output<<str.str;  
  142.   return output;  
  143. }  
  144. //重载输入流运算符>>  
  145. istream &operator>>(istream &input, String &str)  
  146. {  
  147.     input>>str.str;  
  148.   return input;  
  149. }  
  150. //重载小于运算符<  
  151. bool operator<(const String&str1,const String &str2)  
  152. {  
  153.     if(strcmp(str1.str, str2.str) < 0) return 1;  
  154.   else return 0;  
  155. }  
  156. //重载大于运算符>  
  157. bool operator>(const String&str1,const String &str2)  
  158. {  
  159.     if(strcmp(str1.str, str2.str) > 0) return 1;  
  160.   else return 0;  
  161. }  
  162. //重载等于运算符==  
  163. bool operator==(const String&str1,const String &str2)  
  164. {  
  165.     if(strcmp(str1.str, str2.str) == 0) return 1;  
  166.   else return 0;  
  167. }  
  168. //获取字符串长度  
  169. unsigned int String::getlen()const  
  170. {  
  171.     return len;  
  172. }  
  173. //获取字符串  
  174. const char*String::getstr()const  
  175. {  
  176.     return str;  
  177. }  

(3)代码main.cpp:
[html]  view plain  copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include "String.h"  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     String str1;                  //默认构造函数  
  9.     String str2(NULL);            //带参构造函数  
  10.     String str3("helloworld");    //带参构造函数  
  11.     cout<<"str3="<<str3.getstr()<<endl;  
  12.       
  13.     String str4(str3);            //复制构造函数  
  14.     //String str4 = str3;         //复制构造函数  
  15.     cout<<"str4="<<str4.getstr()<<endl;   
  16.       
  17.       
  18.     const String str5("tai");     //重载下标运算符[](const版本)   
  19.     cout<<"str5[0]="<<str5[0]<<endl;  
  20.       
  21.     String str6;  
  22.     str6 = str3;                  //重载复制运算符=  
  23.     cout<<"str6="<<str6.getstr()<<endl;  
  24.   
  25.     String str7 = str3 + str5;    //重载加法运算符+  
  26.     cout<<"str7="<<str7.getstr()<<endl;  
  27.     
  28.     String str8("123");  
  29.     str8 += str7;                 //重载组合运算符+=  
  30.     cout<<"str8="<<str8.getstr()<<endl;  
  31.       
  32.     String str9;  
  33.     cin>>str9;                    //重载输入流运算符>>  
  34.     cout<<"str9="<<str9<<endl;    //重载输出流运算符<<  
  35.       
  36.     String str10("compare String");  
  37.     String str11("compare string");  
  38.     int comp = str10 > str11;  
  39.     cout<< "str10大于str11:" << comp <<endl;      
  40.     return 0;  
  41. }  
  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值