Core Object-Oriented Programming Principles
Encapsulation
Encapsulation bundles data and methods into a single unit, restricting direct access to internal state. External code interacts through defined interfaces, enhancing security and reliability.
Inheritance
Inheritance enables classes to acquire properties and behaviors from parent classes, promoting code reuse and maintainability.
Polymorphism
Polymorphism allows different objects to respond differently to the same message, implemented through virtual functions and templates for runtime flexibility.
Key Differences Between C and C++
C++ extends C with object-oriented capabilities, enhanced standard libraries, namespaces, exception handling, and operator overloading. C++ supports classes, inheritance, and polymorphism, while C remains procedural.
Pointers and Memory Management
Pointer Fundamentals
Pointers store memory addresses and enable direct memory manipulation. Mastery is essential for efficient C++ programming.
Reference Variables
References act as aliases to existing variables, providing safer alternatives to pointers without null assignment or pointer arithmetic.
Function Overloading vs. Overriding
Overloading: Multiple functions with same name but different parameters within the same scope (compile-time polymorphism).
Overriding: Redefining base class virtual functions in derived classes (runtime polymorphism).
Inline Functions
Inline functions expand at call sites, eliminating function call overhead. Suitable for small, frequent called functions.
inline int calculateArea(int width, int height) {
return width * height;
}
Preventing Dangling Pointers
- Initialize pointers immediately
- Set to nullptr after deletion
- Avoid double-free errors
- Prevent scope violations
int* dataPtr = new int(42);
// ... use dataPtr ...
delete dataPtr;
dataPtr = nullptr;
Polymorphism Implementation
Virtual function tables (vtable) and virtual pointers (vptr) enable runtime method resolution. Each object with virtual functions contains a vptr pointing to its class's vtable.
Virtual Functions
Virtual functions enable dynamic binding. Classes with virtual functions become polymorphic base classes.
Virtual Destructors
Base class destructors should be virtual to ensure proper cleanup when deleting derived objects through base pointers.
Arrays vs. Pointers
- Arrays: Fixed-size, constant pointer to first element
- Pointers: Variable, can be reassigned
- Array size includes all elements; pointer size is architecture-dependent
Memory Segments
- Stack: Automatic storage for local variables
- Heap: Dynamic memory allocation
- Global/Static: Program-life duration variables
- Code: Executable instructions
- Constants: Read-only data
Function Call Mechanics
- Push parameters and return address onto stack
- Create new stack frame for local variables
- Execute function code
- Restore stack and return to caller
Lvalues and Rvalues
Lvalues: Addressable, named entities Rvalues: Temporary, non-addressable expressions
HTTP Status Codes
- 200: Successful request
- 301: Permanent redirect
- 302: Temporary redirect
- 404: Resource not found
Non-Virtual Functions
- Regular functions (non-member)
- Constructors
- Inline functions
- Static member functions
- Friend functions
STL Container Characteristics
| Container | Duplicates | Element Type | Ordering | Lookup |
|---|---|---|---|---|
| set | No | Key=Value | Sorted | O(log n) |
| multiset | Yes | Key=Value | Sorted | O(log n) |
| map | No (keys) | Key-Value | Key-sorted | O(log n) |
| multimap | Yes (keys) | Key-Value | Key-sorted | O(log n) |
Smart Pointers
unique_ptr: Exclusive ownership, non-copyable shared_ptr: Shared ownership with reference counting weak_ptr: Non-owning reference to break cycles
The 'this' Pointer
Implicit pointer to current object instance:
- Points to current object address
- Available in member functions
- Passed as hidden parameter
- Unmodifiable pointer, modifiable content
Copy Semantics
Shallow copy: Copies pointers (shared resources) Deep copy: Duplicates pointed-to data (independent resources)
Const Qualifier
- Constants: Immutable values
- Const pointers: Fixed addresses or fixed content
- Const member functions: Non-modifying operations
Static Keyword
- Local static: Function-persistent variables
- Global static: File-scoped linkage
- Static functions: Internal linkage
- Class static: Shared class members
Inheritance Trade-offs
Banefits: Code reuse, efficiency Drawbacks: Tight coupling, fragility to base class changes
Virtual Base Classes
Solve diamond inheritance ambiguity by sharing single base class instance across inheritance paths.
class Base { /* ... */ };
class Derived1 : virtual public Base { /* ... */ };
class Derived2 : virtual public Base { /* ... */ };
class Final : public Derived1, public Derived2 { /* ... */ };