1 #include <iostream>
2 #include <string>
3 #include <cstring>
4 using namespace std;
5 class MyString {
6 char * p;
7 public:
8 MyString(const char * s) {
9 if( s) {
10 p = new char[strlen(s) + 1];
11 strcpy(p,s);
12 }
13 else
14 p = NULL;
15
16 }
17 ~MyString() { if(p) delete [] p; }
18 MyString(const MyString& x) {
19 if (x.p) {
20 p = new char[strlen(x.p + 1)];
21 strcpy(p, x.p);
22 }
23 else
24 p = NULL;
25 }
26 MyString() {
27
28 }
29 void Copy(const char* str) {
30 if (str) {
31 p = new char[strlen(str) + 1];
32 strcpy(p, str);
33 }
34 else p = NULL;
35 }
36 MyString& operator =(const MyString& x) {
37 if (p == x.p) return *this;
38 if (p)delete[]p;
39 if (x.p) {
40 p = new char[strlen(x.p) + 1];
41 strcpy(p, x.p);
42 }
43 else p = NULL;
44
45 return *this;
46 }
47
48 friend ostream& operator <<(ostream& os, const MyString& x) {
49 os << x.p;
50 return os;
51 }
52
53 };
54 int main()
55 {
56 char w1[200],w2[100];
57 while( cin >> w1 >> w2) {
58 MyString s1(w1),s2 = s1;
59 MyString s3(NULL);
60 s3.Copy(w1);
61 cout << s1 << "," << s2 << "," << s3 << endl;
62
63 s2 = w2;
64 s3 = s2;
65 s1 = s3;
66 cout << s1 << "," << s2 << "," << s3 << endl;
67
68 }
69 }
1 #include <iostream>
2 #include <string>
3 #include <cstring>
4 using namespace std;
5 class MyString {
6 char * p;
7 public:
8 MyString(const char * s) {
9 if( s) {
10 p = new char[strlen(s) + 1];
11 strcpy(p,s);
12 }
13 else
14 p = NULL;
15
16 }
17 ~MyString() { if(p) delete [] p; }
18 MyString(const MyString& s){
19 if(s.p){
20 p = new char[strlen(s.p) + 1];
21 strcpy(p,s.p);
22 }
23 else
24 p = NULL;
25 }
26 void Copy(const char* s){
27 if( s) {
28 p = new char[strlen(s) + 1];
29 strcpy(p,s);
30 }
31 else
32 p = NULL;
33 }
34 friend ostream& operator<<(ostream& o,const MyString& s){
35 o << s.p;
36 return o;
37 }
38 MyString& operator=(const MyString& s){
39 if(s.p == p) return *this;//!!!很重要,删了后面就没复制的了
40 if(p) delete []p;
41 if(s.p){
42 p = new char[strlen(s.p) + 1];
43 strcpy(p,s.p);
44 }
45 else
46 p = NULL;
47 return *this;
48 }
49 };
50 int main()
51 {
52 char w1[200],w2[100];
53 while( cin >> w1 >> w2) {
54 MyString s1(w1),s2 = s1;
55 MyString s3(NULL);
56 s3.Copy(w1);
57 cout << s1 << "," << s2 << "," << s3 << endl;
58
59 s2 = w2;
60 s3 = s2;
61 s1 = s3;
62 cout << s1 << "," << s2 << "," << s3 << endl;
63
64 }
65 }