歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++自定義String類

C++自定義String類

日期:2017/3/1 10:23:59   编辑:Linux編程

String.h頭文件:

  1. using namespace std;
  2. class String{
  3. public:
  4. //默認構造函數
  5. String();
  6. //帶參構造函數
  7. String(const char *str);
  8. //析構函數
  9. ~String();
  10. //拷貝構造
  11. String(const String &obj);
  12. //復制初始化
  13. String &operator=(const String &obj);
  14. public:
  15. //把後面字符串加在*this上
  16. String &operator+=(const String &obj);
  17. //把後面字符串加在前面
  18. friend String operator+(const String &obj,const String &obj1);
  19. //==判斷字符串是否相等
  20. bool operator==(const String &str)const;
  21. //!=判斷字符串是否相等
  22. bool operator!=(const String &str)const;
  23. //[]下標運算符重載
  24. char operator[](int index);
  25. //<<運算符重載
  26. friend ostream &operator<<(ostream &os,const String &obj);
  27. //>>運算符重載
  28. friend istream &operator>>(istream &is,const String &obj);
  29. //返回字符串長度
  30. int leng()const;
  31. //取從position所指位置連續取len個字符組成子串返回
  32. String& subString(int position,int len);
  33. private:
  34. char *_str;
  35. int _len;
  36. };

String.cpp實現文件:

  1. #include <iostream>
  2. //#include <string>
  3. #include "String.h"
  4. const int maxLength=128;//字符串的最大長度
  5. //默認構造函數
  6. String::String(){
  7. _len=0;
  8. _str=new char[1];
  9. }
  10. //帶參構造函數
  11. String::String(const char *str){
  12. _str=new char[maxLength+1];
  13. if(!_str)
  14. {
  15. cerr<<"Allocation Error!\n";
  16. exit(1);
  17. }
  18. _len=strlen(str);
  19. strcpy(_str,str);
  20. }
  21. //析構函數
  22. String::~String(){
  23. delete _str;
  24. _str=NULL;
  25. }
  26. //拷貝構造
  27. String::String(const String &obj){
  28. _str=new char[maxLength+1];
  29. if(!_str)
  30. {
  31. cerr<<"Allocation Error!\n";
  32. exit(1);
  33. }
  34. _len=obj._len;
  35. strcpy(_str,obj._str);
  36. }
  37. //復制初始化
  38. String &String::operator=(const String &obj){
  39. _str=new char[maxLength+1];
  40. if(!_str)
  41. {
  42. cerr<<"Allocation Error!\n";
  43. exit(1);
  44. }
  45. _len=obj._len;
  46. strcpy(_str,obj._str);
  47. return *this;
  48. }
  49. //把後面字符串加在前面
  50. String &String::operator+=(const String &obj){
  51. char *temp=_str;//將*this->ch賦給temp
  52. _len=_len+obj._len;//*this->curLen為兩字符串的總長度
  53. _str=new char[_len+1];
  54. if(!_str)
  55. {
  56. cerr<<"Out of memory!\n";
  57. exit(1);
  58. }
  59. strcpy(_str,temp);
  60. strcat(_str,obj._str);
  61. delete[] temp;
  62. return *this;
  63. }
  64. //把後面字符串加在前面
  65. String operator+(const String &obj,const String &obj1){
  66. int len=obj._len+obj1._len;//兩個長度加起來
  67. char *str0=new char[len+1];
  68. if(!str0)
  69. {
  70. cerr<<"Out of memory!\n";
  71. exit(1);
  72. }
  73. strcpy(str0,obj._str);
  74. strcat(str0,obj1._str);
  75. String str(str0);
  76. delete[] str0;
  77. return str;
  78. }
  79. //==判斷字符串是否相等
  80. bool String::operator==(const String &str)const{
  81. if(strcmp(_str, str._str)==0){
  82. return true;
  83. }else{
  84. return false;
  85. }
  86. }
  87. //!=判斷字符串是否相等
  88. bool String::operator!=(const String &str)const{
  89. if(strcmp(_str, str._str)!=0){
  90. return true;
  91. }else{
  92. return false;
  93. }
  94. }
  95. //[]下標運算符重載
  96. char String::operator[](int index){
  97. return _str[index];
  98. }
  99. //<<運算符重載
  100. ostream &operator<<(ostream &os,const String &obj){
  101. os<<obj._str;
  102. return os;
  103. }
  104. //>>運算符重載
  105. istream &operator>>(istream &is,const String &obj){
  106. is>>obj._str;
  107. return is;
  108. }
  109. //返回字符串長度
  110. int String::leng()const{
  111. return _len;
  112. }
  113. //取從position所指位置連續取len個字符組成子串返回
  114. String& String::subString(int position,int len){
  115. if(position<0||position+len-1>=maxLength||len<0) //參數不合理,不取子串
  116. {
  117. _len=0;
  118. _str[0]='\0';
  119. }
  120. else
  121. {
  122. if(position+len-1>=_len) //字符串不夠長
  123. len=_len-position;
  124. _len=len;
  125. for(int i=0,j=position;i<len;i++,j++)
  126. _str[i]=_str[j];
  127. _str[len]='\0';
  128. }
  129. return *this;
  130. }
Copyright © Linux教程網 All Rights Reserved