-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
2417 lines (2119 loc) · 155 KB
/
index.php
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="keywords" content="C++, Programming I, EECS 168, Toolbox, OOP">
<meta name="description" content="Introduction to programming using an object oriented language: using classes, defining classes, and extending classes.
Introduction to algorithms and data structures useful for problem solving: arrays, lists, files, searching, and sorting.">
<meta name="author" content="Brandon Lammey">
<!-- Bootstrap CSS JAVASCRIPT -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- MY CSS -->
<link rel="stylesheet" href="styles.css">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Allerta+Stencil|Gugi|Press+Start+2P" rel="stylesheet">
<title>EECS 168 REVIEW</title>
</head>
<body>
<!-- NavBar: Begin -->
<nav class="navbar navbar-expand-lg navbar-dark fixed-top" id="mainNav">
<a class="navbar-brand" href="#">
<!-- Brand/logo -->
<a class="navbar-brand" href="#">
168
<!-- <img src="pic.jpg" alt="logo" style="width:40px;"> -->
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#toolbox">TOOLBOX</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#concepts">CONCEPTS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#examples">EXAMPLES</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#practice">PRACTICE</a>
</li>
</ul>
</div>
</nav>
<!-- NavBar: End -->
<!-- Intro Header -->
<header class="masthead">
<div class="intro-body">
<div class="container">
<div class="row">
<div class="col-lg-12 mx-auto">
<h1 class="brand-heading">Programming I</h1>
</div>
</div>
</div>
</div>
</header>
<!-- Section Toolbox: Begin -->
<section id="toolbox" class="content-section text-left">
<div class="container">
<div class="row">
<!-- Section Toolbox: Head -->
<div class="col-lg-12 portfolio-item">
<h1 class="my-4"><small>TOOLBOX</small></h1>
</div>
<!-- Tool: Data Types / Operators -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>int</strong> varName; <br>
<strong>double</strong> varName; <br>
<strong>bool</strong> varName; <br>
<strong>char</strong> varName; <br>
std::string varName; <br>
*, /, %, +, - <em>//multiply, divide, mod, add, subtract</em>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#dataTypes" data-toggle="collapse" id="linkHeader">Data Types / Operators</a>
</h4>
<div id="dataTypes" class="collapse">
<p class="card-text">Data types are classifications given to variables that describe what operations can be applied to them.</p>
<ul class="card-text">
<li>integer: Whole numbers</li>
<li>double: Numbers with decimal point</li>
<li>boolean: Represent logical values (True and False)</li>
<li>character: Represent a single character</li>
<li>string: Represent a series of characters. Can contain spaces.</li>
</ul>
<p class="card-text">Operators work just as you would expect. The mod operator gives us the remainder of two numbers. For example, 10 mod 4 will give us 2 because 10/2 has remainder 2.</p>
<p class="card-text">NOTE: operations between different data types are allowed but we need to keep in mind what data types are being assigned. For example, if we assign the addition of two doubles to be an integer, we will only get the whole number (not rounded).</p>
</div>
</div>
</div>
</div>
<!-- Tool: Boolean Logic -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
&& <em>// AND Operation</em> <br>
|| <em>// OR Operation</em> <br>
! <em>// NOT Operation</em> <br> <br>
<em>// Comparison Operations</em> <br>
== <em>// Equal</em> <br>
!= <em>// Not Equal To</em> <br>
> <em>// Greater Than</em> <br>
< <em>// Less Than </em> <br>
>=, <= <em>// Greater than or equal, Less than or equal</em> <br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#boolLogic" data-toggle="collapse" id="linkHeader">Boolean Logic</a>
</h4>
<div id="boolLogic" class="collapse">
<ul class="card-text">
<li>AND: Gives true only when both conditions are true. Otherwise false.</li>
<li>OR: Gives true when at least one condition is true. False if both conditions are false.</li>
<li>NOT: Negates whatever logic is obtained after. If true gives false and if false gives true</li>
<li>EQUAL: Gives true when two values are equal</li>
<li>NOT EQUAL: Gives true when two values are not equal</li>
<li>GREATER THAN: Gives true when the value on the left is bigger than the value on the right</li>
<li>LESS THAN: Gives true when the value on the right is bigger than the value on the left</li>
</ul>
<p class="card-text">NOTE: A single equal sign "=" represents assignment while a double equal "==" represents comparison. If you use "=" for comparison instead of "==", the result will always be TRUE, no matter what the values are.</p>
</div>
</div>
</div>
</div>
<!-- Tool: If / Else-If / Else -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>/*Single IF statement*/</em><br>
<strong>if</strong>(condition) <br>
{<br>
<em>//code that runs only when condition is true</em><br>
}<br>
<br>
<em>/*IF-ELSE statement*/</em><br>
<strong>if</strong>(condition) <br>
{<br>
<em>//code that runs only when condition is true</em><br>
}<br>
<strong>else</strong><br>
{<br>
<em>//code that runs only when above conditions are false</em><br>
}<br>
<br>
<em>/*IF-ELSE IF-ELSE statement*/</em><br>
<strong>if</strong>(condition1) <br>
{<br>
<em>//code that runs only when condition is true</em><br>
}<br>
<strong>else if</strong>(condition2)<br>
{<br>
<em>//code that runs only when condition is true <br>
//AND<br>
//when above conditions are false</em><br>
}<br>
<strong>else</strong><br>
{<br>
<em>//code that runs only when above conditions are false</em><br>
}<br>
<br><br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#ifElse" data-toggle="collapse" id="linkHeader">If / Else-If / Else</a>
</h4>
<div id="ifElse" class="collapse">
<p class="card-text">
If statements give us the ability to control the flow of our program. We write a condition that check if such condition is met (check if true). If it is, we continue through the code in the correct branch.
</p>
<p class="card-text">
We can also include "else if" and/or "else" statements after an if to write code that will only be executed if the above conditions in the block are false.
</p>
<p class="card-text">
NOTE:
<ul class="card-text">
<li>if statements do not necessarily require an else</li>
<li> if statements can be nested inside other if statements or within loops</li>
</ul>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Loops (While / Do-While / For) -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>/*While Loop*/</em><br>
<strong>while</strong>(condition)<br>
{<br>
<em>//check if the condition is true</em> <br>
<em>//if true execute code in this block</em> <br>
<em>//repeat until the condition becomes false</em> <br>
}<br>
<br>
<em>/*Do-While Loop*/</em><br>
<strong>do</strong><br>
{<br>
<em>//execute code in this block</em> <br>
<em>//check if the condition is true</em> <br>
<em>//repeat until the condition becomes false</em> <br>
}<strong>while</strong>(condition)<br>
<br>
<em>/*For Loop*/</em><br>
<strong>for</strong>(initialize variable; condition; increment)<br>
{<br>
<em>//execute code in this block</em> <br>
<em>//check if the condition is true</em> <br>
<em>//repeat until the condition becomes false</em> <br>
}<br>
<br><br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#loops" data-toggle="collapse" id="linkHeader">Loops: While / Do-While / For</a>
</h4>
<div id="loops" class="collapse">
<p class="card-text">
Loops are used when we need to repeatedly execute a block of statements. The code within the loop will execute as long as the conditions of the loop are met.
<ul class="card-text">
<li>While: The test condition is tested before entering the loop body. While the condition is true, the code within the loop will continue to execute. </li>
<li>Do-While: The test condition is tested or evaluated at the end of the loop body ensuring the loop body will execute at least once before checking the condition.</li>
<li>For: a loop variable is used to control the loop which is executed a specific number of times. The expression is initilized then tested. If true, then the code in the block will execute and then the expresssion will be incremented and then checked again. Once false, the loop will end. </li>
</ul>
NOTE: Avoid infinite loops by ensuring and condition is met which causes the loop to exit with the condition failing (evaluating to False).
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Arrays (1D) -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>/* stack allocated */ </em><br>
<strong>type</strong> arrayName [ arraySize ] = {assignment};<br>
arrayName[position] = value;<br>
<br><br>
<em>/* heap allocated */</em><br>
<strong>type</strong>* arrPointer = <strong>nullptr</strong>;<br>
arrPointer = <strong>new</strong> <strong>type</strong>[size];<br>
<em>//once done delete array to free in memory </em><br>
<strong>delete</strong>[] arrPointer;<br>
arrPointer = <strong>nullptr</strong>;<br><br><br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#1DArrays" data-toggle="collapse" id="linkHeader"> 1-D Arrays </a>
</h4>
<div id="1DArrays" class="collapse">
<p class="card-text">
An array is a collection of data that holds a fixed number of values of same type. Think of it as a grid of elements. A 1-D array will consist of one row with multiple columns, each holding a single value (all of which are the same data type) which can be accessed at an index arr[index].
<ul class="card-text">
<li>Stack Allocated: Array is declared on the stack and thus does not require "delete" statement to free memory</li>
<li>Heap Allocated: Dynamic memory allocation (on the heap) performed by "new" allows to assign memory during runtime using any variable value such as size. Used for when the size of the array is large or for when the size is unknown at compile time (before program is running). Memory allocated dynamically must be freed using "delete" once it is no longer needed </li>
</ul>
NOTE:
<ul class="card-item">
<li>
Arrays have 0 as the first index not 1. An array of size n will have indices ranging from 0 to n-1
</li>
<li>
Arrays have a constant size which cannot be changed once set
</liul>
<li>
Failing to free memory (use "delete" for every "new") will result in memory leaks
</li>
</ul>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Arrays (2D) -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>//dimensions</em><br>
<strong>int</strong> A = some size;<br>
<strong>int</strong> B = some size;<br>
<br>
<em>//dynamic allocation</em><br>
<em>//create an array of pointers of size A</em><br>
<strong>type</strong>** arrName = <strong>new</strong> <strong>type</strong>*[A];<br>
<br>
<em>//point each index to a new array of size B</em> <br>
<strong>for</strong>(<strong>int</strong> i = 0; i < A; ++i)<br>
arrName[position] = <strong>new type</strong>[B];<br>
<br>
<em>//free memory</em><br>
<strong>for</strong>(<strong>int</strong> i = 0; i < A; ++i)<br>
<strong>delete</strong>[] arrName[i];<br>
<strong>delete</strong>[] arrName; <em>// delete array of pointers</em><br>
arrName = nullptr; <br><br><br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#multiDimArrays" data-toggle="collapse" id="linkHeader">Multi-Dimensional Arrays</a>
</h4>
<div id="multiDimArrays" class="collapse">
<p class="card-text">
The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To create a dynamically allocated 2-D array, first an array of pointers is created. Each pointer is then used to point to a new array. This can be thought of as a 2-D grid with rows and columns.
</p>
<p class="card-text">NOTE: Remember to delete the arrays furthest from the initial pointer first and move in. Deleting the array of pointers first will only delete that array and result in the arrays that were being pointed to being lost (memory leaks) </p>
</div>
</div>
</div>
</div>
<!-- Tool: void functions -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>void</strong> function_name( parameter list ) <br>
{<br>
<em>//body of the function</em><br>
}<br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#voidFunctions" data-toggle="collapse" id="linkHeader">Void Functions</a>
</h4>
<div id="voidFunctions" class="collapse">
<p class="card-text">
The function name and the parameter list together constitute the function signature.
To use the function this name is called upon with the appropriate paramaters (if any) fed in.
A parameter is like a placeholder.
When a function is invoked, you pass a value to the parameter.
This value is referred to as an argument. Parameters are optional.
Void functions return no values but can modify data and print to the console.
<br><br>
To call a function, you simply need to pass the required parameters along with function name.
</p>
</div>
</div>
</div>
</div>
<!-- Tool: type functions -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>return_type</strong> function_name( parameter list ) <br>
{<br>
<em>//body of the function</em><br>
<strong>return</strong>(returnVar);<br>
}<br>
<em> /*<br>
Call by Reference VS Call by Value
<br>
All arrays are pass by reference<br>
primatives are call by value unless '&' is used <br>
typeName &myVar will result in call by value
<br>
/*
</em><br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#typeFunctions" data-toggle="collapse" id="linkHeader">Type Functions</a>
</h4>
<div id="typeFunctions" class="collapse">
<p class="card-text">
A function may return a value. The return_type is the data type of the value the function returns
(such as an int, bool, int*, etc).
<br><br>
Calling this function is similar to void but to store the value returned
you can set is equal to a variable of the same type.
<br><br>
NOTE: For all function arguments, the function must declare variables that accept the values of the arguments (called formal parameters).
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
There are two methods to pass an argument:<br>
<ul class="card-item">
<li>Call by Value: This method copies the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no effect on the argument.</li>
<li>Call by Reference: This method copies the reference of an argument into the formal parameter.
Inside the function, the reference is used to access the actual argument used in the call.
This means that changes made to the parameter affect the argument. </li>
</ul>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Class Header -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>#ifdef</strong> CLASS_NAME_H <br>
<strong>#define</strong> CLASS_NAME_H <br><br>
<strong>class</strong> class_name <br>
{<br>
<strong>public:</strong><br>
class_name(); <em>//Declare class constructor</em><br>
~class_name(); <em>//Declare class destructor</em><br>
<em>/*Add other public scoped functions <br>
and variables*/</em><br><br>
<strong>private:</strong><br>
<em>/*Add private scoped variables*/</em><br>
};<br>
<strong>#endif</strong>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#classHeader" data-toggle="collapse" id="linkHeader">Class Header (.h)</a>
</h4>
<div id="classHeader" class="collapse">
<p class="card-text">
Header files allow us to declare what methods and member variables we are using in our class. A method is just how we call a function from a class.
</p>
<p class="card-text">The first two lines as well as the last line of the header file will always be those pound sign declarations. These are just declarations we make to tell the compiler to define our header.</p>
<p class="card-text">Methods and member variables inside classes can have either public or private scope:</p>
<ul>
<li>Public Scope: Methods and member variables declared here can be accessed by whoever is using the class (in most cases main). This means that values can be easily changed.</li>
<li>Private Scope: Member variables declared here can only be accessed by methods inside of our class. So we cannot change a variable ourselves, we have to write a method that changes it for us.</li>
</ul>
<p class="card-text">NOTE:
<ul>
<li>We use private because it allows us to add an extra level of protection to our variables.
The fact that a variable cannot be changed anywhere in a program makes it much easier to find problems with our code.
</li>
<li>
The constructor is a function that is only called once, implicitly (we don't call it ouselves), at the time of the objects creation.
Constructors always have the same name as the class itself and have no return type.
It is used to initialize variables of a class at the time of object creation.
They can also take parameters as well just like a normal function. Typically, these arguments help initialize an object when it is created.
</li>
</ul>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Class Definition -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>#include</strong> "class_name.h"<br>
<em>//constuctor definition</em><br>
class_name :: class_name()<br>
{<br>
<em>//initialize variables to a default value</em><br>
}<br>
<strong>return_type</strong> class_name::classFunc(parameters)<br>
{<br>
<em>/*define functions</em><br>
<em>declared in the header (.h) files*/</em><br>
}<br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#classDefinition" data-toggle="collapse" id="linkHeader">Class Definition (.cpp)</a>
</h4>
<div id="classDefinition" class="collapse">
<p class="card-text">
The member function definitions for a class are stored in a separate .cpp file, which is called the class implementation file.
The file usually has the same name as the class, with the .cpp extension.
<br><br>
Constructor: <br>
<ul>
<li>Constructor has same name as the class itself</li>
<li>Constructors don’t have return type</li>
<li>A constructor is automatically called when an object is created</li>
<li>If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body)</li>
<li>Constructor Overloading is possible by having multiple constructors which are parameterized differently (pass arguments to a constructor)</li>
</ul>
Destructor: <br>
<ul>
<li>Destructors have same name as the class preceded by a tilde (~)</li>
<li>Destructors don’t take any argument and don’t return anything</li>
<li>There can only one destructor in a class with classname preceded by ~</li>
<li>If we do not write our own destructor in a class, compiler creates a default destructor for us</li>
<li>When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed</li>
<li>A destructor function is called automatically when the object goes out of scope: the function ends, the program ends, a block containing local variables ends, or a delete operator is called </li>
</ul>
<br>
NOTE:
The advanages of storing class definition in separate file are
<ul>
<li>The class is reusable</li>
<li>The clients of the class know what member functions the class provides, how to call them and what return types to expect</li>
<li>The clients do not know how the class's member functions are implemented.</li>
</ul>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Copy Constructors: Shallow vs Deep Copies -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<strong>class</strong> MyObject <br>
{<br>
<strong>public</strong>:<br>
<em>// simple constructor</em><br>
MyObject(<strong>int</strong> len); <br>
<em>// copy constructor</em><br>
MyObject(const MyObject &obj); <br>
<em>// destructor</em>
~MyObject();<br>
<strong>private</strong>:<br>
<strong>int</strong>* m_ptr;<br>
};
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#SvDCopy" data-toggle="collapse" id="linkHeader">Copy Constructors: Shallow vs. Deep Copy</a>
</h4>
<div id="SvDCopy" class="collapse">
<p class="card-text">
A shallow copy may contain members that point to original objects whereas a deep copy always clones every single member.<br><br>
A shallow copy of an object copies all of the member field values.
This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory,
since copying a pointer simply results in a pointer that points to the same memory as the original pointer. A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields.
To make a deep copy, you must write a copy constructor. <br><br>
The copy constructor is a constructor which creates an object by initializing it with an object of the same class,
which has been created previously. If a copy constructor is not defined in a class, the compiler itself defines one.
If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.
If it does not, then shallow copies are made.
<br><br>
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Dereferencing a Pointer -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>/*Simple Comparison*/</em><br>
Obj* myObjPtr;<br>
myObjPtr = <strong>new</strong> Obj;<br>
myObjPtr; <em>//will access the pointer </em><br>
*myObjPtr; <em>//will access the object directly<br>
</em>
<br>
<em>/*Dereferencing the this pointer*/</em><br>
<strong>class</strong> MyObject <br>
{<br>
<strong>public</strong>:<br>
<strong>returnType</strong> myFunc() <br>
{<br>
<em>//does something</em><br>
}<br>
<strong>returnType</strong> anotherFunc(const MyObject &objParamater)<br>
{<br>
<strong>this</strong>->myFunc(); <em>// calling memeber</em><br>
objParamater.myFunc(); <em>//member passed in</em><br>
*<strong>this</strong>; <em>//access calling object directly </em><br>
}<br>
<strong>private</strong>:<br>
<em>//private member variables</em><br>
};
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#DereferencePointer" data-toggle="collapse" id="linkHeader">Dereferencing a Pointer</a>
</h4>
<div id="DereferencePointer" class="collapse">
<p class="card-text">
Dereferencing a pointer means getting the value that is stored in the memory location pointed to by the pointer.
The operator * is used to do this, and is called the dereferencing operator.
This is important when writing member functions which need to access the calling object.
<br><br>
Every object in C++ has access to its own address through an important pointer called this pointer.
The <i>this</i> pointer is an implicit parameter to all member functions.
Therefore, inside a member function, <i>this</i> may be used to refer to the invoking object.
<br><br>
An important application of using the <i>this</i> keyword and * is when trying to return the calling object in a function.
</p>
</div>
</div>
</div>
</div>
<!-- Tool: Try Catch Blocks -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
<em>//Try Catch Block:</em><br>
<strong>try</strong><br>
{<br>
<em>/* protected code */</em><br>
}<br>
<strong>catch</strong>(ExceptionType e)<br>
{<br>
<em>/*Code that handles exception*/</em><br>
}<br><br>
<em>//Throwing an Exception:</em><br>
returnType someFunc(parameters)
{
<strong>throw</strong> someException;
}
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#tryCatch" data-toggle="collapse" id="linkHeader">Exception Handling (Try Catches)</a>
</h4>
<div id="tryCatch" class="collapse">
<p class="card-text">
Exceptions allow us to react to and handle exceptional circumstances that arise while the program is running. There are three main keywords:
</p>
<ul>
<li><b>Throw:</b> An exception is <i>thrown</i> when a problem shows up like dividing by zero. We throw either the error number, an error message, or whatever helps us identify where the error occurred. </li>
<li><b>Try:</b> Code inside a <i>try</i> is risky code that could possibly throw an exception such as using a divide function which takes in a numerator
and denominator and should return an int. Attempting to divide by zero by calling this function with a 0 denominator paramater should throw an exception which can be caught in the try-catch block </li>
<li><b>Catch:</b> A program <i>catches</i> a thrown exception and handles the problem, such as by outputting a message to the user.</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Tool: Operator Overloading -->
<div class="col-lg-6 portfolio-item">
<div class="card h-100">
<div class="code">
ObjName& <strong>operator</strong>+(const ObjName &rhs)<br>
{<br>
<em>/*Code that adds these two objects*/</em><br>
}<br><br>
<strong>bool</strong> <strong>operator</strong>==(const ObjName &rhs) const<br>
{<br>
<em>/*Code that compares these two objects*/</em><br>
}<br>
</div>
<div class="card-body">
<h4 class="card-title">
<a href="#operatorOverloading" data-toggle="collapse" id="linkHeader">Operator Overloading</a>
</h4>
<div id="operatorOverloading" class="collapse">
<p class="card-text">
Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined.
Like any other function, an overloaded operator has a return type and a parameter list.
<br><br>
We use operator overloading to make operators work for things other than the ones they are originally designed to work with. For example, we can make a '+' work with strings and make it concatenate two strings.
We can even write a definition to compare two objects in a similar manner to comparing two ints or two doubles.
<br><br>
NOTE: Be cautious of your return type, pass by reference and pass by value will both compile but have different functionalities.
Passing by reference will allow for chaining operations to work as expected.
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- Section Toolbox: End -->
<!-- Section General Concepts: Begin -->
<section id="concepts" class="content-section text-left">
<div class="container">
<div class="row">
<!-- Section General Concepts: Head -->
<div class="col-lg-12 portfolio-item">
<h1 class="my-4"><small>General Concepts</small></h1>
</div>
<!-- Concepts: Memory Allocation -->
<div class="col-lg-12 portfolio-item">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
<a href="#memAllocArr" id="linkHeader">Memory Allocation Diagram: Arrays</a>
</h4>
<div class="card-group">
<div class="card">
<img class="card-img-top" src="graphics/arrays1.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Create Stack Array + Array Pointers</h5>
<p class="card-text" id = "code">
<strong>int</strong> stackArr [ 3 ];<br>
<strong>int</strong>* heapArr = <strong>nullptr</strong>;<br>
<strong>int</strong>** twoDimArr = <strong>nullptr</strong>;<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
<div class="card">
<img class="card-img-top" src="graphics/arrays2.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Create Heap Arrays</h5>
<p class="card-text" id= "code">
stackArr[1] = 7;<br><br>
heapArr = <strong>new</strong> <strong>int</strong>[3];<br>
heapArr[1] = 7;<br><br>
<strong>for</strong>(<strong>int</strong> i = 0; i < 3; ++i)<br>
twoDimArr[i] = <strong>new int</strong>[3];<br>
twoDimArr[1][2] = 7;<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
<div class="card">
<img class="card-img-top" src="graphics/arrays3.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Delete Heap Arrays</h5>
<p class="card-text" id= "code">
<strong>delete</strong>[] heapArr; <br><br>
<strong>for</strong>(<strong>int</strong> i = 0; i < 3; ++i)<br>
<strong>delete</strong>[] twoDimArr[i];<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Concepts: Memory Allocation -->
<div class="col-lg-12 portfolio-item">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
<a href="#memAllocFunc" id="linkHeader">Memory Allocation Diagram: Functions</a>
</h4>
<div class="card-group">
<div class="card">
<img class="card-img-top" src="graphics/func0.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Pass By Value (With Return Type)</h5>
<p class="card-text" id="code">
<strong>int</strong> multiply (<strong>int</strong> n, <strong>int</strong> m)<br>
{<br>
<strong>return</strong>(n*m);<br>
}<br>
main()<br>
{<br>
<strong>int</strong> varA = 3;<br>
<strong>int</strong> varB = 4;<br>
<strong>int</strong> varC = multiply(varA, varB);<br>
}<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
<div class="card">
<img class="card-img-top" src="graphics/func1.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Pass By Reference (With No Return)</h5>
<p class="card-text" id="code">
<strong>void</strong> modify (<strong>int</strong> &n)<br>
{<br>
n = 7;<br>
}<br>
main()<br>
{<br>
<strong>int</strong> myInt = 5;<br>
modify(myInt);<br>
}<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
<div class="card">
<img class="card-img-top" src="graphics/func2.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Pass By Reference: Arrays </h5>
<p class="card-text scrollable" id="code">
<strong>void</strong> zeroArr (<strong>int</strong> myArr[], <strong>int</strong> size)<br>
{<br>
<em>
/*<br>
dataType* VS dataType[]<br>
int* is an int pointer, and pointers can be changed to point to other things<br>
int[] cannot be made to refer (point) to anything else<br>
*/<br>
</em>
<strong>for</strong>(<strong>int</strong> i; i < size; i++)<br>
myArr[i] = 0;<br>
}<br>
main()<br>
{<br>
<strong>int</strong> size = 3;<br>
<strong>int</strong>* arrPtr = <strong>nullptr</strong>;<br>
arrPtr = <strong>new</strong> <strong>int</strong>[size];<br>
zeroArr(arrPtr, size);<br>
<em>
/* once zeroArr is complete<br>
it is popped off the stack<br>
remember to free memory<br>
at the end of your program */<br>
</em>
}<br>
</p>
<!-- <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> -->
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Concepts: Classes -->
<div class="col-lg-12 portfolio-item">
<div class="card h-100">
<a href="#"><img class="card-img-top" id="classes" src="graphics/obj.png" alt=""></a>
<div class="card-body">
<h4 class="card-title">
<a href="#classesVisualization" id="linkHeader">Classes (Visualization)</a>
</h4>
<!-- <p class="card-text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit aliquam aperiam nulla perferendis dolor nobis numquam, rem expedita, aliquid optio, alias illum eaque. Non magni, voluptates quae, necessitatibus unde temporibus.
</p> -->
<div class="row">
<div class="col-lg-4 portfolio-item">
<h5>main.cpp</h5>
<p class="card-text scrollable" id="code">
<strong>#include</strong> < iostream > <br>
<strong>#include</strong><"dog.h"> <br>
<br>
<strong>int</strong> main()<br>
{<br>
std::string breed;<br>
std::string name;<br>
<strong>int</strong> age;<br>
<strong>int</strong> arrSize;<br><br>
<em>//cin appropriate parameters</em><br>
dog stackDog(breed, name, age);<br>
<em>//cin appropriate parameters</em><br>
dog* dogPtr = <strong>new</strong> dog(breed, name, age);<br>
dog* dogArrPtr = <strong>new</strong> dog[arrSize];<br><br>
<em>//for a dynically allocated (heap) array of dog pointers</em> <br>
<em>//dog** dogArrPtr = new dog*[arrSize];</em><br><br>
<strong>for</strong>(<strong>int</strong> i = 0; i++; i< arrSize)<br>
{<br>
<em>//cin appropriate parameters</em><br>
dogArrPtr[i].setBreed(breed);<br>
dogArrPtr[i].setName(name);<br>
dogArrPtr[i].setAge(age);<br>
<em>//can be done this way as well</em><br>
<em>// dogArrPtr[i] = dog(breed, name, age);</em><br><br>
<em>/*<br>
for a dynically allocated (heap) array of dog pointers <br>
each index points to a new dog object<br>
function calls require "->" notation<br>
dogArrPtr[i] = new dog();<br>
dogArrPtr[i] -> setBreed(breed);<br>
dogArrPtr[i] -> setName(name);<br>
dogArrPtr[i] -> setAge(age);<br>
*/<br></em>
}<br>
<em>//memory visualization at this point</em><br><br>
<em>
//remember to free memory at the end<br>
//stackDog does not require a manual delete<br>
</em>
<strong>delete</strong> dogPtr; <em>//dont use [] since we are deleting an object not an array</em><br>
<strong>delete[]</strong> dogArrPtr; <br><br>
<em>
/*<br>
for a dynically allocated (heap) array of dog pointers <br>
for(int i = 0; i< arrSize; i++)<br></arrSize>
{<br>
//deleting objects not arrays so do not use []<br>
delete dogArrPtr[i]; //deletes dogs<br>
}<br>
delete[] dogArrPtr; //deletes array<br>
*/<br>
</em>
} <br>
</p>
</div>
<div class="col-lg-4 portfolio-item">
<h5>dog.h</h5>
<p class="card-text scrollable" id="code">
<strong>#ifndef</strong> DOG_H <br>
<strong>#define</strong> DOG_H<br>