forked from bbw7561135/Numerical-Algorithms-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_03.cpp
More file actions
179 lines (121 loc) · 3.46 KB
/
class_03.cpp
File metadata and controls
179 lines (121 loc) · 3.46 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
#include <iostream>
using namespace std;
//默认情况下,c++编译器至少给一个类添加3个函数
//1.默认构造函数(无参,函数体为空)
//2.默认析构函数(无参,函数体为空)
//3.默认拷贝构造函数,对属性进行值拷贝
//构造函数调用规则如下:
//如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
//如果用户定义拷贝构造函数,c++不会再提供其他构造函数
class Person
{
public:
//无参(默认)构造函数
Person()
{
cout << "无参构造函数!" << endl;
}
//有参构造函数
Person(int a)
{
age = a;
cout << "有参构造函数!" << endl;
}
//拷贝构造函数
Person(const Person& p)
{
age = p.age;
cout << "拷贝构造函数!" << endl;
}
//析构函数
~Person()
{
cout << "析构函数!" << endl;
}
public:
int age;
};
void test01()
{
Person p1(18);
//如果不写拷贝构造,编译器会自动添加拷贝构造,并且做浅拷贝操作
//浅拷贝只复制指向某个对象的指针,而不复制对象本身,新旧对象还是共享同一块内存
//但深拷贝在堆区重新申请空间(new) 会另外创造一个一模一样的对象,新对象跟原对象不共享内存,修改新对象不会改到原对象
Person p2(p1);
cout << "p2的年龄为: " << p2.age << endl;
}
void test02()
{
Person p1; //此时如果用户自己没有提供默认构造,会出错
Person p2(10); //用户提供的有参
Person p3(p2); //此时如果用户没有提供拷贝构造,编译器会提供
//如果用户提供拷贝构造,编译器不会提供其他构造函数
Person p4; //此时如果用户自己没有提供默认构造,会出错
Person p5(10); //此时如果用户自己没有提供有参,会出错
Person p6(p5); //用户自己提供拷贝构造
}
int main()
{
test01();
return 0;
}
//如果属性有在堆区开辟的(new),一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
//class Person {
//public:
// //无参(默认)构造函数
// Person() {
// cout << "无参构造函数!" << endl;
// }
// //有参构造函数
// Person(int age ,int height) {
//
// cout << "有参构造函数!" << endl;
// m_age = age;
// m_height = new int(height);
//
// }
// //拷贝构造函数
// Person(const Person& p) {
// cout << "拷贝构造函数!" << endl;
// //如果不利用深拷贝在堆区创建新内存,会导致浅拷贝带来的重复释放堆区问题
// m_age = p.m_age;
// m_height = new int(*p.m_height);//浅拷贝带来的重复释放堆区问题
//
// }
// //析构函数
// ~Person() {
// cout << "析构函数!" << endl;
// if (m_height != NULL)
// {
// delete m_height;//浅拷贝带来的重复释放堆区问题
// }
// }
//public:
// int m_age;
// int* m_height;
//};
//class Person {
//public:
// ////传统方式初始化
// //Person(int a, int b, int c) {
// // m_A = a;
// // m_B = b;
// // m_C = c;
// //}
// //初始化列表方式初始化
// Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {}
// void PrintPerson() {
// cout << "mA:" << m_A << endl;
// cout << "mB:" << m_B << endl;
// cout << "mC:" << m_C << endl;
// }
//private:
// int m_A;
// int m_B;
// int m_C;
//};
//int main() {
// Person p(1, 2, 3);
// p.PrintPerson();
// return 0;
//}