-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.cc
72 lines (63 loc) · 1.09 KB
/
string.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
///
/// @file string.cc
/// @author boluo([email protected])
/// @date 2016-06-09 21:35:18
///
#include <iostream>
#include <string.h>
using namespace std;
class String
{
public:
String()
{
_pstr = new char[1];
cout << "String()" << endl;
}
String(const char *pstr)
{
_pstr = new char[strlen(pstr)+1];
strcpy(_pstr,pstr);
cout << "String(const char *)" << endl;
}
String(const String &rhs)
{
_pstr = new char[strlen(rhs._pstr)+1];
strcpy(_pstr,rhs._pstr);
cout << "String(const String &)" << endl;
}
String & operator = (const String & rhs)
{
if(this == &rhs)
return *this;
delete [] _pstr;
_pstr = new char[strlen(rhs._pstr)+1];
strcpy(_pstr,rhs._pstr);
cout << "String &operator=(const String&)" << endl;
}
~String()
{
delete [] _pstr;
cout << "~String()" << endl;
}
void print()
{
cout << _pstr << endl;
}
private:
char *_pstr;
};
int main()
{
String str1;
str1.print();
String str2 = "hello,world";
String str3 = "wangdao";
str2.print();
str3.print();
str2 = str3;
str2.print();
String str4 = str3;
str4.print();
return 0;
}