string类

[objc]  view plain  copy
 print ?
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <string>  
  4. #include <stdio.h>  
  5.   
  6.   
  7.   
  8. using namespace std;  
  9.   
  10. class String  
  11. {  
  12.         friend bool operator == (const String &,const String &);  
  13.         friend bool operator != (const String &,const String &);  
  14.         friend bool operator <  (const String &,const String &);  
  15.         friend bool operator >  (const String &,const String &);  
  16.         friend bool operator <= (const String &,const String &);  
  17.         friend bool operator >= (const String &,const String &);  
  18.   
  19.         friend ostream &operator <<(ostream &os,const String &s);  
  20.         friend istream &operator >>(istream &os,String &s);  
  21.   
  22.   
  23. public:  
  24.         //无参构造函数  
  25.         String()  
  26.         {  
  27.          pstr_=new char[1];  
  28.         }  
  29.   
  30.         //有参构造函数  
  31.         String (const charchar *s)  
  32.         {  
  33.          pstr_=new char[strlen(s)+1];  
  34.          strcpy(pstr_,s);  
  35.         }  
  36.   
  37.   
  38.         //复制构造函数  
  39.         String (const String &rhs)  
  40.         {  
  41.           pstr_=new char[strlen(rhs.pstr_)+1];  
  42.           strcpy(pstr_,rhs.pstr_);  
  43.         }  
  44.   
  45.   
  46.         //赋值对象重载函数  
  47.         String &operator = (const String &rhs)  
  48.         {  
  49.          if(this!=&rhs)//如果不是自复制  
  50.          {  
  51.           delete []pstr_;//先删除掉原有的内容  
  52.           pstr_=new char[strlen(rhs.pstr_)+1];  
  53.           strcpy(pstr_,rhs.pstr_);  
  54.          }  
  55.          returnreturn *this;  
  56.         }  
  57.   
  58.   
  59.         //赋值字符串重载函数  
  60.         String &operator = (const charchar *s)  
  61.         {  
  62.          pstr_=new char[strlen(s)+1];  
  63.          strcpy(pstr_,s);  
  64.          returnreturn *this;  
  65.         }  
  66.   
  67.   
  68.         //重载+=运算符  左边对象本身发生了改变  
  69.         String & operator +=(const String & rhs)  
  70.         {  
  71.          //用临时的字符串指针存放  
  72.          charchar *tmp;  
  73.          tmp=new char[strlen(pstr_)+strlen(rhs.pstr_)+1];  
  74.          strcpy(tmp,pstr_);  
  75.          strcat(tmp,rhs.pstr_);  
  76.          delete []pstr_;  
  77.          pstr_=tmp;  
  78.          returnreturn *this;  
  79.   
  80.         }  
  81.         String &operator += (const charchar *pstr)  
  82.         {  
  83.          charchar *tmp;  
  84.          tmp = new char[strlen(pstr_)+strlen(pstr)+1];//先开空间  
  85.          strcpy(tmp,pstr_);  
  86.          strcat(tmp,pstr);  
  87.          delete []pstr_;  
  88.          pstr_=tmp;  
  89.          returnreturn *this;  
  90.         }  
  91.   
  92.   
  93.         //重载[]下标运算符  
  94.         char &operator[](size_t index)  
  95.         {  
  96.          static char sNULL='\0';  
  97.          //要判断给出的下标越界的情况  
  98.          if(index<strlen(pstr_) && index>0)  
  99.          {  
  100.           return pstr_[index];  
  101.          }  
  102.          else  
  103.          {  
  104.           cout<<"下标越界"<<endl;  
  105.           return sNULL;  
  106.          }  
  107.         }  
  108.         //重载const对象的运算符  
  109.         const char &operator[](size_t index) const  
  110.         {  
  111.           static char sNULL='\0';  
  112.          //要判断给出的下标越界的情况  
  113.          if(index<strlen(pstr_) && index>0)  
  114.          {  
  115.           return pstr_[index];  
  116.          }  
  117.          else  
  118.          {  
  119.           cout<<"下标越界"<<endl;  
  120.           return sNULL;  
  121.          }  
  122.         }  
  123.   
  124.   
  125.   
  126.   
  127.         //返回字符串长度函数  函数后面加const代表这里面的所有变量只能读不能改  
  128.         size_t size() const  
  129.         {  
  130.           return strlen(pstr_);  
  131.         }  
  132.   
  133.         //重载c_str函数  返回的是当前字符串的首字符地址  
  134.         const char* c_str()const  
  135.         {  
  136.          return pstr_;  
  137.         }  
  138.   
  139.   
  140.   
  141.   
  142.         //析构函数  
  143.         ~String()  
  144.         {  
  145.          delete []pstr_;  
  146.         }  
  147.   
  148.         //打印函数  
  149.         void print()  
  150.         {  
  151.          if(pstr_==NULL)  
  152.          {  
  153.          cout<<""<<endl;  
  154.          }  
  155.         else  
  156.         {  
  157.          cout<<"string :"<<pstr_<<endl;  
  158.         }  
  159.         }  
  160.   
  161. private:  
  162.         charchar *pstr_;  
  163.   
  164. };  
  165. //重载== 函数  
  166. bool operator == (const String &lhs,const String &rhs)  
  167. {  
  168.         if(strcmp(lhs.pstr_,rhs.pstr_)==0)  
  169.         {  
  170.          return 1;  
  171.         }  
  172.         else  
  173.          {  
  174.          return 0;  
  175.          }  
  176. }  
  177. //重载!=函数  
  178. bool operator != (const String &lhs,const String &rhs)  
  179. {  
  180.         if(strcmp(lhs.pstr_,rhs.pstr_)==0)  
  181.         {  
  182.          return 0;  
  183.         }  
  184.         else  
  185.          {  
  186.          return 1;  
  187.          }  
  188. }  
  189. //重载< 函数  
  190. bool operator <(const String &lhs,const String &rhs)  
  191. {  
  192.         if(strcmp(lhs.pstr_,rhs.pstr_)<0)  
  193.         {  
  194.          return 1;  
  195.         }  
  196.         else  
  197.         {  
  198.          return 0;  
  199.         }  
  200. }  
  201. //重载>函数  
  202. bool operator >(const String &lhs,const String &rhs)  
  203. {  
  204.         if(strcmp(lhs.pstr_,rhs.pstr_)>0)  
  205.         {  
  206.          return 1;  
  207.         }  
  208.         else  
  209.         {  
  210.          return 0;  
  211.         }  
  212. }  
  213. //重载<=函数  
  214. bool operator <=(const String &lhs,const String &rhs)  
  215. {  
  216.         if(strcmp(lhs.pstr_,rhs.pstr_)<=0)  
  217.         {  
  218.          return 1;  
  219.         }  
  220.         else  
  221.         {  
  222.          return 0;  
  223.         }  
  224. }  
  225.   
  226. //重载>=函数  
  227. bool operator >=(const String &lhs,const String &rhs)  
  228. {  
  229.    if(strcmp(lhs.pstr_,rhs.pstr_)>=0)  
  230.         {  
  231.          return 1;  
  232.         }  
  233.         else  
  234.         {  
  235.          return 0;  
  236.         }  
  237. }  
  238.   
  239. //重载<<流  
  240. ostream &operator <<(ostream &ofs,const String &rhs)  
  241. {  
  242.         ofs<<rhs.pstr_<<endl;  
  243.         return ofs;  
  244. }  
  245. //重载>>流  
  246. istream &operator >>(istream &ifs, String &rhs)  
  247. {  
  248.   
  249.         char tmp[1024];//先开空间  
  250.         ifs>>tmp;  
  251.         rhs.pstr_=tmp;//输入的字符 放到rhs中  
  252.         return ifs;  
  253.   
  254. }  
  255. //重载+(两个对象之间)  
  256. String operator +(const String &lhs,const String & rhs)  
  257. {  
  258.         String str(lhs);  
  259.         str+=rhs;  
  260.         return str;  
  261. }  
  262. String operator +(const String &lhs,const charchar *s)  
  263. {  
  264.         String str(lhs);  
  265.         str+=s;  
  266.         return str;  
  267. }  
  268. String operator +(const charchar *s,const String &rhs)  
  269. {  
  270.         String str(rhs);  
  271.         str+=s;  
  272.         return s;  
  273.   
  274. }  
  275.   
  276.   
  277. int main()  
  278. {  
  279.   
  280.         String s1;//无参函数  
  281.         s1.print();  
  282.   
  283.         String s2="hello";//有参函数  
  284.         s2.print();  
  285.   
  286.         String s3=s2;//调用复制构造函数  
  287.         s3.print();  
  288.   
  289.         String s4="world";//赋值对象重载函数  
  290.         s4=s2;  
  291.         s4.print();  
  292.   
  293.         String s5//赋值字符串重载函数  
  294.         s5="hello";  
  295.         s5.print();  
  296.   
  297.   
  298.         String s6="hello";//重载+=函数  
  299.         String s7="world";  
  300.         s6+=s7;  
  301.         s6.print();  
  302.   
  303.         String s8="hello";//重载++函数  
  304.         s8+="world";  
  305.         s8.print();  
  306.   
  307.         s8[3]='A';  
  308.         cout<<s8[3]<<endl;//重载[]运算符  
  309.         s8.print();  
  310.   
  311.   
  312.         String s9="hello";//重载c_str函数  
  313.         const char* s;  
  314.         s=s9.c_str();  
  315.         cout<<s<<endl;  
  316.   
  317.   
  318.         String s10="hello";  
  319.         if(s9==s10)       //重载==  
  320.         {  
  321.          cout<<"equal"<<endl;  
  322.         }  
  323.   
  324.   
  325.         String s11="world";  
  326.         if(s10!=s11)     //重载!=  
  327.         {  
  328.          cout<<"unequal"<<endl;  
  329.         }  
  330.   
  331.   
  332.         String s12="i am the max string";//重载<  
  333.   
  334.         if(s11<s12)  
  335.         {  
  336.          cout<<"右边字符串大"<<endl;  
  337.         }  
  338.         else  
  339.         {  
  340.         cout<<"左边字符串大"<<endl;  
  341.         }  
  342.        String s13="hello";//重载<<  
  343.         cout<<s13;  
  344.   
  345.         String s14;//重载>>  
  346.         cin>>s14;  
  347.         cout<<s14;  
  348.   
  349.         String s15="hello";//重载+ 用到了+=  
  350.         String s16="world";  
  351.         cout<<s15+s16<<endl;  
  352.   
  353.   
  354.         return 0;  
  355.   
  356.   
  357. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值