乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      類String的構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)

       jiucool 2013-04-19

      1、代碼如下:

          注意:形參加上const修飾符,函數(shù)體里面判斷參數(shù)是否為NULL,引用不能為空等。

          復制操作符應該判斷是否是自賦值。

          重載輸入操作符時,要注意給data分配足夠的空間,現(xiàn)在還沒有想到太好的辦法,用的是臨時變量,這里一直不是很明白C++中的(string s; cin>>s;)到底最大可以讀取多少個字符,其原理又是怎么實現(xiàn)的呢。

          友元函數(shù)有時候編譯器會有bug,解決方法如下:事先聲明類和友元函數(shù)(在類外面不能用friend 關(guān)鍵字)
            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. #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. //默認構(gòu)造函數(shù)  
      35. String::String()  
      36. {  
      37.     cout<<"default constructor"<<endl;  
      38.     data=new char[1];  
      39.     *data='\0';  
      40. }  
      41.   
      42. //帶參數(shù)構(gòu)造函數(shù)  
      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. //復制構(gòu)造函數(shù)  
      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. //析構(gòu)函數(shù)  
      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. }  

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多