-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp.html
3515 lines (3166 loc) · 132 KB
/
cpp.html
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>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>C++ Cheatsheet</title>
<!-- Bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<!-- CSS -->
<link rel="stylesheet" href="assets/style.css">
</head>
<body data-theme="light">
<div class="container">
<!-- Theme Toggle -->
<div class="button float-end"><i class="bi bi-circle-half"></i></div>
<h3 class="title">C++</h3>
<div class="section-list">
<ul>
<!-- Basic -->
<li><a href="#program-structure">Program Structure</a></li>
<li><a href="#compile-execute">Compile & Execute</a></li>
<li><a href="#input-output">Input & Output</a></li>
<li><a href="#comments">Comments</a></li>
<li><a href="#variables">Variables</a></li>
<li><a href="#data-types">Data Types</a></li>
<li><a href="#storage-classes">Storage Classes</a></li>
<li><a href="#operators">Operators</a></li>
<li><a href="#decision-making">Decision Making</a></li>
<li><a href="#loops">Loops</a></li>
<li><a href="#loop-control-statements">Loop Control Statements</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#pointers">Pointers</a></li>
<li><a href="#references">References</a></li>
<li><a href="#functions">Functions</a></li>
<li><a href="#enum">Enumeration</a></li>
<li><a href="#struct">Structures</a></li>
<!-- OOP -->
<li><a href="#class-objects">Class & Objects</a></li>
<li><a href="#constructor-destructor">Constructor & Destructor</a></li>
<li><a href="#inheritance">Inheritance</a></li>
<li><a href="#polymorphism">Polymorphism</a></li>
<li><a href="#abstract-class">Abstract Classes</a></li>
<li><a href="#abstraction">Abstraction</a></li>
<li><a href="#encapsulation">Encapsulation</a></li>
<!-- Advanced -->
<li><a href="#exception-handling">Exception Handling</a></li>
<li><a href="#file-streams">Files & Streams</a></li>
<li><a href="#dynamic-memory">Dynamic Memory</a></li>
<li><a href="#template">Templates</a></li>
<li><a href="#namespaces">Namespaces</a></li>
<li><a href="#preprocessor">Preprocessor</a></li>
</ul>
</div>
<div class="sections" id="program-structure">
<h4 class="heading"># Program Structure</h4>
<pre class="code-box code-box-color"><code>#include<iostream>
using namespace std;
int main(){
cout << "Hello World" << endl;
return 0;
}</code></pre>
</div>
<div class="sections" id="compile-execute">
<h4 class="heading"># Compile & Execute</h4>
<p>The g++ is a compiler that is used to compile programs written in the C++ programming language. It is part of the GNU Compiler Collection (GCC).</p>
<h5>• Compiling Program :</h5>
<div class="code-box"><code>g++ file.cpp</code></div>
<div class="code-box"><code>g++ file.cpp -o exe_name</code></div>
<h5>• Executing Program :</h5>
<div class="code-box"><code>./a.out</code></div>
<div class="code-box"><code>./exe_name</code></div>
</div>
<div class="sections" id="input-output">
<h4 class="heading"># Input & Output</h4>
<p>The <code>iostream</code> (or input-output stream) is a C++ Standard Library (or header file) that provides definitions for the <code>istream</code> and <code>ostream</code> classes. It is used to read input from a stream of characters, such as a console or file, and write output data to the console or file.</p>
<div>
<h5>• Ostream Class :</h5>
<p>The <code>ostream</code> (or output stream) is used to write a stream of characters as output to the console or a file.</p>
<ul>
<li>
<p><b>Console Output :</b> The <code>cout</code> (or console-output) is a predefined object of the ostream class. It is used with a stream insertion operator (<code><<</code>) to display output.</p>
<pre class="code-box code-box-color"><code>cout << "Hello World";
cout << "\nValue of X is " << x;</code></pre>
</li>
<li>
<p><b>End Line :</b> The <code>endl</code> (or end-line) is a predefined object of the ostream class. It is used to insert new line characters and flush the stream.</p>
<pre class="code-box code-box-color"><code>cout << "Hello World" << endl;</code></pre>
</li>
</ul>
</div>
<div>
<h5>• Istream Class :</h5>
<p>The <code>istream</code> (or input stream) is used to handle input operations from a stream of characters (such as the console or a file).</p>
<ul>
<li>
<p><b>Console Input :</b> The <code>cin</code> (or console input) is a predefined object of the istream class. It is used with the stream extraction operator (<code>>></code>) to read input from the console.</p>
<pre class="code-box code-box-color"><code>cin >> n;
cin >> a >> b >> c;</code></pre>
</li>
</ul>
</div>
<div>
<h5>• Escape Sequences :</h5>
<p>The escape sequences are special non-printing characters that are used to control the printing behavior of the output stream objects.</p>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Escape Char</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\a</code></td>
<td>Alarm or beep sound</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>Backspace</td>
</tr>
<tr>
<td><code>\f</code></td>
<td>Form feed</td>
</tr>
<tr>
<td><code>\n</code></td>
<td>New line</td>
</tr>
<tr>
<td><code>\r</code></td>
<td>Return</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>Tab space</td>
</tr>
<tr>
<td><code>\\</code></td>
<td>Backslash</td>
</tr>
<tr>
<td><code>\'</code></td>
<td>Single quote</td>
</tr>
<tr>
<td><code>\?</code></td>
<td>Question mark</td>
</tr>
<tr>
<td><code>\nnn</code></td>
<td>Octal number</td>
</tr>
<tr>
<td><code>\xhh</code></td>
<td>Hexadecimal number</td>
</tr>
<tr>
<td><code>\0</code></td>
<td>Null</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="comments">
<h4 class="heading"># Comments</h4>
<div>
<h5>• Single-Line Comment :</h5>
<p><code>//</code> is used for single-line comment.</p>
<pre class="code-box code-box-color"><code>// This is single-line comment.
cout << "Hello World"; // This is comment</code></pre>
</div>
<div>
<h5>• Multi-Line Comment :</h5>
<p><code>/* */</code> is used for multi-line comment.</p>
<pre class="code-box code-box-color"><code>/* This is
multi-line comment. */
int x = 22; /* This is multi-line
comment. */</code></pre>
</div>
</div>
<div class="sections" id="variables">
<h4 class="heading"># Variables</h4>
<div>
<h6>Syntax :</h6>
<div class="code-box"><code>data_type variable_name;</code></div>
<pre class="code-box code-box-color"><code>int a;
int x, y, z = 17;
int n = 22;
string name = "Mandar";</code></pre>
</div>
<div>
<h5>• Local & Global Variables :</h5>
<p>The <strong>Local variables</strong> are declared inside a function or block and can be used only by statements that are inside that function or block. The <strong>Global variables</strong> are defined outside of all the functions, can be accessed by any function, and hold their value throughout the life of program.</p>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>#include<iostream>
using namespace std;
// Global Variables :
float f = 17.22;
int n = 0;
int main(){
// Local Variables :
int x, y = 22;
float a, b;
a = b = 22.17;
return 0;
}</code></pre>
</div>
</div>
<div class="sections" id="data-types">
<h4 class="heading"># Data Types</h4>
<p>A data type specifies the type of data that a variable can store such as integer, floating, character, etc.</p>
<div>
<h5>• Primitive Data Types :</h5>
<p>A primitive type is predefined and built into a programming language. It is named with a reserved keyword.</p>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Date Type</th>
<th>Size</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>bool</code></td>
<td>1 byte</td>
<td>boolean values (<code>true</code> or <code>false</code>)</td>
</tr>
<tr>
<td><code>char</code></td>
<td>1 byte</td>
<td>single character, letter, number or ASCII values</td>
</tr>
<tr>
<td><code>int</code></td>
<td>4 byte</td>
<td>integers (non-decimals)</td>
</tr>
<tr>
<td><code>float</code></td>
<td>4 byte</td>
<td>fractional numbers (upto 7 decimal digits)</td>
</tr>
<tr>
<td><code>double</code></td>
<td>8 byte</td>
<td>fractional numbers (upto 15 decimal digits)</td>
</tr>
<tr>
<td><code>void</code></td>
<td>-</td>
<td>represents no type or absence of type</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Data Type Modifier :</h5>
<p>Modifiers are prefix keywords that change the properties of the basic primitive data type.</p>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Date Type</th>
<th>Size</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>signed</code></td>
<td>4 byte</td>
<td>only negative numbers</td>
</tr>
<tr>
<td><code>unsigned</code></td>
<td>4 byte</td>
<td>only positive numbers</td>
</tr>
<tr>
<td><code>short</code></td>
<td>2-4 byte</td>
<td>small numbers (machine dependent)</td>
</tr>
<tr>
<td><code>long</code></td>
<td>4-8 byte</td>
<td>large numbers (machine dependent)</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Date Type Qualifiers :</h5>
<p>A type qualifier is used to refine the declaration of a variable, a function, and parameters.</p>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Keyword</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>const</code></td>
<td>constant (read-only variables that can't be reassigned)</td>
</tr>
<tr>
<td><code>volatile</code></td>
<td>volatile variables' value could change unexpectedly, and no optimization is done on them.</td>
</tr>
<tr>
<td><code>restrict</code></td>
<td>restrict is used for pointers, which tells the compiler that a restricted pointer is the only way to access the object pointed at it (no other pointer can point to the same object).</td>
</tr>
</tbody>
</table>
</div>
<pre class="code-box code-box-color"><code>const int COUNT = 1;</code></pre>
</div>
<div>
<h5>• Typedef Keyword :</h5>
<p>The <code>typedef</code> keyword assigns a new name or alias to an existing data type.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>typedef type newname;</code></div>
<pre class="code-box code-box-color"><code>typedef int feet;
feet distance;
distance = 2002;
cout << "Distance = " << distance << endl;</code></pre>
</div>
</div>
<div class="sections" id="storage-classes">
<h4 class="heading"># Storage Classes</h4>
<p>The Storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ program.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>class type name</code></div>
<h6>C++ uses 5 storage classes, namely -</h6>
<ul>
<li>
<h6>Automatic :</h6>
<p>It is the default storage class for all local variables.</p>
</li>
<li>
<h6>External :</h6>
<p>It specifies that the symbol has external linkage to an external file or scope. The external storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used.</p>
</li>
<li>
<h6>Static :</h6>
<p>When a variable is declared static, a single copy of the variable is created (memory is allocated only once) and shared among all objects at the class level. All instances of the class share the same static variable. The static is a property of the class, not the object, and hence the object of the class can't use it to access it; it can be called using the class name. The value of static variables and data members persists until the end of the program.</p>
</li>
<li>
<h6>Register :</h6>
<p>Register variables are stored in the processor register instead of memory <strong>(deprecated)</strong>.</p>
</li>
<li>
<h6>Mutable :</h6>
<p>It allows a member of an object to override a const member function (applies only to class objects).</p>
</li>
</ul>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Class</th>
<th>Lifetime</th>
<th>Visibility</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>auto</code></td>
<td>function/block</td>
<td>local</td>
<td>garbage</td>
</tr>
<tr>
<td><code>extern</code></td>
<td>throughout program</td>
<td>global</td>
<td>zero</td>
</tr>
<tr>
<td><code>static</code></td>
<td>throughout program</td>
<td>local</td>
<td>zero</td>
</tr>
<tr>
<td><code>register</code></td>
<td>function/block</td>
<td>local</td>
<td>garbage</td>
</tr>
<tr>
<td><code>mutable</code></td>
<td>class</td>
<td>local</td>
<td>garbage</td>
</tr>
</tbody>
</table>
</div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>auto int x;
register int amount = 0;
static int count = 1;</code></pre>
</div>
<div class="sections" id="operators">
<h4 class="heading"># Operators</h4>
<div>
<h5>• Arithmetic Operators :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>+</code></td>
<td>Addition</td>
</tr>
<tr>
<td><code>-</code></td>
<td>Substraction</td>
</tr>
<tr>
<td><code>*</code></td>
<td>Multiplication</td>
</tr>
<tr>
<td><code>/</code></td>
<td>Division</td>
</tr>
<tr>
<td><code>%</code></td>
<td>Modulus</td>
</tr>
<tr>
<td><code>++</code></td>
<td>Increment</td>
</tr>
<tr>
<td><code>--</code></td>
<td>Decrement</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Relational Operators :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>==</code></td>
<td>Equal to</td>
</tr>
<tr>
<td><code>!=</code></td>
<td>Not equal to</td>
</tr>
<tr>
<td><code>></code></td>
<td>Greater than</td>
</tr>
<tr>
<td><code><</code></td>
<td>Less than</td>
</tr>
<tr>
<td><code>>=</code></td>
<td>Greater than or equal to</td>
</tr>
<tr>
<td><code><=</code></td>
<td>Less than or equal to</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Logical Operators :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&&</code></td>
<td>Logical and</td>
</tr>
<tr>
<td><code>||</code></td>
<td>Logical or</td>
</tr>
<tr>
<td><code>!</code></td>
<td>Logical not</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Bitwise Operators :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&</code></td>
<td>Bitwise AND</td>
</tr>
<tr>
<td><code>|</code></td>
<td>Bitwise OR</td>
</tr>
<tr>
<td><code>^</code></td>
<td>Bitwise XOR</td>
</tr>
<tr>
<td><code>~</code></td>
<td>One's Complement (unary)</td>
</tr>
<tr>
<td><code><<</code></td>
<td>Left Shift Operator</td>
</tr>
<tr>
<td><code>>></code></td>
<td>Right Shift Operator</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<h5>• Sizeof Operator :</h5>
<p>The <code>sizeof()</code> operator or function returns the amount of memory that is allocated to data types in bytes.</p>
<pre class="code-box code-box-color"><code>int x = 22;
cout << sizeof(x);</code></pre>
</div>
<div>
<h5>• Ternary Operator :</h5>
<div class="code-box"><code>[condition] ? [expr_1] : [expr_2]</code></div>
<p>If the condition is true, then expression 1 is evaluated; otherwise, expression 2 is evaluated.</p>
<pre class="code-box code-box-color"><code>int a=5, b=10, large;
large = a > b ? a : b;
cout << "Largest number is " << large << endl;</code></pre>
</div>
<div>
<h5>• Type Casting Operator :</h5>
<p>Typecasting is the conversion of one data type into another by using a casting operator.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>type(expression)</code></div>
<div class="code-box"><code>(type)expression</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>// Casting float into int:
cout << int(22.17);
cout << (int)17.22;
</code></pre>
</div>
<div>
<h5>• Membership Operator :</h5>
<p>The <strong>dot operator</strong> (<code>.</code>) and the <strong>arrow operator</strong> (<code>-></code>) are used to reference individual members of classes, structures, and unions. To access members through a pointer, the arrow operator is used.</p>
<pre class="code-box code-box-color"><code>struct Classroom {
int roll;
char name[20];
};
int main(){
// Using dot operator :
struct Classroom s = { 17, "Mandar" };
cout << "Roll: " << s.roll << "\nName: "<< s.name << endl;
// Using arrow operator (pointer) :
struct Classroom *sp = &s;
cout << "Roll: " << sp->roll << "\nName: "<< sp->name;
return 0;
}</code></pre>
</div>
<div>
<h5>• Referencing & Dereferencing Operator :</h5>
<p>The <strong>referencing operator</strong> (<code>&</code>) is used for accessing the memory location of a variable, and the <strong>dereferncing operator</strong> (<code>*</code>) is used for accessing the value stored at the memory location.</p>
<pre class="code-box code-box-color"><code>// Referencing:
int v = 22;
cout << "Address of v is " << &v;
// Dereferencing:
cout << "\nValue of v is " << *(&v);</code></pre>
</div>
<div>
<h5>• Scope Resolution Operator :</h5>
<p>The scope resolution operator (<code>::</code>) is used to access variables, functions, or classes defined in a specific scope or namespace. It can be used to define the member function outside of the class and to access the static members of a class.</p>
<pre class="code-box code-box-color"><code>int x = 100;
int main(){
int x = 50;
cout << "Local X is " << x << endl;
cout << "Global X is " << ::x << endl;
return 0;
}</code></pre>
</div>
</div>
<div class="sections" id="decision-making">
<h4 class="heading"># Decision Making</h4>
<div>
<h5>• If Statement :</h5>
<h6>Syntax :</h6>
<pre class="code-box"><code>if(condition){
// statements_if_true
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int a=10, b=20;
if(a > b){
cout << "A is larger than B";
}</code></pre>
</div>
<div>
<h5>• If-Else Statement :</h5>
<h6>Syntax :</h6>
<pre class="code-box"><code>if(condition){
// statements_if_true
} else {
// statements_if_false
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int a=10, b=20;
if(a > b){
cout << "A is greater than B";
} else {
cout << "A is smaller than B";
}</code></pre>
</div>
<div>
<h5>• Else-If Statement :</h5>
<h6>Syntax :</h6>
<pre class="code-box"><code>if(condition){
// statements_if_true
} else if (condition) {
// statements_if_true
} else {
// statements_if_false
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int a=10, b=20;
if(a > b){
cout << "A is greater than B";
} else if (a < b) {
cout << "A is smaller than B";
} else {
cout << "A is equal to B";
}</code></pre>
</div>
<div>
<h5>• Nested If-Else :</h5>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int age = 22;
if(age > 0){
if(age < 18)
cout << "You are young";
else if ( age >= 18)
cout << "You are adult";
} else {
if(age < 0)
cout << "Age cannot be negative";
else
cout << "Age cannot be zero";
}</code></pre>
</div>
<div>
<h5>• Switch Case :</h5>
<p>A switch statement allows a variable to be tested for equality against a list of values.</p>
<h6>Syntax :</h6>
<pre class="code-box"><code>switch(expression){
case constant-expression-1 :
statement(s);
break;
case constant-expression-2 :
statement(s);
break;
case constant-expression-N :
statement(s);
break;
default :
statement(s);
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int day = 3;
switch(day){
case 1 :
cout << "Mon";
break;
case 2 :
cout << "Tue";
break;
case 3 :
cout << "Wed";
break;
case 4 :
cout << "Thu";
break;
case 5 :
cout << "Fri";
break;
case 6 :
cout << "Sat";
break;
case 7 :
cout << "Sun";
break;
default :
cout << "Incorrect Day!";
}</code></pre>
</div>
</div>
<div class="sections" id="loops">
<h4 class="heading"># Loops</h4>
<div>
<h5>• While Loop :</h5>
<p>Generally, when the number of iterations is unknown, the while loop is used.</p>
<h6>Syntax :</h6>
<pre class="code-box"><code>while(condition) {
statement(s);
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int n = 1;
while(n <= 5){
cout << "Hello World";
n++;
}</code></pre>
</div>
<div>
<h5>• Do-While Loop :</h5>
<h6>Syntax :</h6>
<pre class="code-box"><code>do {
statement(s);
} while(condition);</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int n = 1;
do {
cout << "Hello World";
n++;
} while(n <= 5)</code></pre>
</div>
<div>
<h5>• For Loop :</h5>
<p>Generally, when the number of iterations is known, the for loop is used.</p>
<h6>Syntax :</h6>
<pre class="code-box"><code>for ( init; condition; increment ){
statement(s);
}</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>for(int i=1; i<=5; i++){
cout << "Hello World";
}</code></pre>
</div>
<div>
<h5>• Nested Loops :</h5>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>for(int i=1; i<=5; i++)
for(int j=1; j<=i; j++)
cout << "Hello World";</code></pre>
</div>
</div>
<div class="sections" id="loop-control-statements">
<h4 class="heading"># Loop Control Statements</h4>
<div>
<h5>• Break Statement :</h5>
<p>Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>break;</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int n = 1;
while(n <= 100){
if (n == 22) break;
cout << "Hello World";
n++;
}</code></pre>
</div>
<div>
<h5>• Continue Statement :</h5>
<p>This causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>continue;</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int n = 1;
while(n <= 100){
if (n%2 == 0) continue;
cout << "Hello World";
n++;
}</code></pre>
</div>
<div>
<h5>• Goto Statement :</h5>
<p>Transfers control to the labelled statement.</p>
<h6>Syntax :</h6>
<pre class="code-box"><code>goto label;
labal:
statement(s);</code></pre>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int n;
INPUT:
cout << "Enter Odd Number :";
cin >> n;
if (n%2 == 0) goto INPUT;</code></pre>
</div>
</div>
<div class="sections" id="strings">
<h4 class="heading"># Strings</h4>
<p>Strings are one-dimensional arrays of characters terminated by the null character '\0'. There are two ways of using strings in C++ :</p>
<ol>
<li>String as Char Array (C-style)</li>
<li>String Class (recommended)</li>
</ol>
<div>
<h5>• String as Char Array :</h5>
<p>Declaring a string as a character array as in the C language</p>
<h6>Syntax :</h6>
<div class="code-box"><code>char name[];</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>char greet[] = "Hello World";
char name[10];
name = "Mandar";</code></pre>
</div>
<div>
<h5>• String Class :</h5>
<p>The standard C++ library provides a string class. For this, we must include the library/header file <code><string></code>.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>string name;</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>#include<iostream>
#include<string>
using namespace std;
int main(){
string greet = "Hello World";
cout << greet << endl;
cout << "String Length is " << greet.length() << endl;
return 0;
}</code></pre>
</div>
<div>
<h5>• String Functions :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>str.length()</code></td>
<td>Returns the length of string</td>
</tr>
<tr>
<td><code>str.append(string)</code></td>
<td>Appends the string at the end of str.</td>
</tr>
<tr>
<td><code>str.copy(string,len,pos)</code></td>
<td>Copies c-style string of size (len) from position (pos) to str</td>
</tr>
<tr>
<td><code>str.replace(pos,len,string)</code></td>
<td>Replace string of size (len) from position (pos) in str</td>
</tr>
<tr>
<tr>
<td><code>str.compare(str2)</code></td>
<td>Returns 0 if both are the same; less than 0 if str<str2; greater than 0 if str>str2.</td>
</tr>
<tr>
<td><code>str.find(string)</code></td>
<td>Find first occurance of string in str and returns the position</td>
</tr>
<tr>
<td><code>str.substr(pos,len)</code></td>
<td>Returns sub-string of length (len) from the string from position (pos)</td>
</tr>
<tr>
<td><code>str.c_str()</code></td>
<td>Returns C string equivalent (null-terminated sequence of characters)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="arrays">
<h4 class="heading"># Arrays</h4>
<p>An array is a fixed-size sequential collection of elements of the same type. All arrays consist of contiguous memory locations. Array indexes start with 0: [0] is the first element. Arrays cannot be reinitialized.</p>
<h6>Declaring array :</h6>
<div class="code-box"><code>type name[size];</code></div>
<h6>Initializing array :</h6>
<pre class="code-box"><code>type name[N] = { element1, element2, ..., elementN };
type name[] = { element1, element2, ..., elementN };</code></pre>
<h6>Array Indexing :</h6>
<pre class="code-box"><code>array[index];</code></pre>
<h6>Size of array :</h6>
<div class="code-box"><code>sizeof(array) / sizeof(type);</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int num[] = { 1, 2, 3, 4, 5 };
int n = num[2];
int square_num[5];
int size = sizeof(square_num) / sizeof(int);
for (int i = 0; i < size; i++)
square_num[i] = (i+1)*(i+1);
for(int i=0; i < size; i++)
cout << square_num[i] << " ";</code></pre>
<div>
<h5>• Multi-dimensional Arrays :</h5>
<p>A multi-dimensional array is an array of arrays.</p>
<h6>Syntax :</h6>
<div class="code-box"><code>type name[size1][size2]...[sizeN];</code></div>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code>int arr[4][2] = { {1,2}, {3,4}, {5,6}, {7,8} };
for( int i=0; i<4; i++)
for( int j=0; j<2; j++)
cout << arr[i][j] << " ";</code></pre>
</div>
<div>