forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_assignment.cpp
More file actions
225 lines (198 loc) · 7.09 KB
/
object_assignment.cpp
File metadata and controls
225 lines (198 loc) · 7.09 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
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
*
* Program: Object Assignment
*
* Description: Demonstration of how object assignment works in C++, including
* an example of why we need to be careful with object assignment if our objects
* contain member variables that are pointers to data on the heap.
*
* YouTube Lesson: https://www.youtube.com/watch?v=SJy4_Ci80No
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// A simple object with a single public int member variable
class Simple
{
public:
int x;
};
// An object with a member variable that is itself an object
class Stack
{
public:
Simple simple;
};
// An object with a member variable that is a pointer to data on the heap
class Heap
{
public:
Simple *simple;
Heap(int set_x)
{
simple = new Simple;
simple->x = set_x;
}
};
int main()
{
// See the visualizations of memory in the comment below the main function
// for these three examples...
// Example 1
//
// Assign an object to another object.
//
// create two Simple objects, assign a value to member variable x
Simple simpleA;
simpleA.x = 4;
Simple simpleB;
simpleB.x = 0;
// when we assign simpleA to simpleB, the value of member variable x for
// simpleA will be assigned to the value of the member variable x for
// simpleB, we call this a "shallow copy"
simpleB = simpleA;
// we'll find that both x values are the same (4)
cout << "simpleA.x: " << simpleA.x << endl;
cout << "simpleB.x: " << simpleB.x << endl;
// if we alter the member variable x of simpleB, this will not effect the
// member variable x of simpleA, as they are two distinct objects in
// memory
simpleB.x = 20;
cout << "simpleA.x: " << simpleA.x << endl;
cout << "simpleB.x: " << simpleB.x << endl;
// Example 2
//
// Assign an object to another object when the object's have member variables
// that are themselves objects.
//
// Create two Stack objects, both of which have Simple object's as member
// variables... assign to the x member variable of these Simple objects
Stack stackA;
stackA.simple.x = 4;
Stack stackB;
stackB.simple.x = 0;
// When we assign stackA to stackB, an assignment operation occurs with the
// Simple object member variables, and then as in Example 1 the member
// variable x value is assigned
stackB = stackA;
cout << "stackA.simple.x: " << stackA.simple.x << endl;
cout << "stackB.simple.x: " << stackB.simple.x << endl;
// Again if we modify the x member variable of the Simple object of stackB,
// we're going to find this does not modify the x member variable of the
// Simple object of stackB... because these are all distinct objects in
// memory... altering one does not effect the other.
stackB.simple.x = 20;
cout << "stackA.simple.x: " << stackA.simple.x << endl;
cout << "stackB.simple.x: " << stackB.simple.x << endl;
// Example 3
//
// Assign an object to another object when the object member variables
// include pointers to data on the heap.
//
// Create two heap objects, the constructor for heap objects will create
// a Simple object and assign to the Simple object pointer member variable
// "simple" the memory address for this object on the heap.
Heap heapA(4);
Heap heapB(0);
// When we assign heapA to heapB, just as in the above two examples,
// the values of the member variables of heapA will be assigned to the
// member variables of heapB. BUT the member variable simple in this
// case is a POINTER that stores a memory address for Simple objects on
// the HEAP. So heapB's simple pointer member variable will store the
// SAME memory address as heapA's simple pointer member variable after
// the assignment... in other words, they will be pointing to the same
// object on the heap after this assignment!
heapB = heapA;
// Both x values will output as '4' which may give us the impression that
// we're getting the same behaviour as Examples 1 and 2, but what's really
// going on this time is that heapA and heapB's simple pointer member
// variable are both pointing to the SAME Simple object.
cout << "heapA.simple->x: " << heapA.simple->x << endl;
cout << "heapB.simple->x: " << heapB.simple->x << endl;
// When we alter the x value of the object that heapB's simple pointer
// member variable is pointing to, we modify the object that BOTH heapA
// and heapB are now pointing to! So we'll get the same result (20) as
// output for "both" x values (really they are the same object).
heapB.simple->x = 20;
cout << "heapA.simple->x: " << heapA.simple->x << endl;
cout << "heapB.simple->x: " << heapB.simple->x << endl;
return 0;
}
//
//
// Example 1 Memory View:
//
// STACK
// =====
//
// ------------ ------------
// | simpleA | | simpleB |
// | | | |
// | int x = 4 | | int x = 20 |
// ------------ ------------
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// HEAP
// ====
//
// Nothing
//
//
//
//
//
// Example 2 Memory View:
//
// STACK
// =====
//
// --------------- ---------------
// | stackA | | stackB |
// | | | |
// | Simple simple | | Simple simple |
// --------- | --- --------- | ---
// | |
// | |
// ------------ ------------
// | simple | | simple |
// | | | |
// | int x = 4 | | int x = 20 |
// ------------ ------------
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// HEAP
// =====
//
// Nothing
//
//
//
//
//
// Example 3 Memory View:
//
// STACK
// =====
//
// ---------------- ----------------
// | heapA | | heapB |
// | | | |
// | Simple *simple | | Simple *simple |
// --------- | ---- --------- | ----
// | /
// | ----------
// | /
// ~~~~~~~~~~~~ | ~~~~~~~ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// | /
// | / HEAP
// | / =====
// ------------ ------------
// | simple | | simple |
// | | | |
// | int x = 20 | | int x = 0 |
// ------------ ------------
//