-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork34.cpp
More file actions
171 lines (142 loc) · 2.97 KB
/
Copy pathwork34.cpp
File metadata and controls
171 lines (142 loc) · 2.97 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char *s;
public:
// 默认构造
String()
{
s = new char[1];
s[0] = '\0';
}
// 从C字符串构造
String(const char *str)
{
if (str)
{
s = new char[strlen(str) + 1];
strcpy(s, str);
}
else
{
s = new char[1];
s[0] = '\0';
}
}
// 拷贝构造(深拷贝)
String(const String &other)
{
s = new char[strlen(other.s) + 1];
strcpy(s, other.s);
}
// 析构函数
~String()
{
delete[] s;
}
// 赋值 = char*
String &operator=(const char *str)
{
delete[] s;
s = new char[strlen(str) + 1];
strcpy(s, str);
return *this;
}
// 赋值 = String
String &operator=(const String &other)
{
if (this != &other)
{
delete[] s;
s = new char[strlen(other.s) + 1];
strcpy(s, other.s);
}
return *this;
}
// +
String operator+(const char *str)
{
String temp;
delete[] temp.s;
temp.s = new char[strlen(s) + strlen(str) + 1];
strcpy(temp.s, s);
strcat(temp.s, str);
return temp;
}
String operator+(const String &other)
{
return *this + other.s;
}
// +=
String &operator+=(const char *str)
{
char *temp = new char[strlen(s) + strlen(str) + 1];
strcpy(temp, s);
strcat(temp, str);
delete[] s;
s = temp;
return *this;
}
String &operator+=(const String &other)
{
return (*this += other.s);
}
// 输入
friend istream &operator>>(istream &in, String &str)
{
char buffer[1000];
in >> buffer;
delete[] str.s;
str.s = new char[strlen(buffer) + 1];
strcpy(str.s, buffer);
return in;
}
// 输出
friend ostream &operator<<(ostream &out, const String &str)
{
out << str.s;
return out;
}
// ==
friend bool operator==(const String &a, const char *b)
{
return strcmp(a.s, b) == 0;
}
friend bool operator==(const String &a, const String &b)
{
return strcmp(a.s, b.s) == 0;
}
// !=
friend bool operator!=(const String &a, const char *b)
{
return !(a == b);
}
friend bool operator!=(const String &a, const String &b)
{
return !(a == b);
}
};
int main()
{
String s;
s += "hello";
cout << s << endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n"
<< s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}