SlideShare a Scribd company logo
OBJECT ORIENTED
PROGRAMMING
w/ C++
What’s Ahead 👀
Identity and behaviour of
an object
C++ garbage collection Dynamic memory
allocation
01 02 03
Objects
Garbage
Collection
Memory
Allocation
01
Identity and behaviour
The identity and behaviour of an object are two
critical aspects that define its uniqueness and
functionality within a program.
Identity of an Object
Identity refers to the unique property that
distinguishes one object from another, even if
their data (state) and behaviour are identical. In
memory, each object occupies a distinct location,
giving it a unique identity.
Example
class Car {
public:
string make;
string model;
int year;
Car(string m, string mod, int y) : make(m), model(mod), year(y) {}
};
int main() {
Car car1("Toyota", "Corolla", 2020);
Car car2("Toyota", "Corolla", 2020);
// Even though car1 and car2 have the same state, they are different objects.
if (&car1 != &car2) {
cout << "car1 and car2 are distinct objects.n";
}
return 0;
}
Consider two instances of a Car class
Behaviour of an Object
Behaviour refers to the methods or functions
that define what an object can do. It's the
implementation of operations that can be
performed on or by the object.
Example
class Car {
public:
string make;
string model;
int year;
Car(string m, string mod, int y) : make(m), model(mod), year(y) {}
void accelerate() {
cout << "The car is accelerating.n";
}
void brake() {
cout << "The car is braking.n";
}
};
int main() {
Car myCar("Toyota", "Camry", 2023);
myCar.accelerate();
myCar.brake();
return 0;
}
Extending the Car class with behaviour:
Advantages
Encapsulation: Identity and behaviour encapsulate data and methods,
promoting modularity and reusability.
Abstraction: Hides complex implementation details, exposing only
necessary interfaces.
Polymorphism: Objects can be treated as instances of their parent class
rather than their actual class.
Disadvantages
Complexity: Understanding object identity, especially in languages that
allow for object cloning or copying, can be complex.
Memory Management: Objects with complex behaviours may require
careful memory management to prevent leaks.
02
C++ Garbage Collection
Garbage Collection (GC) is an automatic memory management feature that
reclaims memory occupied by objects that are no longer in use. Unlike
languages like Java or C#, C++ does not have built-in garbage collection;
instead, it relies on manual memory management.
Manual Memory Management in C++
In C++, dynamic memory is allocated using new or malloc(), and memory
must be manually deallocated using delete or free() to avoid memory leaks.
Example:
int main() {
int* ptr = new int(5); // Allocating memory
cout << "Value: " << *ptr << endl;
delete ptr; // Deallocating memory
return 0;
}
Destructors for garbage collection
Object Creation: When you create an object, memory is allocated for it on
the heap.
Object Usage: The object is used throughout its lifetime.
Object Destruction: When the object goes out of scope (e.g., when a
function returns or a block ends), its destructor is called.
Resource Cleanup: The destructor typically performs tasks like:
● Deallocating dynamically allocated memory using delete or delete[].
● Closing files or network connections.
● Releasing other resources.
Example
class MyClass {
public:
MyClass() {
std::cout << "Constructor calledn";
data = new int[10];
}
~MyClass() {
std::cout << "Destructor calledn";
delete[] data;
}
private:
int* data;
};
int main() {
MyClass obj; // Object created on the heap
// ... use obj ...
return 0; // obj goes out of scope, destructor is called
}
Here, the destructor is responsible for deleting the dynamically allocated array data. If the
destructor wasn't called, the memory would be leaked, leading to potential memory issues.
03
Dynamic Memory Allocation
Dynamic Memory Allocation allows programs to request memory at runtime,
providing flexibility for variable-sized data structures and efficient memory
usage.
In C++, Dynamic Memory Allocation
Allocation: Using new or malloc().
Deallocation: Using delete or free().
Example
int* arr = new int[10]; // Allocate array of 10 integers
// Use arr
delete[] arr; // Deallocate memory
Example with new and delete:
Advantages
Flexibility: Allocate memory as needed, handling variable-sized data.
Efficient Memory Use: Only use memory when necessary.
Dynamic Data Structures: Enables the creation of data structures like
linked lists, trees, etc.
Disadvantages
Memory Leaks: Failure to deallocate memory leads to leaks.
Dangling Pointers: Deallocating memory that's still in use can cause
undefined behaviour.
Fragmentation: Frequent allocation and deallocation can fragment
memory.
Requirements
Manual Management: Responsibility on the developer to manage memory
lifecycle.
Understanding Pointers: Proficiency with pointers is essential.
Error Handling: Proper checks to handle allocation failures.
Thank You!
A Presentation by Rakshit
Dogra

More Related Content

DOCX
C# Unit 2 notes
Sudarshan Dhondaley
 
DOC
Memory management
LavishMaheshwari
 
PPTX
Dynamic memory allocation in c++
Tech_MX
 
PDF
(3) cpp abstractions more_on_user_defined_types
Nico Ludwig
 
