Complete Notes: Object-Oriented Programming Concepts
1. Note-Taking During OOP Study
It's recommended to take brief keyword notes while studying and then elaborate after completing topics.
- During reading/watching: jot down key terms only.
- After finishing topic: write in-depth explanations with examples.
- Regular review: consolidate into summary pages with insights or bugs learned.
2. Object-Oriented Language Meaning
A language is called object-oriented if it supports:
- Classes and Objects
- Encapsulation
- Inheritance
- Polymorphism
Examples: C++, Java, Python. C is not OOP as it lacks class-based abstraction.
3. Static vs Dynamic Object Creation
- Static: Created on the stack. Lifetime is scope-bound. Example: MyClass obj;
- Dynamic: Created on the heap using new/malloc. Lifetime controlled manually. Example: MyClass* obj =
new MyClass();
4. Stack vs Heap Memory
- Stack: Fast, automatic cleanup, used for static/local variables.
- Heap: Slower, manual memory management, used for dynamic allocation.
- new/delete (C++) and malloc/free (C) allocate on heap.
5. Class vs Structure
- Struct defaults to public members; class defaults to private.
- Both can have functions, inheritance, and private members.
- Historically, structs were used for passive data-only types in C.
Complete Notes: Object-Oriented Programming Concepts
6. Virtual Functions and Polymorphism
- Virtual functions enable runtime polymorphism.
- Requires base class pointer to call overridden function in derived class.
- Implemented using vtables (virtual table).
- Without 'virtual', static (compile-time) binding happens.
Dynamic dispatch: decision made at runtime based on actual object type.
7. Object Slicing
Occurs when a derived class object is assigned to a base class object (not pointer).
Only base part is copied; derived part is sliced off. Example:
Base b = Derived(); // slicing happens, only base portion exists in 'b'
8. Copy Constructor: Default vs User-Defined
- Default copy constructor: member-wise (shallow) copy.
- User-defined copy constructor is needed when class manages dynamic memory or resources.
Otherwise, multiple objects may point to same memory -> double deletion.
9. Ownership of Resources
- Ownership = who is responsible for creating, using, and freeing a resource.
- Default copy copies pointers -> shared ownership -> risk.
- Deep copy: each object has its own resource.
- STL containers like vector handle this safely by deep copying internally.
10. Move Semantics
- Move constructor allows transferring ownership of resource instead of copying.
- Introduced in C++11 for performance and safety.
- Syntax: MyClass(MyClass&& other) { ptr = other.ptr; other.ptr = nullptr; }
- std::move enables use of move constructor by converting lvalue to rvalue.
- No allocation or copy; just pointer reassignment.