-
Notifications
You must be signed in to change notification settings - Fork 48
/
OOP.html
704 lines (643 loc) · 23 KB
/
OOP.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OOP in C++ - The Checklist</title>
<link href="https://api.fontshare.com/v2/css?f[]=nunito@400&f[]=bebas-neue@400&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="icon" href="./resources/Yellow Abstract Cooking Fire Free Logo.png" />
<link rel="stylesheet" href="styles.css" />
<link rel="stylesheet" href="dark-mode.css" />
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/atom-one-dark.min.css" />
<link rel="stylesheet" href="scrollbar.css">
</head>
<body>
<header>
<h1><a href="index.html">The Checklist</a></h1>
</header>
<nav>
<!-- may use this thing later -->
<!-- <ul>
<li><a href="checklist1.html">Checklist 1</a></li>
<li><a href="checklist2.html">Checklist 2</a></li>
</ul> -->
<!-- Add more checklists here -->
<!-- dark mode elements -->
<button class="dark-mode-toggle">
<i class="sun-icon"></i>
<i class="moon-icon"></i>
</button>
</nav>
<main>
<section class="checklist">
<h2>Object-Oriented Programming (OOP) in C++</h2>
<ul>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
What is OOP?
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Object-Oriented Programming (OOP) is a programming paradigm that
focuses on organizing code into objects, which are instances of
classes. It emphasizes concepts such as encapsulation,
inheritance, and polymorphism to create modular and reusable code.
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Classes and Objects
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Classes are user-defined data types in C++ that encapsulate data
and functions. Objects are instances of classes. Learn how to
create classes, define member functions, and access class members
using objects.
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Constructors and Destructors
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Constructors are special member functions that initialize objects
when they are created. Destructors are used to release resources
when objects are destroyed. Learn about different types of
constructors and destructors in C++.
</div>
</li>
<!-- Add more OOP in C++ topics here -->
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Encapsulation
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Encapsulation in C++ is defined as the wrapping up of data and
information in a single unit. In Object-Oriented Programming,
Encapsulation is defined as binding together the data and the
functions that manipulate them.
<p style="text-align: left"></p>
<h1 style="text-align: left">
Two Important property of Encapsulation
</h1>
<ul style="text-align: left">
<li>
<strong>Data Protection</strong> - Encapsulation protects the
internal state of an object by keeping its data members
private. Access to and modification of these data members is
restricted to the class’s public methods, ensuring controlled
and secure data manipulation.
</li>
<li>
<strong>Information Hiding</strong> - Encapsulation hides the
internal implementation details of a class from external code.
Only the public interface of the class is accessible,
providing abstraction and simplifying the usage of the class
while allowing the internal implementation to be modified
without impacting external code.
</li>
</ul>
<p style="text-align: left"><strong>Example</strong></p>
<pre style="text-align: left">
<code>
#include <iostream>
using namespace std;
class temp{
int a;
int b;
public:
int solve(int input){
a=input;
b=a/2;
return b;
}
};
int main() {
int n;
cin>>n;
temp half;
int ans=half.solve(n);
cout<<ans<<endl;
}
</code>
</pre>
</div>
</li>
<!-- Add more OOP in C++ topics here -->
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Abstraction
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Data abstraction is one of the most essential and important
features of object-oriented programming in C++. Abstraction means
displaying only essential information and hiding the details. Data
abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or
implementation.
<h1 style="text-align: left">Types of Abstraction</h1>
<ul style="text-align: left">
<li>
<strong>Data abstraction</strong> – This type only shows the
required information about the data and hides the unnecessary
data.
</li>
<li>
<strong>Control Abstraction</strong> – This type only shows
the required information about the implementation and hides
unnecessary information.
</li>
</ul>
<p style="text-align: left"><strong>Example</strong></p>
<pre style="text-align: left">
<code>
// working of Abstraction
#include <iostream>
using namespace std;
class implementAbstraction {
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
</code>
</pre>
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Polymorphism
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of
a message to be displayed in more than one form. A person at the same time can have different
characteristics. A man at the same time is a father, a husband, and an employee. So the same person
possesses different behavior in different situations. This is called polymorphism.
<h1 style="text-align: left">Types of Polymorphism</h1>
<ul style="text-align: left">
<li>
<strong>Compile Time Polymorphism</strong> – This type of polymorphism is achieved by function
overloading or operator overloading.
<ol>
<li>
<strong>Function Overloading</strong> - When there are multiple functions with the same name but
different parameters, then the functions are said to be overloaded, hence this is known as Function
Overloading. Functions can be overloaded by changing the number of arguments or/and changing the
type of arguments. In simple terms, it is a feature of object-oriented programming providing many
functions that have the same name but distinct parameters when numerous tasks are listed under one
function name. There are certain Rules of Function Overloading that should be followed while
overloading a function.
<p style="text-align: left;">
<strong>Example</strong>
</p>
<pre>
<code>
// C++ program to demonstrate
// function overloading or
// Compile-time Polymorphism
#include <bits/stdc++.h>
using namespace std;
class Poly {
public:
// Function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// Function with same name but
// 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}
// Function with same name and
// 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y
<< endl;
}
};
// Driver code
int main()
{
Poly obj1;
// Function being called depends
// on the parameters passed
// func() is called with int value
obj1.func(7);
// func() is called with double value
obj1.func(9.132);
// func() is called with 2 int values
obj1.func(85, 64);
return 0;
}
</code>
</pre>
</li>
<li>
<strong>Operator Overloading </strong> - C++ has the ability to provide the operators with a special
meaning for a data type, this ability is known as operator overloading. For example, we can make use
of the addition operator (+) for string class to concatenate two strings. We know that the task of
this operator is to add two operands. So a single operator ‘+’, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
<pre>
<code>
// C++ program to demonstrate
// Operator Overloading or
// Compile-Time Polymorphism
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// This is automatically called
// when '+' is used with between
// two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
// An example call to "operator+"
Complex c3 = c1 + c2;
c3.print();
}
</code>
</pre>
</li>
</ol>
</li>
<li>
<strong>Runtime Polymorphism</strong> – This type only shows
the required information about the implementation and hides
unnecessary information.
<ol>
<li>
<strong>Function Overriding </strong> - Function Overriding occurs when a derived class has a
definition for one of the member functions of the base class. That base function is said to be
overridden.
<pre>
<code>
// C++ program for function overriding with data members
#include <bits/stdc++.h>
using namespace std;
// base class declaration.
class Animal {
public:
string color = "Black";
};
// inheriting Animal class.
class Dog : public Animal {
public:
string color = "Grey";
};
// Driver code
int main(void)
{
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
cout << d.color;
}
</code>
</pre>
</li>
<li>
<strong>Virtual Function </strong> - A virtual function is a member function that is declared in the
base class using the keyword virtual and is re-defined (Overridden) in the derived class.
<pre><code>
// C++ Program to demonstrate
// the Virtual Function
#include <iostream>
using namespace std;
// Declaring a Base class
class Base {
public:
// virtual function
virtual void display()
{
cout << "Called virtual Base Class function"
<< "\n\n";
}
void print()
{
cout << "Called Base print function"
<< "\n\n";
}
};
// Declaring a Child Class
class Child : public Base {
public:
void display()
{
cout << "Called Child Display Function"
<< "\n\n";
}
void print()
{
cout << "Called Child print Function"
<< "\n\n";
}
};
// Driver code
int main()
{
// Create a reference of class GFG_Base
Base* base;
Child child;
base = &child;
// This will call the virtual function
base->GFG_Base::display();
// this will call the non-virtual function
base->print();
}
</code></pre>
</li>
</ol>
</li>
</ul>
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Inheritance
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
The capability of a class to derive properties and characteristics from another class is called Inheritance.
Inheritance is one of the most important features of Object-Oriented Programming.
<ol style="text-align: left;">
<li>
<strong>Sub Class</strong>: The class that inherits properties from another class is called Sub class or
Derived Class.
</li>
<li>
<strong>Super Class</strong>: The class whose properties are inherited by a sub-class is called Base
Class or Superclass.
</li>
<li><strong>Reusability</strong>: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields and methods of
the existing class.</li>
</li>
</ol>
<p style="text-align: left;"><strong>Example</strong></p>
<pre style="text-align: left;">
<code>
// Example: define member function without argument within
// the class
#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
};
class Student : private Person {
char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
</code>
</pre>
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Dynamic Binding
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
In dynamic binding, the code to be executed in response to the function call is decided at runtime. C++ has
virtual functions to support this. Because dynamic binding is flexible, it avoids the drawbacks of static
binding, which connected the function call and definition at build time.
<p style="text-align: left;"><strong>Example</strong></p>
<pre style="text-align: left;">
<code>
// C++ Program to Demonstrate the Concept of Dynamic binding
// with the help of virtual function
#include <iostream>
using namespace std;
class Dynamic {
public:
void call_Function() // function that call print
{
print();
}
void print() // the display function
{
cout << "Printing the Base class Content" << endl;
}
};
class Dynamic2 : public Dynamic
{
public:
void print()
{
cout << "Printing the Derived class Content"
<< endl;
}
};
int main()
{
Dynamic dynamic; // Creating Dynamic object
dynamic.call_Function(); // Calling call_Function
Dynamic2 dynamic2; // creating Dynamic2 object
dynamic2.call_Function(); // calling call_Function
// for GFG2 object
return 0;
}
</code>
</pre>
</div>
</li>
<li class="checklist-item">
<label>
<input type="checkbox" class="checkbox" data-checklist="oop-cpp" />
<span class="checkmark"></span>
Message Passing
<span class="dropdown-arrow"></span>
<!-- Add the drop-down arrow icon -->
</label>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
Objects communicate with one another by sending and receiving information. A message for an object is a
request for the execution of a procedure and therefore will invoke a function in the receiving object that
generates the desired results. Message passing involves specifying the name of the object, the name of the
function, and the information to be sent.
</div>
</li>
</ul>
</section>
</main>
<!-- Footer section -->
<footer>
<!-- <div class="footer-panel1">
<a href=""> Back to top</a>
</div> -->
<div class="footer-panel2">
<div class="navFooterlinkCol">
<div class="footerlinkcol">Get to Know Us</div>
<ul>
<li class="footlink-li"><a href="#"> About</a></li>
<li class="footlink-li"><a href="#">Careers</a></li>
<li class="footlink-li"><a href="#">Documentation</a></li>
<li class="footlink-li"><a href="#">Computer Science</a></li>
</ul>
</div>
<div class="navFooterlinkCol">
<div class="footerlinkcol">Connect with Us</div>
<ul>
<li class="footlink-li"><a href="#">Facebook</a></li>
<li class="footlink-li"><a href="#">Twitter</a></li>
<li class="footlink-li"><a href="#">Instagram</a></li>
</ul>
</div>
<div class="navFooterlinkCol">
<div class="footerlinkcol">Learn Subjects with Us</div>
<ul>
<li class="footlink-li"><a href="C_Programming.html">C Programming</a></li>
<li class="footlink-li"><a href="C++_Basics.html">C++ Basics</a></li>
<li class="footlink-li"><a href="python_Basics.html">Python Basics</a></li>
<li class="footlink-li"><a href="OOP.html">Object Oriented Programming</a></li>
<li class="footlink-li"><a href="STL.html">Standard Template Library</a></li>
<li class="footlink-li"><a href="WebApp.html">Web Application Fundamentals</a></li>
<li class="footlink-li"><a href="algorithm.html">Algorithm Design Techniques</a></li>
<li class="footlink-li"><a href="JavaBasic.html">Java Basics</a></li>
<li class="footlink-li"><a href="linux.html">Linux Basics</a></li>
<li class="footlink-li"><a href="golang.html">Golang Basics</a></li>
<li class="footlink-li"><a href="frontendProjects.html">Frontend Projects</a></li>
</ul>
</div>
</div>
<div class="footer-panel4">
<div class="pages">
<div class="copyright">
Copyright © <span id="year"></span>, Ujjwal Sharma All Rights Reserved
</div>
</div>
</div>
</footer>
<!-- Scroll back Button -->
<button onclick="topFunction()" id="myBtn" title="Go to top"><i class='fas fa-arrow-circle-up'
style='font-size:36px'></i></button>
<!-- End of scroll back btton -->
<!-- End Footer section -->
<!-- Javascript -->
<script>
document.getElementById('year').innerHTML = new Date().getFullYear();
</script>
<script src="script.js"></script>
<script src="dark-mode.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script>
<script src="scrollbar.js"></script>
<script>
hljs.highlightAll();
</script>
</body>
</html>