【1】String类基本函数如何实现?
示例代码如下:
1 #include<iostream> 2 #include<assert.h> 3 #include<string> 4 using namespace std; 5 6 class String 7 { 8 public: 9 String(const char *str = NULL); // 复合默认构造函数 10 String(const String &other); // 拷贝构造函数 11 String & operator=(const String &other); // 赋值构造函数 12 String operator+(const String &s1); // 双目运算符重载 13 ~String(); // 析构函数 14 void PrintData() const; 15 16 private: 17 char *m_data; //指针成员变量 18 }; 19 20 String::String(const char *str) 21 { 22 cout << "Construction" << endl; 23 if (NULL == str) 24 { 25 m_data = new char[1]; 26 assert(m_data != NULL); 27 *m_data = '\0'; 28 } 29 else 30 { 31 int length = strlen(str); 32 m_data = new char[length + 1]; 33 assert(m_data != NULL); 34 strcpy(m_data, str); 35 } 36 } 37 38 String::String(const String &other) 39 { 40 cout << "Copy Construction" << endl; 41 int length = strlen(other.m_data); 42 m_data = new char[length + 1]; 43 assert(m_data != NULL); 44 strcpy(m_data, other.m_data); 45 } 46 47 String & String::operator=(const String &other) 48 { 49 cout << "= Construction" << endl; 50 if (this == &other) 51 { 52 return *this; 53 } 54 delete []m_data; 55 int length = strlen(other.m_data); 56 m_data = new char[length + 1]; 57 assert(m_data != NULL); 58 strcpy(m_data, other.m_data); 59 return *this; 60 } 61 62 String String::operator+(const String &s1) 63 { 64 cout << "+ operator" << endl; 65 String temp; 66 delete []temp.m_data; 67 temp.m_data = new char[strlen(m_data) + strlen(s1.m_data) + 1]; 68 strcpy(temp.m_data, m_data); 69 strcat(temp.m_data, s1.m_data); 70 return temp; 71 72 } 73 74 void String::PrintData() const 75 { 76 cout << m_data << endl; 77 } 78 79 String::~String() 80 { 81 delete []m_data; 82 } 83 84 void main() 85 { 86 String s1; // 默认 87 s1.PrintData(); 88 89 String s2("Liuyong"); // 构造函数 90 s2.PrintData(); 91 92 String s3 = "abcde"; // 隐式转换 93 s3.PrintData(); 94 95 String s4(s3); // 拷贝构造 96 s4.PrintData(); 97 98 String s5; 99 s5 = s4; // 赋值构造 100 s5.PrintData(); 101 102 String s6 = s2 + s3; //+operator 103 s6.PrintData(); 104 105 system("pause"); 106 } 107 //run out: 108 /* 109 Construction 110 111 Construction 112 Liuyong 113 Construction 114 abcde 115 Copy Construction 116 abcde 117 Construction 118 = Construction 119 abcde 120 + operator 121 Construction 122 Copy Construction 123 Liuyongabcde 124 请按任意键继续. . . 125 */
上面是string类的部分函数实现,具体实现了构造函数,拷贝构造函数,赋值构造函数,析构函数,以及实现了深拷贝的示范。
【2】String类详细实现
1 #include<cassert> 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 6 class String 7 { 8 public: 9 String(); 10 String(int n, char c); 11 String(const char *source); 12 String(const String &s); 13 String & operator=(char *s); 14 String & operator=(const String &s); 15 ~String(); 16 char &operator[](int i); 17 const char &operator[](int i)const; 18 String &operator+=(const String &s); 19 String &operator+=(const char *s); 20 friend ostream & operator<<(ostream &out, String &s); 21 friend istream & operator>>(istream &in, String &s); 22 friend bool operator<(const String &left, const String &right); 23 friend bool operator>(const String &left, const String &right); 24 friend bool operator==(const String &left, const String &right); 25 friend bool operator!=(const String &left, const String &right); 26 int length(); 27 char *GetData(); 28 29 private: 30 int size; 31 char *data; 32 }; 33 34 String::String() 35 { 36 data = new char[1]; 37 assert(data != NULL); 38 *data = '\0'; 39 size = 0; 40 } 41 42 String::String(int n, char c) 43 { 44 data = new char[n + 1]; 45 assert(data != NULL); 46 size = n; 47 char *temp = data; 48 while (n--) 49 { 50 *temp++ = c; 51 } 52 *temp = '\0'; 53 } 54 String::String(const char *source) 55 { 56 if (NULL == source) 57 { 58 data = new char[1]; 59 assert(data != NULL); 60 *data = '\0'; 61 size = 0; 62 } 63 else 64 { 65 size = strlen(source); 66 data = new char[size + 1]; 67 assert(data != NULL); 68 strcpy(data, source); 69 } 70 } 71 String::String(const String &s) 72 { 73 data = new char[s.size + 1]; 74 assert(data != NULL); 75 strcpy(data, s.data); 76 size = s.size; 77 } 78 79 String & String::operator=(char *s) 80 { 81 if (data != NULL) 82 { 83 delete []data; 84 } 85 size = strlen(s); 86 data = new char[size + 1]; 87 assert(data != NULL); 88 strcpy(data, s); 89 return *this; 90 } 91 92 String &String::operator=(const String &s) 93 { 94 if (this == &s) 95 { 96 return *this; 97 } 98 if (data != NULL) 99 { 100 delete []data; 101 } 102 size = strlen(s.data); 103 data = new char[size + 1]; 104 assert(data != NULL); 105 strcpy(data, s.data); 106 return *this; 107 } 108 109 String::~String() 110 { 111 if (data != NULL) 112 { 113 delete []data; 114 data = NULL; 115 size = 0; 116 } 117 } 118 119 char &String::operator[](int i) 120 { 121 return data[i]; 122 } 123 124 const char &String::operator[](int i)const 125 { 126 return data[i]; 127 } 128 129 String &String::operator+=(const String &s) 130 { 131 int len = size + s.size + 1; 132 char *pTemp = data; 133 data = new char[len]; 134 assert(data != NULL); 135 size = len - 1; 136 strcpy(data, pTemp); 137 strcat(data, s.data); 138 delete []pTemp; 139 return *this; 140 } 141 142 String & String::operator+=(const char *s) 143 { 144 if (NULL == s) 145 { 146 return *this; 147 } 148 int len = size + strlen(s) + 1; 149 char *pTemp = data; 150 data = new char[len]; 151 assert(data != NULL); 152 size = len - 1; 153 strcpy(data, pTemp); 154 strcat(data, s); 155 delete []pTemp; 156 return *this; 157 } 158 159 int String::length() 160 { 161 return size; 162 } 163 164 ostream & operator<<(ostream &out, String &s) 165 { 166 for (int i = 0;i < s.length();i++) 167 { 168 out << s[i] << " "; 169 } 170 return out; 171 } 172 173 istream & operator<<(istream &in, String &s) 174 { 175 char p[50]; 176 in.getline(p, 50); 177 s = p; 178 return in; 179 } 180 181 bool operator<(const String &left, const String &right) 182 { 183 int i = 0; 184 while (left[i] == right[i] && left[i] != 0 && right[i] != 0) 185 { 186 i++; 187 } 188 return left[i] - right[i] <0 ? true : false; 189 } 190 191 bool operator>(const String &left, const String &right) 192 { 193 int i = 0; 194 while (left[i] == right[i] && left[i] != 0 && right[i] != 0) 195 { 196 i++; 197 } 198 return left[i] - right[i] > 0 ? true : false; 199 } 200 201 bool operator ==(const String &left, const String &right) 202 { 203 int i = 0; 204 while (left[i] == right[i] && left[i] != 0 && right[i] != 0) 205 { 206 i++; 207 } 208 return (left[i] == right[i]) ? true : false ; 209 } 210 211 bool operator !=(const String &left, const String &right) 212 { 213 int i = 0; 214 while (left[i] == right[i] && left[i] != 0 && right[i] != 0) 215 { 216 i++; 217 } 218 return (left[i] == right[i]) ? false : true; 219 } 220 221 void main() 222 { 223 String str(3,'a'); 224 String str1(str); 225 String str2("asdf"); 226 String str3; 227 228 cout << "str: " << str << endl; 229 cout << "str1: " << str1 << endl; 230 cout << "str2: " << str2 << endl; 231 cout << "str3: " << str3 << endl; 232 233 str3 = str2; 234 cout << "str2: " << str2 << endl; 235 cout << "str3: " << str3 << endl; 236 237 str3 = "12awee"; 238 cout << "str3: " << str3 << endl; 239 240 cout << "str3[2]= " << str3[2] << endl; 241 242 str3 += "1234"; 243 cout << "str3: " << str3 << endl; 244 245 str3 += str1; 246 cout << "str3: " << str3 << endl; 247 248 String t1 = "1234"; 249 String t2 = "1234"; 250 String t3 = "12345"; 251 String t4 = "12335"; 252 253 cout << "t1 == t2 ? " << (t1 == t2) << endl; 254 cout << "t1 < t3 ? " << (t1 < t3) << endl; 255 cout << "t1 > t4 ? " << (t1 > t4) << endl; 256 cout << "t1 != t4 ? " << (t1 != t4) << endl; 257 258 system("pause"); 259 } 260 // run out: 261 /* 262 str: a a a 263 str1: a a a 264 str2: a s d f 265 str3: 266 str2: a s d f 267 str3: a s d f 268 str3: 1 2 a w e e 269 str3[2]= a 270 str3: 1 2 a w e e 1 2 3 4 271 str3: 1 2 a w e e 1 2 3 4 a a a 272 t1 == t2 ? 1 273 t1 < t3 ? 1 274 t1 > t4 ? 1 275 t1 != t4 ? 1 276 请按任意键继续. . . 277 */
Good Good Study,Day Day Up.
顺序 选择 循环 总结