C++ OOPs and Design Patterns Notes
C++ Example Code
#include<bits/stdc++.h>
using namespace std;
class Check{
private:
int x = 0 ;
public:
Check(int x)
{
this->x = x ;
}
Check()
{
}
Check(const Check& obj)
{
this->x = obj.x ;
}
Check& operator=(const Check& obj)
{
this->x = obj.x+1 ;
}
int get(){
return x ;
}
};
int main()
{
// Checking Some concepts :
// Check obj(2) ;
// Check obj1;
// obj1 = obj ;
// cout<<obj1.get();
return 0;
}
Core OOP Concepts
**Class**: Blueprint/template defining attributes (data members) and methods
(functions).
**Object**: Instance of a class. Holds values of attributes and can call methods.
**Constructor**: Special method called when an object is created (initializes attributes).
**Destructor**: Special method called when an object is destroyed (cleanup).
Procedural vs OOP
Classic / Procedural Programming Object-Oriented Programming (OOP)
Data and functions are separate Data + functions are bundled
(Encapsulation)
Function reuse only Reuse via inheritance & polymorphism
Limited abstraction Strong abstraction via classes/interfaces
Harder to model real-world Easier to model real-world with
classes/objects
Less flexible Flexible via polymorphism & dynamic
binding
Copy Constructor & Assignment Operator
- Copy constructor is called during object creation with another object.
- Assignment operator is called after both objects are created.
- Always take `const reference` to avoid unnecessary copies.
- Passing objects by value calls copy constructor.
Const, Friend, and Static
- Const methods cannot modify instance members (except `mutable`).
- Const objects can only call const methods.
- Friend functions can access private/protected members.
- Static methods belong to the class, not objects (no `this` pointer).
Object Slicing & Casting
- **Object Slicing**: Assigning derived object to base object removes derived parts.
- **Upcasting**: Base = Derived (safe).
- **Downcasting**: Requires `dynamic_cast` for runtime safety.
- RTTI requires virtual function (polymorphic class) → Vtable, Vptr.
Polymorphism
- Compile-time (static): function/operator overloading.
- Runtime (dynamic): via virtual functions and vtable.
4 Pillars of OOP
Encapsulation: Bundle data + methods; control access with access modifiers.
Abstraction: Hide implementation details, expose only essential functionalities.
Inheritance: Child acquires attributes/methods from parent.
Polymorphism: Same method/object behaves differently (overriding, overloading).
Design Patterns
Observer Pattern: One-to-many dependency (publisher-subscriber).
Strategy Pattern: Define family of algorithms; interchangeable.
State Pattern: Object behavior changes with state.
Command Pattern: Encapsulate request as object; undo/redo supported.
Template Pattern: Define algorithm skeleton; subclasses redefine steps.
Chain of Responsibility: Pass request along chain of handlers.
Mediator Pattern: Encapsulates communication among objects.
Design Principles
DRY: Don't Repeat Yourself.
YAGNI: You Aren't Gonna Need It.
KISS: Keep It Simple, Stupid.
SOC: Separation of Concerns.
LoD: Law of Demeter (least knowledge).
Composition over Inheritance.
POLA: Principle of Least Astonishment.
High Cohesion & Low Coupling.
Fail Fast Principle: Detect errors early.
SLA: Single Level of Abstraction.
Tell, Don't Ask.
IoC: Inversion of Control.
DI: Dependency Injection.