http://wenku.baidu.com/view/d7ac113243323968011c925b.html
已知类String的原型为:
class String{ public: String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operator =(const String &other);// 赋值函数 private: char *m_data; // 用于保存字符串 };
请编写String的上述4个函数。
1 /* 2 ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30 3 new不忘delete 4 Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString): 5 6 MyString();//构造 7 MyString(const char* cString);//地址不能变 8 char at(int index) const;//输入数组下标,返回字符 9 int length() const;//长度 10 void clear();//清空 len=0 11 bool empty() const;//是否清空?len不变 12 int compare(const MyString& s) const;// 13 int compare(int index, int n, const MyString& s) const; 14 void copy(char s[], int index, int n); 15 char* data() const; 16 int find(char ch) const; 17 int find(char ch, int index) const; 18 int find(const MyString& s, int index) const; 19 20 */ 21 22 #include<iostream> 23 #include<string.h> 24 using namespace std; 25 class MyString 26 { 27 public: 28 MyString(void):a(NULL){cout << "MyString()" << endl;} 29 MyString(const char* cString=NULL);//ordinary func 30 MyString(const MyString &other);//copy func 31 MyString & operator =(const MyString &other);//assign func 32 //~MyString(void); 33 char at(int index) const;// 34 int length() const; 35 void clear(); 36 bool empty() const; 37 int compare(const MyString& s) const; 38 int compare(int index, int n, const MyString& s) const; 39 void copy(char s[], int index, int n); 40 char* data() const; 41 int find(char ch) const; 42 int find(char ch, int index) const; 43 int find(const MyString& s, int index) const; 44 45 // MyString &operator+=(const MyString &other); 46 //MyString operator +(const MyString &mystr); 47 //MyString &operator +(const MyString &mystr); 48 //重载为友元函数,重载"+"运算符(不改变被加对象) 注意返回引用不行 49 friend MyString operator +(const MyString& mystr1,const MyString &mystr2); 50 MyString &operator+=(const MyString &other); 51 52 bool operator > (const MyString & another); 53 bool operator < (const MyString & another); 54 bool operator == (const MyString & another); 55 56 char& operator[](int idx); 57 58 void dis(); 59 friend ostream& operator<<(ostream& os,const MyString& other); 60 61 ~MyString(); 62 63 private: 64 char *a; 65 int len; 66 }; 67 68 69 //MyString::MyString(){ 70 // cout << "MyString()" << endl; 71 //} 72 MyString::~MyString(){ 73 cout<<"~MyString():delete..."<<endl; 74 delete []a; 75 } 76 77 MyString::MyString(const char* cString) 78 { 79 if (cString==NULL) 80 { 81 a=new char[1]; 82 a[0]=0; 83 //int len=0; 84 } 85 else 86 { 87 len = strlen(cString);//new 88 a=new char [len+1]; 89 strcpy(a,cString); 90 //len=strlen(cString); 91 } 92 } 93 94 MyString::MyString(const MyString &other){ 95 int len = strlen(other.a); 96 a = new char[len+1]; 97 strcpy(a, other.a); 98 } 99 100 MyString &MyString::operator =(const MyString &other){ 101 //self check assign 102 if(this == &other){ 103 return *this; 104 } 105 //释放原有的内存资源 106 delete []a; 107 //分配新的内存资源,并复制内容 108 int len = strlen(other.a); 109 a = new char[len+1]; 110 strcpy(a, other.a); 111 //返回本对象的引用 112 return *this; 113 } 114 115 char MyString::at(int index) const 116 { 117 if(len==0) 118 {cout<<"no char in string"<<endl; 119 return 0; 120 } 121 if(index>len) 122 { 123 cout<<"the maximun array exceed"<<endl; 124 return 0 ; 125 } 126 else 127 { 128 129 return a[index-1]; 130 } 131 } 132 133 int MyString::length() const 134 { 135 return len; 136 } 137 138 void MyString::clear() 139 { 140 if(!a) 141 { 142 delete []a; 143 a=NULL; 144 } 145 146 len=0; 147 //a=" "; 148 } 149 bool MyString::empty() const 150 { 151 if(len==0) 152 return true; 153 else 154 return false; 155 } 156 157 158 int MyString::compare(const MyString& s) const 159 { 160 int m=this->len;int n=s.len; 161 162 for(int i=0;i<m&&i<n;i++) 163 { 164 if(s.a[i]==a[i]) 165 continue; 166 else if(s.a[i]<a[i]) 167 return 1; 168 else 169 return -1; 170 } 171 return 0; 172 173 174 } 175 int MyString::compare(int index, int n, const MyString& s) const 176 { 177 int m=len,k=s.len; 178 if(index+n>m||index+n>k) 179 { 180 cout<<"cannot compare!"<<endl; 181 ///I dont know how to solve it 182 } 183 for(int i=index-1,j=0;i<n+index;i++,j++) 184 { 185 if(s.a[j]==a[i]) 186 continue; 187 else if(s.a[j]<a[i]) 188 return 1; 189 else 190 return -1; 191 } 192 return 0; 193 } 194 195 196 void MyString::copy(char s[], int index, int n) 197 { 198 if(n>=len) 199 {cout<<"the maximun array exceed"<<endl;} 200 else if(n+index-1>len) 201 { 202 cout<<"the maximun array exceed"<<endl; 203 return; 204 } 205 else 206 { 207 for(int i=n-1,j=0;i<n+index-1;i++,j++) 208 s[j]=a[i]; 209 } 210 211 } 212 char* MyString::data() const 213 { 214 return a; 215 } 216 217 218 219 220 int MyString::find(char ch) const 221 { 222 223 for(int i=0;i<len;i++) 224 { 225 if(ch==a[i]) 226 return i+1; 227 } 228 229 return 0; 230 } 231 int MyString::find(char ch, int index) const 232 { 233 if(index>len||index<1) 234 { 235 cout<<"the index num is should be 1~ "<<len<<endl; 236 return 0; 237 } 238 for(int i=index-1;i<len;i++) 239 { 240 if(ch==a[i]) 241 return i+1; 242 } 243 return -1; 244 245 } 246 247 int MyString::find(const MyString& s, int index) const 248 { 249 if(index>s.len||index<1) 250 { 251 cout<<"the index num is should be 1~ "<<s.len<<endl; 252 return 0; 253 } 254 char ch=s.a[index-1]; 255 256 for(int i=0;i<len;i++) 257 { 258 if(ch==a[i]) 259 return i+1; 260 } 261 262 return 0; 263 } 264 265 // 加法运算符重载 266 //MyString MyString::operator +(const MyString & another){ 267 // int len = strlen(this->a) + strlen(another.a); 268 // MyString str(""); 269 270 // delete []str.a; 271 // str.a = new char[len + 1]; 272 // memset(str.a,0,len + 1); 273 // //int len1 = strlen(this->a) + 1; 274 // //strcat_s(str.a, len1, this->a); 275 // strcat(str.a, this->a); 276 // // 源串长度 + 目标串长度 + 结束符 277 // //int len2 = strlen(this->a) + strlen(another.a) + 1; 278 // //strcat_s(str.a,len2, another.a); 279 // strcat(str.a, another.a); 280 // return str; 281 //} 282 283 //MyString MyString::operator +(const MyString &mystr){//mem leak 284 // MyString newString(""); 285 // if (!mystr.a) 286 // newString = *this; 287 // else if (!a) 288 // newString = mystr; 289 // else { 290 // newString.a = new char[strlen(a) + strlen(mystr.a) + 1]; 291 // strcpy(newString.a, a); 292 // strcat(newString.a, mystr.a); 293 // } 294 // return newString; 295 //} 296 297 //重载"+"运算符(不改变被加对象) 298 //MyString &MyString::operator +(const MyString &mystr){ 299 // MyString *temp=new MyString(""); 300 // temp->a=new char[strlen(a)+strlen(mystr.a)+1]; 301 // strcpy(temp->a,a); 302 // strcat(temp->a,mystr.a); 303 // return *temp; 304 //} 305 306 307 /* 308 //重载"+"运算符(改变被加对象) 309 Mystring &operator +(Mystring &mystr){ 310 char *temp=str; 311 str=new char[strlen(temp)+strlen(mystr.str)+1]; 312 strcpy(str,temp); 313 strcat(str,mystr.str); 314 delete [] temp; 315 return *this; 316 } 317 */ 318 //friend 319 MyString operator +(const MyString& mystr1,const MyString &mystr2){ 320 MyString str(""); 321 delete[] str.a; 322 str.len = strlen(mystr1.a)+strlen(mystr2.a); 323 str.a = new char[str.len + 1]; 324 strcpy(str.a, mystr1.a); 325 strcat(str.a, mystr2.a); 326 return str; 327 328 // //MyString *temp=new MyString("");//mem leak 329 // MyString temp(""); 330 // temp.a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1]; 331 // //temp->a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1]; 332 // strcpy(temp.a,mystr1.a); 333 // strcat(temp.a,mystr2.a); 334 // return temp; 335 } 336 337 // ==关系运算符重载 338 bool MyString::operator==(const MyString &other) 339 { 340 if(strcmp(this->a,other.a) == 0) 341 return true; 342 else 343 return false; 344 } 345 // >关系运算符重载 346 bool MyString::operator>(const MyString &other) 347 { 348 if(strcmp(this->a,other.a) > 0) 349 return true; 350 else 351 return false; 352 } 353 // <运算符重载 354 bool MyString::operator<(const MyString &other) 355 { 356 if(strcmp(this->a,other.a) < 0) 357 return true; 358 else 359 return false; 360 } 361 // []运算符重载 362 char& MyString::operator[](int idx) 363 { 364 return a[idx]; 365 } 366 367 ostream& operator<<(ostream& os,const MyString& other){ 368 return os << other.a; 369 } 370 371 MyString &MyString::operator+=(const MyString &other) 372 { 373 int len = strlen(a) + strlen(other.a) + 1; 374 char *newstr = new char[len]; 375 memset(newstr, 0, len); 376 strcpy(newstr, a); 377 strcat(newstr, other.a); 378 379 delete[] a; 380 381 a = newstr; 382 return *this; 383 } 384 385 int main(){ 386 MyString ms="hello"; 387 388 MyString mt("world"); 389 mt += ms; 390 cout << mt << endl; 391 return 0; 392 }