PPT
C++ oop
Sunil OS
 
PPTX
CSC2161Programming_in_Cpp_Lecture4-OOP Classes and Objects[1].pptx
winebaldbanituze
 
PPTX
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
FiraolGadissa
 
C# Unit 2 notes
Sudarshan Dhondaley
 
Memory management
LavishMaheshwari
 
Dynamic memory allocation in c++
Tech_MX
 
(3) cpp abstractions more_on_user_defined_types
Nico Ludwig
 
C++ oop
Sunil OS
 
CSC2161Programming_in_Cpp_Lecture4-OOP Classes and Objects[1].pptx
winebaldbanituze
 
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
Chapter 2 OOP using C++ (Introduction).pptx
FiraolGadissa
 

Similar to OBJECT ORIENTED PROGRAMMING using C++ / CPP (20)

PDF
(5) cpp dynamic memory_arrays_and_c-strings
Nico Ludwig
 
DOCX
New microsoft office word document (2)
rashmita_mishra
 
DOCX
C questions
parm112
 
DOCX
OOP and C++Classes
MuhammadHuzaifa981023
 
PDF
Object Oriented Programming (OOP) using C++ - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PPS
Oops recap
Niit Care
 
PPTX
Interview preparation for programming.pptx
BilalHussainShah5
 
PPTX
Dma
Joy Forerver
 
PPT
C++tutorial
dips17
 
PPT
data Structure Lecture 1
Teksify
 
PDF
Smart Pointers in C++
Francesco Casalegno
 
PPTX
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
PPT
Object oriented programming using c++
Hoang Nguyen
 
PPTX
PPT DMA.pptx
Abhishekkumarsingh630054
 
PDF
Op ps
Shehzad Rizwan
 
PDF
One Careful Owner
Kevlin Henney
 
PPTX
Object Oriented Programming using c++.pptx
olisahchristopher
 
PDF
New c sharp3_features_(linq)_part_i
Nico Ludwig
 
PPTX
Constructors and Destructors
Keyur Vadodariya
 
(5) cpp dynamic memory_arrays_and_c-strings
Nico Ludwig
 
New microsoft office word document (2)
rashmita_mishra
 
C questions
parm112
 
OOP and C++Classes
MuhammadHuzaifa981023
 
Object Oriented Programming (OOP) using C++ - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Oops recap
Niit Care
 
Interview preparation for programming.pptx
BilalHussainShah5
 
C++tutorial
dips17
 
data Structure Lecture 1
Teksify
 
Smart Pointers in C++
Francesco Casalegno
 
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
Object oriented programming using c++
Hoang Nguyen
 
One Careful Owner
Kevlin Henney
 
Object Oriented Programming using c++.pptx
olisahchristopher
 
New c sharp3_features_(linq)_part_i
Nico Ludwig
 
Constructors and Destructors
Keyur Vadodariya
 
Ad

More from RAKSHITDOGRA1 (7)

PPTX
Searching & Algorithms IN DATA STRUCTURES
RAKSHITDOGRA1
 
PPTX
Object Oriented Programming using C++ / C Plus Plus QUIZ
RAKSHITDOGRA1
 
PDF
Presentation on Algorithms and Flowcharts
RAKSHITDOGRA1
 
PDF
Distinction test for functional groups
RAKSHITDOGRA1
 
PDF
Presentation on Poem Analysis of My Mother At Sixty- Six
RAKSHITDOGRA1
 
PDF
Determination of Caffeine In Tea Samples
RAKSHITDOGRA1
 
PDF
Mineral resources in Sikkim ppt
RAKSHITDOGRA1
 
Searching & Algorithms IN DATA STRUCTURES
RAKSHITDOGRA1
 
Object Oriented Programming using C++ / C Plus Plus QUIZ
RAKSHITDOGRA1
 
Presentation on Algorithms and Flowcharts
RAKSHITDOGRA1
 
Distinction test for functional groups
RAKSHITDOGRA1
 
Presentation on Poem Analysis of My Mother At Sixty- Six
RAKSHITDOGRA1
 
Determination of Caffeine In Tea Samples
RAKSHITDOGRA1
 
Mineral resources in Sikkim ppt
RAKSHITDOGRA1
 
Ad

Recently uploaded (20)

PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Architecture of the Future (09152021)
EdwardMeyman
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 

