forked from bbw7561135/Numerical-Algorithms-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_10.cpp
More file actions
282 lines (164 loc) · 3.62 KB
/
class_10.cpp
File metadata and controls
282 lines (164 loc) · 3.62 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
using namespace std;
//c++编译器至少给一个类添加4个函数
//默认构造函数(无参,函数体为空)
//默认析构函数(无参,函数体为空)
//默认拷贝构造函数,对属性进行值拷贝
//赋值运算符 operator=, 对属性进行值拷贝
//如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
class Person
{
public:
Person(int age)
{
//将年龄数据开辟到堆区
m_Age = new int(age); //set initial value as age
}
//重载赋值运算符
Person& operator=(Person &p)
{
if (m_Age!= NULL)
{
delete m_Age;
m_Age = NULL;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题
m_Age = new int(*p.m_Age);//set *p.m_Age as init value
//返回自身
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//年龄的指针
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main()
{
test01();
//int a = 10;
//int b = 20;
//int c = 30;
//c = b = a;
//cout << "a = " << a << endl;
//cout << "b = " << b << endl;
//cout << "c = " << c << endl;
return 0;
}
//重载关系运算符,可以让两个自定义类型对象进行对比操作
//class Person
//{
//public:
// Person(string name, int age)
// {
// this->m_Name = name;
// this->m_Age = age;
// };
// bool operator==(Person & p)
// {
// if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
// {
// return true;
// }
// else
// {
// return false;
// }
// }
// bool operator!=(Person & p)
// {
// if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
// {
// return false;
// }
// else
// {
// return true;
// }
// }
// string m_Name;
// int m_Age;
//};
//void test01()
//{
// Person a("孙悟空", 18);
// Person b("孙悟空", 18);
// if (a == b)
// {
// cout << "a和b相等" << endl;
// }
// else
// {
// cout << "a和b不相等" << endl;
// }
// if (a != b)
// {
// cout << "a和b不相等" << endl;
// }
// else
// {
// cout << "a和b相等" << endl;
// }
//}
//int main()
//{
// test01();
// return 0;
//}
// 函数调用运算符重载
//函数调用运算符 () 也可以重载
//由于重载后使用的方式非常像函数的调用,因此称为仿函数
//仿函数没有固定写法,非常灵活
//class MyPrint
//{
//public:
// void operator()(string text)
// {
// cout << text << endl;
// }
//};
//void test01()
//{
// //重载的()操作符 也称为仿函数
// MyPrint myFunc;
// myFunc("hello world");
//}
//class MyAdd
//{
//public:
// int operator()(int v1, int v2)
// {
// return v1 + v2;
// }
//};
//void test02()
//{
// MyAdd add;
// int ret = add(10, 10);
// cout << "ret = " << ret << endl;
// //匿名对象调用
// cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
//}
//int main()
//{
// test01();
// test02();
// return 0;
//}