C++ String类的实现

来源:互联网 发布:数据标准 编辑:程序博客网 时间:2024/05/16 07:05

面试的时候被问及了String类的实现,结果没写好... 
就当是重新复习一下吧。 
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数。

 

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<iomanip>  
  3. using namespace std;  
  4.   
  5. class String{  
  6.     friend ostream& operator<< (ostream&,String&);//重载<<运算符  
  7.     friend istream& operator>> (istream&,String&);//重载>>运算符  
  8. public:  
  9.     String(const char* str=NULL);                //赋值构造兼默认构造函数(char)  
  10.     String(const String &other);                 //赋值构造函数(String)  
  11.     String& operator=(const String& other);       //operator=  
  12.     String operator+(const String &other)const;  //operator+  
  13.     bool operator==(const String&);              //operator==  
  14.     char& operator[](unsigned int);              //operator[]  
  15.     size_t size(){return strlen(m_data);};  
  16.     ~String(void) {delete[] m_data;}  
  17. private:  
  18.     char *m_data; // 用于保存字符串  
  19. };  
  20.   
  21. inline String::String(const char* str)     
  22. {  
  23.     if(!str)m_data=0;      //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用  
  24.     else {  
  25.         m_data=new char[strlen(str)+1];  
  26.         strcpy(m_data,str);  
  27.     }  
  28. }  
  29.   
  30. inline String::String(const String &other)  
  31. {  
  32.     if(!other.m_data)m_data=0;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)  
  33.     else   
  34.     {  
  35.         m_data=new char[strlen(other.m_data)+1];  
  36.         strcpy(m_data,other.m_data);  
  37.     }  
  38. }  
  39.   
  40. inline String& String::operator=(const String& other)  
  41. {  
  42.     if (this!=&other)  
  43.     {  
  44.         delete[] m_data;  
  45.         if(!other.m_data) m_data=0;  
  46.         else  
  47.         {  
  48.             m_data = new char[strlen(other.m_data)+1];  
  49.             strcpy(m_data,other.m_data);  
  50.         }  
  51.     }  
  52.     return *this;  
  53. }  
  54. inline String String::operator+(const String &other)const  
  55. {  
  56.     String newString;  
  57.     if(!other.m_data)  
  58.         newString = *this;  
  59.     else if(!m_data)  
  60.         newString = other;  
  61.     else  
  62.     {  
  63.         newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];  
  64.         strcpy(newString.m_data,m_data);  
  65.         strcat(newString.m_data,other.m_data);  
  66.     }  
  67.     return newString;  
  68. }  
  69.   
  70. inline bool String::operator==(const String &s)      
  71. {  
  72.     if ( strlen(s.m_data) != strlen(m_data) )  
  73.         return false;  
  74.     return strcmp(m_data,s.m_data)?false:true;  
  75. }  
  76.   
  77. inline char& String::operator[](unsigned int e)  
  78. {  
  79.     if (e>=0&&e<=strlen(m_data))  
  80.         return m_data[e];  
  81. }  
  82.   
  83. ostream& operator<<(ostream& os,String& str)  
  84. {  
  85.     os << str.m_data;  
  86.     return os;  
  87. }  
  88.   
  89. istream &operator>>( istream &input, String &s )  
  90. {  
  91.    char temp[ 255 ]; //用于存储输入流  
  92.    input>>setw(255)>>temp;  
  93.    s = temp; //使用赋值运算符  
  94.    return input; //使用return可以支持连续使用>>运算符  
  95. }  
  96.   
  97. int main()  
  98. {  
  99.     String str1="Aha!";  
  100.     String str2="My friend";  
  101.     String str3 = str1+str2;  
  102.     cout<<str3<<"/n"<<str3.size()<<endl;  
  103.     return 0;  
  104. }