-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo19.cpp
55 lines (48 loc) · 951 Bytes
/
demo19.cpp
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
#include <iostream>
#include <cstring>
using namespace std;
class String
{
public:
// String(char const *chars = "");
String(const char *chars = "");
void print();
// String& operator = (const String &);
String& operator = (String const &);
String& operator = (char const *);
String& operator = (char);
private:
char *ptrChars;
};
//String::String(char const *chars)
String::String(const char *chars)
{
chars = chars ? chars : "";
ptrChars = new char[strlen(chars) + 1];
strcpy(ptrChars, chars);
}
//String& String::operator = (const String &tg)
String& String::operator = (String const &tg)
{
if(strlen(ptrChars) != strlen(tg.ptrChars))
{
char *tmp = new char[strlen(tg.ptrChars) + 1];
delete[] ptrChars;
ptrChars = tmp;
}
strcpy(ptrChars, tg.ptrChars);
return *this;
}
void String::print()
{
cout << ptrChars << endl;
}
int main()
{
String s("hello");
s.print();
String s2("Dog");
s = s2;
s.print();
return 0;
}