0% found this document useful (0 votes)
16 views20 pages

FY - Btech-C - Unit-6

This document presents advanced object-oriented concepts, focusing on operator overloading and dynamic memory management in C++. It discusses the use of smart pointers and the Standard Template Library (STL) to improve memory management and code reliability. Key topics include deep copies, dynamic memory allocation, and practical applications in game development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

FY - Btech-C - Unit-6

This document presents advanced object-oriented concepts, focusing on operator overloading and dynamic memory management in C++. It discusses the use of smart pointers and the Standard Template Library (STL) to improve memory management and code reliability. Key topics include deep copies, dynamic memory allocation, and practical applications in game development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT 6: Advanced Object-

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.

The syntax is:

returnType operator symbol (parameters) { ... }


Friend Functions
Access
Granting non-member functions access to private
members is very useful.

Declaration
`friend returnType functionName(parameters);` within class.

Use Case
Allowing stream insertion (`<<`) for custom objects
improves utility.
Overloading Assignment Operator (=)

Syntax

1 `ClassName& operator=(const ClassName& other);`

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.

3 Delete existing memory


Prevent memory leaks by deleting existing memory.
Dynamic Memory
Management: Introduction
Allocation
Allocate memory during runtime to fit the amount
required.

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[]`).

Implement deep copies


Use copy constructors and assignment operators for
deep copies of data.

Consider smart pointers


Use `std::unique_ptr`, `std::shared_ptr` for auto
memory management.
Smart Pointers & STL:
Modern C++ Object
Management
Explore smart pointers and the Standard Template Library
(STL). Learn how to improve code reliability and reduce
memory leaks. Enhance performance with practical, real-world
examples. This presentation covers smart pointers, STL
containers, iterators, and algorithms.
Memory Management Challenges in C++
C++'s manual memory management can be error-
prone. Common issues include memory leaks and
dangling pointers. Double freeing memory can also
cause undefined behavior. Understanding these
challenges is essential for writing robust code.
Introduction to Smart
Pointers
Automatic Memory RAII Principle
Management
They utilize RAII to
Smart pointers manage resources
automate memory during object
management, reducing construction and
manual intervention. destruction.

Types of Smart Pointers


Key types are unique_ptr, shared_ptr, and weak_ptr,
each serving specific use cases.
Unique Pointers
(`unique_ptr`)
1 Exclusive Ownership
Only one `unique_ptr` can point to an object.

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

2 Operate on ranges defined by


iterators.
Iterators
1
Generalize pointer arithmetic
for traversing containers.
Sorting
Example: `std::sort(vec.begin(),
3 vec.end());`
Real-world Case Study: Game Development
Problem

Managing game objects with complex lifetimes is


challenging.

Solution

Use `shared_ptr` for game objects and `weak_ptr`


for relationships.

Benefits

Automatic memory management and easier object tracking.


Conclusion: Benefits of
Smart Pointers and STL
Simplified Memory Management
Reduces memory leaks and improves code safety.

Improved Code Quality


Enhances readability and maintainability.

Increased Productivity
Offers reusable containers and algorithms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy