-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md.backup
363 lines (228 loc) · 6.56 KB
/
README.md.backup
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Classes
## Table of Contents
1. Memory Management and C++
2. Memory Partitioning
3. The Heap
4. Object Orientation
5. The Three P's
6. Constuctors and Destructors in C++
---
---
## Memory Management and C++
Control over how and where to obtain memory
* Static memory
- Global variables
- Static local variable in functions or classes
* The Stack
- Non-static variables
- Anything declared at **compile time**
+ Can possibly lead to a memory leak
* The Heap
- Dynamically allocated memory
- Variables
+ Static
+ Dynamic
## Memory Partitioning
Operating System allocated memory block from Appropriate division
### Static Memory
Memory allocated during compile time and before program execution
Only one copy of memory will be created
Changes made to static variables will impact all uses of that variable
* Static variables will only go out of scope when the program terminates
Created through the static keyword
```c++
{
void login()
{
static int counter = 0; //only initialized once
readId_pwd();
if(verified())
counter++; //count the number of users that are logged in
}
}
```
### Static
Static variables are not the same as a global variable
* They can be global, but can also be local
* Global variables are always accessible
- Statics in a local sense can only be accessed within that function
These static variables can also be defined within a part of a class
A static local variable doesn't go out of the scope
## The Heap
Used for dynamic allocation
Used within Java because JVM created a Heap for garbage collection and memory allocation
The heap is allocated during run time through dynamic allocation, changed during runtime
* This is where garbage collection and management comes from
### Trouble
Manging memory can come with issues
* Memory leaks
- Forget to delete unused objects or a dynamically created structure
+ Takes up RAM that cannot be reallocated
- If the memory is abandoned by a pointer.. it will continue to exist
* Dangling Reference and Segmentation Faults
- A dangling reference happens when object or memory is trying to be accessed which has already been deleted or out of scope
- A seg-fault happens when you try to access some memory illegally
+ Going out of bounds within an array
+ Accessing bad memory through a dangling reference
+ Using a pointer that hasn't been allocated correctly (null-reference)
---
---
# Object Orientation
## Object Oriented Paradigm
Classes become the primary unit of abstraction
A class definition beings with the keyword *class*
The body of the class is contained within a set of `{ };` (notice the semicolon)
Two things that make a class
* properties
- Properties can accept values like strings, integers, and booleans (true/false values), like any other variable
- Student ID, grades, age, gender
* Object
- Something that would store the information from properties, assign personal properties
* Methods
- A function within a class that manipulates some object or returns values
Method is a function that specifically belongs to a class
Python can have both, where as Java would only deal with methods rather than functions
```c++
{
class class name
{
...
}; //do NOT FORGET the semicolon
}
```
## The Three P's
Encapsulation and Data Hiding require judicious use of Visibility Modifiers
Unlike other programming languages, these are NOT prefixes
Public, Private and Protected are sections within the class
Attention must be payed to organization of properties and methods
* items are private by default
```cpp
{
class class_name
{
private:
...
...
...
public:
...
...
...
}; //SEMI-COLON!
}
```
### Example Class
```cpp
{
class Circle
{
private:
double radius;
public:
void setRadius(double radius);
double getDiameter();
double getArea();
};
}
```
Different than something like Java we only declare the methods and variables (properties) but nothing is defined
### Details
Scope-resolution must be used to define method in the class
The double-colon
```cpp
{
//class A could be forward declared in any situation, as most things can
class A; //forward declaration... not necessary
class A
{
private:
int a;
void doubleA();
public:
static int nextId;
int getA();
void setA(int);
};
int A::nexId = 1000; //modify the variable
int nextId = 1000; //this would declare a Global variable
int getA() //this would define a function rather than a method from the class
{
return this->a;
}
int A::getA() //this defines a METHOD from class A
{
...
}
void A::setA(int val)
{
this ->a = val;
}
void A::doubleA()
{
this->a = a * 2;
}
int main(int argc, char ** argv)
{
return 0;
}
}
```
### Instantiation
Create a variable of the object on the stack or one can be created through a pointer on the heap
* Using a pointer is much more common within C++ code
```cpp
circle c1;
Circle* c2 = new Circle();
```
## Constuctors and Destructors in C++
A **constructor** in a class is a function whose name is the same as the class name and is used to initialize objects
A **destructor** is used to collect garbage
```cpp
{
class Queue {
private:
int queue_size;
protected:
int *buffer;
int front;
int rear;
public:
...
}
}
```
The constructor is defined below:
```cpp
Queue(void) { //constructor
front = 0; rear = 0;
queue_size = 0;
buffer = NULL;
}
Queue(int n){ //constructor
front = 0; rear = 0;
queue_size = n;
buffer = new int[queue_size];
} //constructor Overload
```
If it is put on the heap and we delete the pointer, it would now be a memory leak. We need garabge collection which is not automatically done for us with C++. The destructor allows us to delete everything from the object. Helping with our memory leak.
*Destructor -->*
```cpp
virtual ~Queue(void) {
delete buffer;
buffer[] = NULL;
}
```
### Combine Delete and Destructor
```cpp
void application(){
Queue *myQueue; //declare pointer only
myQueue = new Queue(500); //created in application
myQueue->enqueue(23); //add 23 on myQueue
myQueue->enqueue(8);
delete myQueue; //delete will call ~Queue();
}
```
The destructor is called in *delete* function
### Responsibilities of Garbage Collecting Heap Memory
* Class-Writer: If heap memory is used (through new() operator) in the class (constructor) a destructor must be used to delete the memory - Class users are NOT responsible for the garbage collection of memory they did not explicitly create
* For each new() operation, one must