-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathassignment_operator.cpp
More file actions
107 lines (96 loc) · 2.22 KB
/
assignment_operator.cpp
File metadata and controls
107 lines (96 loc) · 2.22 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
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
class CMyString
{
public:
CMyString(char* pData=NULL);
CMyString(const CMyString& str);
CMyString& operator=(const CMyString& str);
~CMyString();
Print();
private:
char* m_pData;
};
CMyString::CMyString(char* pData)
{
if(pData==NULL) {
m_pData = new char[1];
m_pData[0] = '\0';
} else {
int length = strlen(pData);
m_pData = new char[length+1];
strcpy(m_pData, pData);
}
}
CMyString::~CMyString()
{
delete[] m_pData;
}
CMyString::CMyString(const CMyString& str)
{
if(this != &str) {
int length = strlen(str.m_pData);
m_pData = new char[length+1];
strcpy(m_pData, str.m_pData);
}
}
CMyString::Print()
{
if(m_pData==NULL)
cout << "no data" << endl;
else {
char* temp = m_pData;
while(*temp!='\0') {
cout << *temp;
temp++;
}
cout << endl;
}
}
CMyString& CMyString::operator=(const CMyString& str)
{
if(this != &str) {
CMyString strTemp(str);
char* p_Temp = strTemp.m_pData;
strTemp.m_pData = m_pData;
m_pData = p_Temp;
}
return *this;
}
int main(int argc, char** argv)
{
char* pData = "assignment opreator";
CMyString strTest1(pData);
cout << "test1" << endl;
strTest1.Print();
CMyString strTest2(strTest1);
cout << "test2" << endl;
strTest2.Print();
CMyString strTest3, strTest4;
cout << "test3" << endl;
strTest3=strTest4=strTest1;
strTest3.Print();
strTest4.Print();
cout << "test4" << endl;
strTest4 = strTest4;
strTest4.Print();
return 0;
}