类String的构造函数、析构函数和赋值函数

1、代码如下:

    注意:形参加上const修饰符,函数体里面判断参数是否为NULL,引用不能为空等。

    复制操作符应该判断是否是自赋值。

    重载输入操作符时,要注意给data分配足够的空间,现在还没有想到太好的办法,用的是临时变量,这里一直不是很明白C++中的(string s; cin>>s;)到底最大可以读取多少个字符,其原理又是怎么实现的呢。

    友元函数有时候编译器会有bug,解决方法如下:事先声明类和友元函数(在类外面不能用friend 关键字)
      class String;
      ostream& operator<<(ostream& out,const String& s);
      stream& operator>>(istream& in,const String& s);
      bool operator<(const String& s1,const String &s2);

  1. <pre class="html" name="code">#include "stdafx.h"  
  2. #include <iostream.h>  
  3. #include <string.h>  
  4.   
  5. class String  
  6. {  
  7. public:  
  8.     String();  
  9.     String(const char *str);  
  10.     String(const String &s);  
  11.     ~String();  
  12.     String& operator=(const String &str);  
  13.     friend ostream& operator<<(ostream& out,const String& s);  
  14.     friend istream& operator>>(istream& in,String& s);  
  15.     friend bool operator<(const String& s1,const String &s2);  
  16.     char& operator[](int pos)  
  17.     {  
  18.         cout<<"index operator"<<endl;  
  19.         int len=strlen(data);  
  20.         if (pos>=len)  
  21.         {  
  22.             return data[len-1];  
  23.         }   
  24.         else  
  25.         {  
  26.             return data[pos];  
  27.         }  
  28.     }  
  29.   
  30. private:  
  31.     char *data;  
  32. };  
  33.   
  34. //默认构造函数  
  35. String::String()  
  36. {  
  37.     cout<<"default constructor"<<endl;  
  38.     data=new char[1];  
  39.     *data='\0';  
  40. }  
  41.   
  42. //带参数构造函数  
  43. String::String(const char *str)  
  44. {  
  45.     cout<<"parameter constructor"<<endl;  
  46.     if (str==NULL)  
  47.     {  
  48.         data=new char[1];  
  49.         *data='\0';  
  50.     }   
  51.     else  
  52.     {  
  53.         int len=strlen(str);  
  54.         data=new char[len+1];  
  55.         strcpy(data,str);  
  56.     }  
  57. }  
  58.   
  59.   
  60. //复制构造函数  
  61. String::String(const String &str)  
  62. {  
  63.     cout<<"copy constructor"<<endl;  
  64.     int len=strlen(str.data);  
  65.     data=new char[len+1];  
  66.     strcpy(data,str.data);  
  67. }  
  68.   
  69. //析构函数  
  70. String::~String()  
  71. {  
  72.     cout<<"destructor"<<endl;  
  73.     delete[] data;  
  74. }  
  75.   
  76. //赋值操作符  
  77. String& String::operator=(const String &str)  
  78. {  
  79.     cout<<"assign operator"<<endl;  
  80.     if (this!=&str)  
  81.     {  
  82.         int len=strlen(str.data);  
  83.         delete[] data;  
  84.         data=new char[len+1];  
  85.         strcpy(data,str.data);  
  86.     }  
  87.       
  88.     return *this;  
  89. }  
  90.   
  91. //输出操作符  
  92. ostream& operator<<(ostream& out,const String& s)  
  93. {  
  94.     cout<<"cout operator"<<endl;  
  95.     out<<s.data<<endl;  
  96.     return out;  
  97. }  
  98.   
  99. //输入操作符  
  100. istream& operator>>(istream& in,String& s)  
  101. {  
  102.     cout<<"cin operator"<<endl;  
  103.     //这个地方很伤神,目前没有想到很好的办法,只能先用中间变量来获取输入的长度,然后释放中间变量  
  104.     char *sTemp=new char[1000];  
  105.     in>>sTemp;  
  106.   
  107.     delete[] s.data;  
  108.     int len=strlen(sTemp);  
  109.     s.data=new char[len+1];  
  110.     strcpy(s.data,sTemp);  
  111.   
  112.     delete[] sTemp;  
  113.     return in;  
  114. }  
  115.   
  116. //比较操作符  
  117. bool operator<(const String& s1,const String &s2)  
  118. {  
  119.     cout<<"compare operator"<<endl;  
  120.     if (strcmp(s1.data,s2.data)<0)  
  121.     {  
  122.         return 1;  
  123.     }  
  124.     return 0;  
  125. }  
  126.   
  127.   
  128.   
  129. void main()  
  130. {  
  131.     String s1;            //default constructor  
  132.     String s2("12345");   //parameter constructor  
  133.     String s3=s2;         //copy constructor  
  134.     s1=s2;                //assign operator  
  135.       
  136.     cin>>s1;              //输入操作符  
  137.     cout<<s1;             //输出操作符  
  138.   
  139.     cout<<(s1<s2)<<endl;  //比较操作符  
  140.   
  141.     cout<<(s1[5])<<endl;  //获取字符  
  142.     
  143.     //destructor destructor destructor  
  144. }</pre>  
  145. <pre></pre>  
  146. <p> </p>  
  147. <pre></pre>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值