C++ SIG DAY - 5
Pehle Promotion.
OOPs
Inheritance:
Inheritance is a fundamental concept in Object-Oriented Programming
(OOP) where a new class (derived class) acquires properties and behaviors
(data members and methods) of an existing class (base class).
Purpose: Inheritance promotes code reusability, scalability, and
maintenance.
🤫🤫 sabke
Example: Car is-a Vehicle, Dog is-a Animal, Papa ki property (
mein thodi thodi inherit hogi).
OOPs
Access Specifiers:
Public
Private
Protected
Inheritance
Types of Inheritance:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
OOPs
Polymorphism:
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.
Purpose: Reusability, Scalability
Example: A person (father, husband, etc.), ek friend (Infront of crush and
when his/her crush is not around) - behavior
Polymorphism
Types of Polymorphism:
Compile time Polymorphism (static or early binding):
This type of polymorphism is achieved by function overloading or
operator overloading.
Function overloading: Function with ame name but different
parameters, return time, etc.
Operator Overloading: Add sign(+) for strings and numbers, “<<” & “>>”
operator.
Polymorphism
Runtime Polymorphism (dynamic or late binding):
This type of polymorphism is achieved by function over-riding.
Function overriding: Derived class has a definition for one of the
member functions of the base class. That base function is said to be
overridden.
Why dynamic??? related to objects and its creation.
Why static??? related to coding style.
Polymorphism
Virtual Functions:
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.
Dynamic in nature.
“virtual” Keyword.
Called during Runtime Execution.
Searching
Linear Search:
Description: In a linear search, each element of the list is checked one by one
until the target element is found or the entire list is traversed.
Process: Start from the first element and compare it with the target. Move to the
next element if they don’t match.
Time Complexity: O(n)
Use Case: Works on both sorted and unsorted lists.
Linear Search
Searching
Binary Search:
Description: Binary search works on sorted arrays. It repeatedly divides the search
interval in half. If the target is less than the middle element, search in the left half;
otherwise, search in the right half.
Process:
Compare the target value with the middle element.
If they match, return the index.
If the target is smaller, search the left half; if larger, search the right half.
Time Complexity: O(log n)
Use Case: Efficient for large, sorted datasets.
Binary Search
Sorting
Bubble Sort:
Description: Bubble sort repeatedly steps through the list, compares adjacent pairs,
and swaps them if they are in the wrong order. The process is repeated until the list
is sorted.
Process:
a. Start from the first element.
b. Compare adjacent elements and swap if necessary.
c. Move to the next pair, and repeat until no swaps are needed.
Time Complexity: O(n²)
Sorting
Selection Sort:
Description: Selection sort works by repeatedly finding the minimum element from
the unsorted part of the list and swapping it with the first unsorted element.
Process:
1. Find the smallest element in the unsorted part of the array.
2. Swap it with the first unsorted element.
3. Repeat for the rest of the list.
Time Complexity: O(n²)
Sorting
Link for Visualization :
https://visualgo.net/en/sorting
More Optimized Algorithms :
1. Merge Sort
2. Quick Sort