KEMBAR78
OBJECT ORIENTED PROGRAMMING using C++ / CPP | PPTX
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

OBJECT ORIENTED PROGRAMMING using C++ / CPP

  • 1.
  • 2.
    What’s Ahead 👀 Identityand behaviour of an object C++ garbage collection Dynamic memory allocation 01 02 03 Objects Garbage Collection Memory Allocation
  • 3.
  • 4.
    Identity and behaviour Theidentity and behaviour of an object are two critical aspects that define its uniqueness and functionality within a program.
  • 5.
    Identity of anObject 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: stringmake; 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 anObject 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: stringmake; 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 andbehaviour 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 objectidentity, 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.
  • 12.
    C++ Garbage Collection GarbageCollection (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 Managementin 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 garbagecollection 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.
  • 17.
    Dynamic Memory Allocation DynamicMemory 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 memoryas 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: Failureto 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: Responsibilityon 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 Presentationby 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.