-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodelab-cpp.html
More file actions
1149 lines (951 loc) Β· 43.4 KB
/
codelab-cpp.html
File metadata and controls
1149 lines (951 loc) Β· 43.4 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
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">
<title>Ai CodeLab - Core Documentation</title>
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Modern Styles -->
<link rel="stylesheet" href="styles.css">
<style>
.study-grid {
display: grid;
grid-template-columns: 3fr 1fr;
gap: 3rem;
max-width: 1400px;
margin: 0 auto;
}
.study-content {
background: linear-gradient(135deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
border: 1px solid var(--glass-border);
border-radius: 20px;
padding: 3rem;
backdrop-filter: blur(20px);
color: var(--text-pure);
}
.study-content h1, .study-content h2, .study-content h3 {
color: var(--secondary);
margin-top: 2.5rem;
margin-bottom: 1rem;
border-bottom: 1px solid var(--glass-border);
padding-bottom: 0.5rem;
}
.study-content h1 {
color: var(--primary);
font-size: 3rem;
}
.study-content p, .study-content li {
margin-bottom: 1.2rem;
color: var(--text-muted);
line-height: 1.8;
font-size: 1.05rem;
}
.study-content ul, .study-content ol {
margin-left: 2rem;
margin-bottom: 2rem;
}
.study-content pre {
background: rgba(0,0,0,0.6);
border: 1px solid var(--glass-border);
padding: 1.5rem;
border-radius: 10px;
overflow-x: auto;
margin-bottom: 2rem;
color: var(--accent);
font-family: monospace;
box-shadow: inset 0 0 20px rgba(0,0,0,0.5);
}
.study-sidebar {
background: linear-gradient(135deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
border: 1px solid var(--glass-border);
border-radius: 20px;
padding: 2rem;
backdrop-filter: blur(20px);
position: sticky;
top: 100px;
height: fit-content;
}
.study-sidebar h2 {
color: var(--primary);
margin-bottom: 1.5rem;
border-bottom: 1px solid var(--glass-border);
padding-bottom: 0.5rem;
font-size: 1.2rem;
}
.study-sidebar a {
display: flex;
align-items: center;
gap: 1rem;
padding: 0.5rem 0;
color: var(--text-muted);
transition: all 0.3s;
}
.study-sidebar a:hover {
color: var(--secondary);
transform: translateX(5px);
}
@media (max-width: 900px) {
.study-grid { grid-template-columns: 1fr; }
.study-sidebar { position: relative; top: 0; }
}
</style>
</head>
<body>
<!-- Custom Cursor -->
<div id="custom-cursor"></div>
<!-- Animated Mesh Gradient Background -->
<div class="mesh-bg">
<div class="blob blob-1"></div>
<div class="blob blob-2"></div>
<div class="blob blob-3"></div>
</div>
<!-- Modern Extreme Navbar -->
<nav>
<div class="logo-container">
<i class="fas fa-robot"></i>
<span>CodeLab<span style="color:var(--primary)">.ai</span></span>
</div>
<div class="nav-links">
<a href="index.html">Platform</a>
<a href="courses.html">Syllabus</a>
<a href="study.html" class="active">Resources</a>
<a href="join.html">Community</a>
</div>
<div style="display:flex;gap:10px;">
<a href="login.html" class="magnetic-btn" style="padding: 0.5rem 1.5rem; font-size: 0.9rem;"><span>Login</span></a>
</div>
</nav>
<!-- Page Title Header -->
<header style="margin-top: 120px; text-align: center; padding: 2rem;">
<h1 class="hover-target" style="font-size: clamp(2.5rem, 5vw, 4rem);"><span class="gradient-text">Protocol</span> Documentation</h1>
</header>
<main style="padding: 2rem;">
<div class="study-grid">
<div class="study-content reveal hover-target" data-tilt>
<!-- intro -->
<h1 id="introduction">Introduction to C++</h1>
<p>C++ is a high-performance, compiled programming language known for its efficiency and flexibility. Created by <strong>Bjarne Stroustrup</strong> in 1983, C++ is widely used in system programming, game development, competitive programming, embedded systems, and more.</p>
<h2 id="why-learn-cpp">Why Learn C++?</h2>
<ul>
<li><strong>High Performance</strong> β Faster execution compared to interpreted languages.</li>
<li><strong>Versatile</strong> β Used for game development, operating systems, and real-time applications.</li>
<li><strong>Object-Oriented</strong> β Supports OOP principles like encapsulation and inheritance.</li>
<li><strong>Strong Community Support</strong> β Extensive resources, libraries, and frameworks.</li>
<li><strong>Career Opportunities</strong> β In demand for software development and system programming.</li>
<li><strong>Cross-Platform</strong> β Runs on Windows, macOS, and Linux.</li>
</ul>
<!-- Installing -->
<h2 id="installing-cpp">Installing C++</h2>
<ol>
<li>Download and install a C++ compiler such as <a href="https://sourceforge.net/projects/mingw/">MinGW</a> (Windows) or use <strong>g++</strong> (Linux/macOS).</li>
<li>Verify installation by running:</li>
<pre><code>g++ --version</code></pre>
<li>Use an IDE like <strong>Code::Blocks</strong>, <strong>Visual Studio Code</strong>, or <strong>Dev-C++</strong> for writing C++ programs.</li>
</ol>
<!-- Basics -->
<h2 id="cpp-basics">C++ Basics</h2>
<h3 id="hello-world">Hello World Program</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
</code></pre>
<p><strong id="variables-data-types">Variables and Data Types in C++</strong></p>
<p>In C++, a <strong>variable</strong> is a named storage location in memory. You must declare a variable with its <strong>data type</strong> before using it. For example, <code>string name = "Alice";</code> stores the text "Alice" in the variable <code>name</code>. C++ has various data types:</p>
<p><strong>Integer:</strong> Whole numbers e.g., <code>int x = 10;</code> </p>
<p><strong>Float:</strong> Decimal numbers e.g., <code>float price = 10.5;</code></p>
<p><strong>Double:</strong> More precise decimal numbers e.g., <code>double pi = 3.14159;</code></p>
<p><strong>Character:</strong> Single character e.g., <code>char grade = 'A';</code></p>
<p><strong>Boolean:</strong> True or False values e.g., <code>bool isRaining = false;</code></p>
<p>C++ is a <strong>statically typed</strong> language, meaning you must specify the data type when declaring a variable. You can use the <code>typeid()</code> function (from the <code>typeinfo</code> library) to check a variableβs type.</p>
<pre><code>#include <iostream>
using namespace std;
int main() {
string name = "Shifa";
int age = 20;
bool isStudent = true;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Is Student: " << isStudent << endl;
return 0;
}
</code></pre>
<!-- input ouput and cntrol statements -->
<h3 id="input-output">Input and Output</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
return 0;
}
</code></pre>
<h2 id="control-statements">Control Statements in C++</h2>
<p>Control statements help us <strong>control the flow of a program</strong> based on conditions and loops. There are two main types:</p>
<ul>
<li><strong>If-Else Statements</strong> β Used to make decisions</li>
<li><strong>Loops</strong> β Used to repeat a block of code</li>
</ul>
<h3 id="if-else">1οΈβ£ If-Else Statement</h3>
<p>An <strong>if-else statement</strong> checks a condition and decides what to do based on whether the condition is <strong>true or false</strong>.</p>
<pre><code>#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are a minor." << endl;
}
return 0;
}
</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li>The program asks the user to enter their age.</li>
<li>The <code>if</code> condition checks if the age is <strong>18 or more</strong>.</li>
<li>If <strong>true</strong>, it prints <em>"You are an adult."</em></li>
<li>If <strong>false</strong>, it prints <em>"You are a minor."</em></li>
</ul>
<p>π‘ <strong>Use case:</strong> If-Else is useful when making <strong>decisions</strong>, like checking if a user is old enough to vote.</p>
<hr>
<!-- operators -->
<h2 id="operators">Operators in C++</h2>
<p>Operators in C++ are symbols that perform operations on variables and values. C++ supports different types of operators.</p>
<h3>πΉ Types of Operators in C++</h3>
<ul>
<li><strong>Arithmetic Operators</strong></li>
<li><strong>Relational (Comparison) Operators</strong></li>
<li><strong>Logical Operators</strong></li>
<li><strong>Bitwise Operators</strong></li>
<li><strong>Assignment Operators</strong></li>
<li><strong>Increment & Decrement Operators</strong></li>
<li><strong>Conditional (Ternary) Operator</strong></li>
</ul>
<h3>π 1. Arithmetic Operators</h3>
<p>These operators perform mathematical operations.</p>
<table border="1">
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>+</code></td>
<td>Addition</td>
<td><code>a + b</code></td>
</tr>
<tr>
<td><code>-</code></td>
<td>Subtraction</td>
<td><code>a - b</code></td>
</tr>
<tr>
<td><code>*</code></td>
<td>Multiplication</td>
<td><code>a * b</code></td>
</tr>
<tr>
<td><code>/</code></td>
<td>Division</td>
<td><code>a / b</code></td>
</tr>
<tr>
<td><code>%</code></td>
<td>Modulus (Remainder)</td>
<td><code>a % b</code></td>
</tr>
</table>
<h3>π 2. Relational (Comparison) Operators</h3>
<p>These operators compare two values.</p>
<table border="1">
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>==</code></td>
<td>Equal to</td>
<td><code>a == b</code></td>
</tr>
<tr>
<td><code>!=</code></td>
<td>Not equal to</td>
<td><code>a != b</code></td>
</tr>
<tr>
<td><code>></code></td>
<td>Greater than</td>
<td><code>a > b</code></td>
</tr>
<tr>
<td><code><</code></td>
<td>Less than</td>
<td><code>a < b</code></td>
</tr>
<tr>
<td><code>>=</code></td>
<td>Greater than or equal to</td>
<td><code>a >= b</code></td>
</tr>
<tr>
<td><code><=</code></td>
<td>Less than or equal to</td>
<td><code>a <= b</code></td>
</tr>
</table>
<h3>π 3. Logical Operators</h3>
<p>These operators are used to combine conditional expressions.</p>
<table border="1">
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>&&</code></td>
<td>Logical AND</td>
<td><code>(a > 5) && (b < 10)</code></td>
</tr>
<tr>
<td><code>||</code></td>
<td>Logical OR</td>
<td><code>(a > 5) || (b < 10)</code></td>
</tr>
<tr>
<td><code>!</code></td>
<td>Logical NOT</td>
<td><code>!(a == b)</code></td>
</tr>
</table>
<h3>π 4. Bitwise Operators</h3>
<p>These operators perform operations at the bit level.</p>
<table border="1">
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>&</code></td>
<td>Bitwise AND</td>
<td><code>a & b</code></td>
</tr>
<tr>
<td><code>|</code></td>
<td>Bitwise OR</td>
<td><code>a | b</code></td>
</tr>
<tr>
<td><code>^</code></td>
<td>Bitwise XOR</td>
<td><code>a ^ b</code></td>
</tr>
<tr>
<td><code>~</code></td>
<td>Bitwise NOT</td>
<td><code>~a</code></td>
</tr>
<tr>
<td><code><<</code></td>
<td>Left Shift</td>
<td><code>a << 1</code></td>
</tr>
<tr>
<td><code>>></code></td>
<td>Right Shift</td>
<td><code>a >> 1</code></td>
</tr>
</table>
<h3>π 5. Assignment Operators</h3>
<p>These operators assign values to variables.</p>
<table border="1">
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td><code>=</code></td>
<td>Assign</td>
<td><code>a = b</code></td>
</tr>
<tr>
<td><code>+=</code></td>
<td>Add and assign</td>
<td><code>a += b</code></td>
</tr>
<tr>
<td><code>-=</code></td>
<td>Subtract and assign</td>
<td><code>a -= b</code></td>
</tr>
<tr>
<td><code>*=</code></td>
<td>Multiply and assign</td>
<td><code>a *= b</code></td>
</tr>
<tr>
<td><code>/=</code></td>
<td>Divide and assign</td>
<td><code>a /= b</code></td>
</tr>
<tr>
<td><code>%=</code></td>
<td>Modulus and assign</td>
<td><code>a %= b</code></td>
</tr>
</table>
<h3>π 6. Increment & Decrement Operators</h3>
<p>These operators increase or decrease a variableβs value.</p>
<ul>
<li><code>++</code> (Increment) β Increases value by 1</li>
<li><code>--</code> (Decrement) β Decreases value by 1</li>
</ul>
<h3>π 7. Conditional (Ternary) Operator</h3>
<p>A shorthand way of writing an if-else statement.</p>
<pre><code>result = (a > b) ? a : b;</code></pre>
<h3>β
Summary</h3>
<p>Operators in C++ are essential for performing calculations, comparisons, and logical operations. Understanding them is key to writing efficient C++ programs.</p>
<!-- Constructor & Distrutor -->
<h2 id="constructors-destructors">Constructors and Destructors in C++</h2>
<p>In C++, <strong>constructors</strong> and <strong>destructors</strong> are special member functions used for initializing and cleaning up objects automatically.</p>
<h3>πΉ What is a Constructor?</h3>
<ul>
<li>A <strong>constructor</strong> is a special function that is automatically called when an object is created.</li>
<li>It has the same name as the class and does not have a return type.</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
// Constructor
Person(string n, int a) {
name = n;
age = a;
cout << "Constructor called for " << name << endl;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p1("Alice", 25);
p1.display();
return 0;
}
</code></pre>
<h3>πΉ What is a Destructor?</h3>
<ul>
<li>A <strong>destructor</strong> is a special function that is automatically called when an object is destroyed.</li>
<li>It has the same name as the class but is preceded by a tilde (<code>~</code>).</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
class Person {
public:
string name;
// Constructor
Person(string n) {
name = n;
cout << "Constructor called for " << name << endl;
}
// Destructor
~Person() {
cout << "Destructor called for " << name << endl;
}
};
int main() {
Person p1("Bob");
return 0;
}
</code></pre>
<h3>πΉ Types of Constructors</h3>
<h4>1οΈβ£ Default Constructor</h4>
<p>A constructor with no parameters.</p>
<pre><code>#include <iostream>
using namespace std;
class Example {
public:
Example() { // Default Constructor
cout << "Default Constructor called!" << endl;
}
};
int main() {
Example obj; // Constructor is called automatically
return 0;
}
</code></pre>
<h4>2οΈβ£ Parameterized Constructor</h4>
<p>A constructor that takes arguments.</p>
<pre><code>#include <iostream>
using namespace std;
class Car {
public:
string brand;
// Parameterized Constructor
Car(string b) {
brand = b;
cout << "Car brand: " << brand << endl;
}
};
int main() {
Car c1("Toyota");
Car c2("Honda");
return 0;
}
</code></pre>
<h4>3οΈβ£ Copy Constructor</h4>
<p>A constructor that copies values from one object to another.</p>
<pre><code>#include <iostream>
using namespace std;
class Number {
public:
int value;
// Parameterized Constructor
Number(int v) {
value = v;
}
// Copy Constructor
Number(const Number &obj) {
value = obj.value;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Number num1(10);
Number num2 = num1; // Calls copy constructor
num2.display();
return 0;
}
</code></pre>
<h3>β
Why Use Constructors and Destructors?</h3>
<ul>
<li><strong>Automatic Initialization</strong> β Constructors initialize objects automatically.</li>
<li><strong>Memory Management</strong> β Destructors free memory when objects are destroyed.</li>
<li><strong>Code Simplicity</strong> β Avoids manual setup and cleanup of objects.</li>
</ul>
<h3>π Demonstrating Constructor and Destructor Execution</h3>
<p>Let's see how constructors and destructors work together.</p>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
class Demo {
public:
// Constructor
Demo() {
cout << "Constructor executed!" << endl;
}
// Destructor
~Demo() {
cout << "Destructor executed!" << endl;
}
};
int main() {
cout << "Creating an object..." << endl;
Demo obj; // Constructor is called
cout << "Object is about to be destroyed..." << endl;
return 0; // Destructor is called automatically
}
</code></pre>
<p>Understanding constructors and destructors in C++ helps in efficient memory management and simplifies object initialization!</p>
<!-- loopps in C++ -->
<h3 id="loops">2οΈβ£ Loops in C++</h3>
<p>Loops are used to <strong>repeat</strong> a block of code multiple times. C++ has three main loops:</p>
<h4 id="for-loop">πΉ For Loop</h4>
<p>A <strong>for loop</strong> is used when we know <strong>how many times</strong> we want to repeat something.</p>
<pre><code>#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
return 0;
}
</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li><code>int i = 0;</code> initializes the loop variable.</li>
<li><code>i < 5;</code> is the condition that keeps the loop running.</li>
<li><code>i++;</code> increases <code>i</code> by 1 in each iteration.</li>
<li>The loop runs 5 times, printing numbers <strong>0 to 4</strong> one by one.</li>
</ul>
<p>π‘ <strong>Use case:</strong> Use a <strong>for loop</strong> when you want to repeat a task a set number of times, like printing numbers or iterating over an array.</p>
<hr>
<h4 id="while-loop">πΉ While Loop</h4>
<p>A <strong>while loop</strong> runs as long as a condition is <strong>true</strong>.</p>
<pre><code>#include <iostream>
using namespace std;
int main() {
int x = 0;
while (x < 5) {
cout << x << endl;
x++;
}
return 0;
}
</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li>The loop starts with <code>x = 0</code>.</li>
<li>The condition <code>x < 5</code> is checked before each loop run.</li>
<li>Inside the loop, <code>x</code> is printed, and then <strong>increased by 1</strong> <code>x++;</code>.</li>
<li>The loop stops when <code>x</code> becomes <strong>5</strong>.</li>
</ul>
<p>π‘ <strong>Use case:</strong> Use a <strong>while loop</strong> when you <strong>don't know how many times</strong> the loop should run, like waiting for user input.</p>
<hr>
<!-- Functions -->
<h2 id="functions">Functions in C++</h2>
<p>A <strong>function</strong> in C++ is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can use functions to make our programs shorter and easier to understand.</p>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
// Function definition
string greet(string name) {
return "Hello, " + name;
}
int main() {
cout << greet("Shifa") << endl;
return 0;
}
</code></pre>
<h3>πΉ How This Code Works?</h3>
<ul>
<li>The function <code>greet(string name)</code> is defined before <code>main()</code>.</li>
<li>It takes one <strong>input parameter (name)</strong>.</li>
<li>Inside the function, it returns <strong>"Hello, " + name</strong>, which means it adds the name to "Hello, ".</li>
<li>We call the function using <code>cout << greet("Shifa")</code>.</li>
<li>This prints <strong>"Hello, Shifa"</strong> on the screen.</li>
</ul>
<h3>β
Why Use Functions?</h3>
<ul>
<li>β <strong>Reusability:</strong> We can call the function many times instead of writing the same code again.</li>
<li>β <strong>Better Readability:</strong> Code looks cleaner and is easier to understand.</li>
<li>β <strong>Easier to Fix Errors:</strong> If something goes wrong, we only need to change the function instead of the entire program.</li>
</ul>
<h3>π Example: Calling the Function Multiple Times</h3>
<pre><code>#include <iostream>
using namespace std;
string greet(string name) {
return "Hello, " + name;
}
int main() {
cout << greet("Alice") << endl;
cout << greet("Bob") << endl;
cout << greet("Charlie") << endl;
return 0;
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code>Hello, Alice
Hello, Bob
Hello, Charlie</code></pre>
<p>This shows how functions help us avoid repeating code! π―</p>
<!-- Arrays and Pointers -->
<h2 id="arrays-pointers">Arrays and Pointers in C++</h2>
<p>In C++, <strong>arrays</strong> and <strong>pointers</strong> are closely related. Arrays store multiple elements in a contiguous memory block, while pointers allow direct memory access and manipulation.</p>
<h3>πΉ What are Arrays?</h3>
<ul>
<li><strong>Array</strong> β A collection of elements of the same type stored in memory.</li>
<li>Arrays use <strong>zero-based indexing</strong>, meaning the first element is at index <code>0</code>.</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
cout << "First element: " << numbers[0] << endl;
cout << "Second element: " << numbers[1] << endl;
return 0;
}
</code></pre>
<h3>πΉ What are Pointers?</h3>
<ul>
<li><strong>Pointer</strong> β A variable that stores the memory address of another variable.</li>
<li>Pointers allow efficient memory manipulation and dynamic allocation.</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
int num = 10;
int* ptr = # // Pointer stores address of num
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Pointer holds address: " << ptr << endl;
cout << "Value at pointer: " << *ptr << endl; // Dereferencing
return 0;
}
</code></pre>
<h3>πΉ Relationship Between Arrays and Pointers</h3>
<ul>
<li>In C++, an <strong>array name</strong> acts as a pointer to the first element.</li>
<li>Using pointer arithmetic, we can traverse the array efficiently.</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int* ptr = arr; // Points to first element
cout << "First element: " << *ptr << endl;
cout << "Second element: " << *(ptr + 1) << endl;
return 0;
}
</code></pre>
<h3>β
Benefits of Using Pointers with Arrays</h3>
<ul>
<li><strong>Efficiency</strong> β Direct memory access speeds up operations.</li>
<li><strong>Flexibility</strong> β Enables dynamic memory allocation.</li>
<li><strong>Optimized Code</strong> β Reduces redundancy in accessing elements.</li>
</ul>
<h3>π Dynamic Memory Allocation</h3>
<p>We can use pointers to create arrays dynamically at runtime using <code>new</code>.</p>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
int main() {
int* arr = new int[3]; // Dynamic array allocation
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
cout << "Dynamic Array: " << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;
delete[] arr; // Free allocated memory
return 0;
}
</code></pre>
<p>Understanding arrays and pointers in C++ helps in optimizing memory usage and writing efficient programs!</p>
<!-- Structures and Classes -->
<h2 id="structures-classes">Structures and Classes in C++</h2>
<p>In C++, both <strong>structures</strong> (<code>struct</code>) and <strong>classes</strong> (<code>class</code>) are used to group related variables and functions. The main difference is that <code>struct</code> members are public by default, while <code>class</code> members are private by default.</p>
<h3>πΉ What is a Structure?</h3>
<ul>
<li>A <strong>structure</strong> (<code>struct</code>) is a user-defined data type that groups different variables.</li>
<li>By default, all members of a <code>struct</code> are <strong>public</strong>.</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person p1 = {"Alice", 25};
cout << "Name: " << p1.name << endl;
cout << "Age: " << p1.age << endl;
return 0;
}
</code></pre>
<h3>πΉ What is a Class?</h3>
<ul>
<li>A <strong>class</strong> is an advanced version of a structure with <strong>private</strong> members by default.</li>
<li>It can have both <strong>data members</strong> (variables) and <strong>member functions</strong> (methods).</li>
</ul>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
void setData(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Person p1;
p1.setData("Bob", 30);
p1.display();
return 0;
}
</code></pre>
<h3>πΉ Differences Between Structures and Classes</h3>
<table border="1">
<tr>
<th>Feature</th>
<th>Structure (<code>struct</code>)</th>
<th>Class (<code>class</code>)</th>
</tr>
<tr>
<td>Default Access Modifier</td>
<td>Public</td>
<td>Private</td>
</tr>
<tr>
<td>Encapsulation</td>
<td>Limited</td>
<td>Full (Data Hiding)</td>
</tr>
<tr>
<td>Usage</td>
<td>Simple Data Grouping</td>
<td>Object-Oriented Programming (OOP)</td>
</tr>
</table>
<h3>β
Why Use Classes?</h3>
<ul>
<li><strong>Encapsulation</strong> β Keeps data secure by using private members.</li>
<li><strong>Reusability</strong> β We can create multiple objects from a single class.</li>
<li><strong>Scalability</strong> β Classes support inheritance and polymorphism.</li>
</ul>
<h3>π Creating Multiple Objects</h3>
<p>We can create multiple objects using a class, just like with a structure.</p>
<h3>π Example Code:</h3>
<pre><code>#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
void setData(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};