FY - Btech-C - Unit-6
FY - Btech-C - Unit-6
Oriented Concepts
This presentation explores advanced object-oriented concepts.
We will focus on operator overloading and dynamic memory
management. Learn to enhance class functionality and
resource control.
Operator Overloading: Introduction
Extending Operators Vector Class Example
Operator overloading extends operators to work An example of this is overloading `+` for a `Vector`
with user-defined types. class to enable addition.
Declaration
`friend returnType functionName(parameters);` within class.
Use Case
Allowing stream insertion (`<<`) for custom objects
improves utility.
Overloading Assignment Operator (=)
Syntax
Deep Copy
2 Create independent copies of dynamically allocated data.
Default behavior
3 Member-wise copy (shallow copy).
Deep Copy Assignment
Operator
1 Class with 2 Create new memory
dynamically
Copy content from
allocated `char*`
right-hand side object
member
Demonstrates the issue to avoid conflict.
of the default
assignment operator.
Flexibility
Essential for creating flexible data structures that adapt.
Dynamic Size
Avoids limitations of fixed-size arrays and objects.
The `new` Operator
Allocate Memory Returns Pointer
Allocate memory from the Returns a pointer to the
heap at runtime as allocated memory
needed. location to track it.
Syntax
`Type* pointer = new Type;` or `Type* pointer = new Type[size];`
The `delete` Operator
Prevent Leaks
2 Crucial for preventing memory
leaks and resource exhaustion.
Deallocate Memory
Release memory allocated with 1
`new` back to the system.
Syntax
`delete pointer;` or `delete[]
3 pointer;` for arrays.
Dynamic Array Allocation Example
Allocate 1
Allocate an array of integers using `new` based on size.
2 Populate
Fill the array with calculated or input values to start.
Deallocate 3
Release the array using `delete[]` to avoid memory leaks.
Best Practices & Summary
Pair new and delete
Always match `new` with `delete` (and `new[]` with `delete[]`).
2 Automatic Deletion
Automatically deletes the object when it goes out
of scope.
3 Move Semantics
Cannot be copied, only moved using `std::move`.
Shared Pointers (`shared_ptr`) and Reference
Counting
Shared Ownership Reference Counting Automatic Deletion
Multiple `shared_ptr` Tracks the number of The object is deleted when the
instances can point to the `shared_ptr` instances reference count reaches zero.
same object. pointing to an object.
Weak Pointers (`weak_ptr`)
Non-Owning
Does not increment the reference count.
Observation
Used to observe objects managed by `shared_ptr`.
Circular Dependency
Breaks circular dependencies in `shared_ptr` graphs.
Introduction to Standard
Template Library (STL)
Containers
Vectors
Dynamic arrays with fast random access.
Lists
Doubly-linked lists for quick insertion/deletion.
Maps
Key-value pairs with ordered keys for fast lookups.
Sets
Unique elements, ordered for fast membership testing.
STL Iterators and Algorithms
Algorithms
Solution
Benefits
Increased Productivity
Offers reusable containers and algorithms.