0% found this document useful (0 votes)
45 views27 pages

Oops 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views27 pages

Oops 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In Data Structures and Algorithms (DSA) and Competitive

Programming (CP), the focus is primarily on problem-solving


using logical and mathematical thinking, whereas Object-
Oriented Programming (OOP) is more about designing software
systems using abstraction, encapsulation, inheritance, and
polymorphism.
Examples:

DSA/CP Approach (Logical Thinking):

Problem: "Find the shortest path in a maze."

Solution: Use BFS (Breadth-First Search) with a queue.

Focus: Optimal path, time complexity (O(N*M)).

OOP Approach (Design Thinking):

Problem: "Design a game with characters and weapons."

Solution: Create Character and Weapon classes with


inheritance (e.g., Warrior, Wizard).

Focus: Code reusability, encapsulation (private/protected


methods).
When to Use Which?

DSA/CP Thinking:

Needed in interviews (FAANG), CP contests (Codeforces,


LeetCode).

Used when optimization & correctness matter most.

OOP Thinking:

Needed in software development (backend, game dev).

Used when scalability, maintainability, and teamwork


matter.

🚀
Conclusion:
DSA/CP Logic, math, efficiency.

OOP Structure, design patterns, maintainability.

Both are important, but DSA/CP thinking is more about


"how to solve," while OOP is about "how to structure."
Theory concepts
Examples
Important definitions
Object Oriented Programming

most important for interviews (Definitions + Examples)

we solve real life problems using c++ oops concepts

Example we have -> vectors, strings, stacks,


priority_queue (STL) due to oops .

internally used oops concept to build this data structures


Classes & Objects
objects are entities in the real world

class is like a blueprint of these entities

objects -> pen, laptop etc

class -> blueprint


example : toyota made one design of car and with that
design they started building all the other cars
Example 2 : take a system like college

A teacher ->[name , deptartment , salary , subject ] -> properties


->changeDepartment() -> mathods
Access Modifiers

private : data & methods accessible inside class

public : data & methods accessible to everyone

protected : data & mathods accessible inside class & to its


derived class
Encapsulation

-> Encapsulation is wrapping up of data & members


functions in a single unit is called class. (data hiding)

oops major pillors


[Link]
[Link]
[Link]
[Link]
Constructor

special method invoked automatically at time of object creation.


used for initialisation.
1. same name as class

2. constructor doesn't have return type

3. only called once (automatically), at object creation

4. memory allocation happens when constructor is called


Constructor

Three Types Constructor

1. Non parameterised

2. Parameterised

3. Copy Constructor
Copy Constructor

Special Constructor (default) used to copy properties of


one object into another
Constructor

this is a special pointer in C++ that points to the current object

this->prop is same as *(this).prop

Example :
int x = 10;
int *ptr = &x
*ptr is nothing but 10 -> * Dereferencing operator
Shallow & Deep Copy

A Shallow copy of an object copies all of the member values from one object to
another

A deep Copy, on other hand, not only copies the member values but also makes
copies of any dynamically allocated memory that the members point to.
Inline in C++

Definition: The inline keyword is used to suggest the compiler to insert the
complete function code at the place of its call, instead of performing a regular
function call.
Why We Need Inline:

Reduces function call overhead.


Useful for small, frequently called functions.
Can speed up execution time.

When Not to Use:

For large functions (increases binary size).


With recursion or loops inside the function.
Static Member Variable in C++

Definition: A static member variable is shared among all objects of a class. It is


initialized only once and has a single memory location.

Static Member Function in C++

Definition: A static member function belongs to the class, not to any specific
object. It can only access static members of the class.
Key Points:
Can be called without creating an object.
Cannot access non-static members.
Useful for utility functions and accessing static data.
Array of Objects

You might also like