OBJECT ORIENTED PROGRAMMING using C++ / CPP

  • 2. What’s Ahead 👀 Identity and behaviour of an object C++ garbage collection Dynamic memory allocation 01 02 03 Objects Garbage Collection Memory Allocation
  • 3. 01
  • 4. Identity and behaviour The identity and behaviour of an object are two critical aspects that define its uniqueness and functionality within a program.
  • 5. Identity of an Object Identity refers to the unique property that distinguishes one object from another, even if their data (state) and behaviour are identical. In memory, each object occupies a distinct location, giving it a unique identity.
  • 6. Example class Car { public: string make; string model; int year; Car(string m, string mod, int y) : make(m), model(mod), year(y) {} }; int main() { Car car1("Toyota", "Corolla", 2020); Car car2("Toyota", "Corolla", 2020); // Even though car1 and car2 have the same state, they are different objects. if (&car1 != &car2) { cout << "car1 and car2 are distinct objects.n"; } return 0; } Consider two instances of a Car class
  • 7. Behaviour of an Object Behaviour refers to the methods or functions that define what an object can do. It's the implementation of operations that can be performed on or by the object.
  • 8. Example class Car { public: string make; string model; int year; Car(string m, string mod, int y) : make(m), model(mod), year(y) {} void accelerate() { cout << "The car is accelerating.n"; } void brake() { cout << "The car is braking.n"; } }; int main() { Car myCar("Toyota", "Camry", 2023); myCar.accelerate(); myCar.brake(); return 0; } Extending the Car class with behaviour:
  • 9. Advantages Encapsulation: Identity and behaviour encapsulate data and methods, promoting modularity and reusability. Abstraction: Hides complex implementation details, exposing only necessary interfaces. Polymorphism: Objects can be treated as instances of their parent class rather than their actual class.
  • 10. Disadvantages Complexity: Understanding object identity, especially in languages that allow for object cloning or copying, can be complex. Memory Management: Objects with complex behaviours may require careful memory management to prevent leaks.
  • 11. 02
  • 12. C++ Garbage Collection Garbage Collection (GC) is an automatic memory management feature that reclaims memory occupied by objects that are no longer in use. Unlike languages like Java or C#, C++ does not have built-in garbage collection; instead, it relies on manual memory management.
  • 13. Manual Memory Management in C++ In C++, dynamic memory is allocated using new or malloc(), and memory must be manually deallocated using delete or free() to avoid memory leaks. Example: int main() { int* ptr = new int(5); // Allocating memory cout << "Value: " << *ptr << endl; delete ptr; // Deallocating memory return 0; }
  • 14. Destructors for garbage collection Object Creation: When you create an object, memory is allocated for it on the heap. Object Usage: The object is used throughout its lifetime. Object Destruction: When the object goes out of scope (e.g., when a function returns or a block ends), its destructor is called. Resource Cleanup: The destructor typically performs tasks like: ● Deallocating dynamically allocated memory using delete or delete[]. ● Closing files or network connections. ● Releasing other resources.
  • 15. Example class MyClass { public: MyClass() { std::cout << "Constructor calledn"; data = new int[10]; } ~MyClass() { std::cout << "Destructor calledn"; delete[] data; } private: int* data; }; int main() { MyClass obj; // Object created on the heap // ... use obj ... return 0; // obj goes out of scope, destructor is called } Here, the destructor is responsible for deleting the dynamically allocated array data. If the destructor wasn't called, the memory would be leaked, leading to potential memory issues.
  • 16. 03
  • 17. Dynamic Memory Allocation Dynamic Memory Allocation allows programs to request memory at runtime, providing flexibility for variable-sized data structures and efficient memory usage. In C++, Dynamic Memory Allocation Allocation: Using new or malloc(). Deallocation: Using delete or free().
  • 18. Example int* arr = new int[10]; // Allocate array of 10 integers // Use arr delete[] arr; // Deallocate memory Example with new and delete:
  • 19. Advantages Flexibility: Allocate memory as needed, handling variable-sized data. Efficient Memory Use: Only use memory when necessary. Dynamic Data Structures: Enables the creation of data structures like linked lists, trees, etc.
  • 20. Disadvantages Memory Leaks: Failure to deallocate memory leads to leaks. Dangling Pointers: Deallocating memory that's still in use can cause undefined behaviour. Fragmentation: Frequent allocation and deallocation can fragment memory.
  • 21. Requirements Manual Management: Responsibility on the developer to manage memory lifecycle. Understanding Pointers: Proficiency with pointers is essential. Error Handling: Proper checks to handle allocation failures.
  • 22. Thank You! A Presentation by Rakshit Dogra

Editor's Notes

  • #6: In this example, car1 and car2 have the same attributes but are distinct due to their unique memory addresses.
  • #7: Alters the object.
  • #8: Here, accelerate() and brake() define the behaviour of the Car object.
  • #9: Encapsulation: Abstraction: Polymorphism
  • #10: Complexity Memory Management
  • #12: Here, accelerate() and brake() define the behaviour of the Car object.
  • #13: In this example, memory is manually allocated for an integer using new, and later deallocated using delete to prevent a memory leak.
  • #14: In this example, memory is manually allocated for an integer using new, and later deallocated using delete to prevent a memory leak.
  • #15: Here, accelerate() and brake() define the behaviour of the Car object.
  • #17: Here, accelerate() and brake() define the behaviour of the Car object.
  • #18: Here, accelerate() and brake() define the behaviour of the Car object.
  • #19: Here, accelerate() and brake() define the behaviour of the Car object.
  • #20: Here, accelerate() and brake() define the behaviour of the Car object.
  • #21: Here, accelerate() and brake() define the behaviour of the Car object.