Implementation of several basic functions of string # include <iostream> #include <assert.h> #include <string.h>using namespace std; Class String{public:string () {_str = new char[1];_str[0] = ' + ';} String (char *str) {assert (str! = NULL); _str = new Char[strlen (str) + 1];strcpy (_str, str);} String (const string& s) {_str = new Char[strlen (S._STR) + 1];strcpy (_str, s._str);} string& operator= (const string& s) {if (this = &s) {delete[] _str;_str = new Char[strlen (S._STR) + 1];strcpy (_s TR, s._str);} return *this;} ~string () {delete[] _str;} Public:void Getstr () {cout << _str << Endl;} Private:char *_str;}; int main () {String s;s.getstr (); String S1 ("123"); S1.getstr (); String s2 = S1;s2.getstr (); s = S1;s.getstr (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several basic functions of the Simple String class "C + +"