1 / 24

New, Delete, This Pointer and Object Return

University Presentation

19202103416
Download Presentation

New, Delete, This Pointer and Object Return

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome

  2. Our Team Members

  3. TOPICS Here, we will represent about : 1. New 2. Delete 3. This Pointer 4. Object Return

  4. TABLE OF CONTENTS • INTRODUCING C++ • QUOTE • WHAT IS NEW AND DELETE OPERATOR ? • How we can use NEW operator ? • MINIMAL DEFINITION of OPERATOR new() • How we can use DELETE operator ? • Allocation and De-allocation of Array • WHAT IS THIS POINTER !? • How we can use THIS pointer ? • WHAT IS OBJECT RETURN ? • CONDITION FOR OBJECT RETURN • TYPES OF OBJECT RETURN • How We Can Return a Object? • RESOURCES

  5. INTRODUCING C++ C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.

  6. “Within C++, there is a much smaller and cleaner language struggling to get out.” —Bjarne Stroustrup

  7. New & Delete

  8. C++ allows us to allocate the memory of a variable or an array in run time. This is known as dynamic memory allocation.In C++, we need to deallocate the dynamically allocated memory manually after we have no use for the variable.We can allocate and then deallocate memory dynamically using the NEW and DELETE operators respectively. WHAT IS NEW AND DELETE OPERATOR ?

  9. How we can use NEW operator? Operator “new()” can be user defined or built in. If a programmer don’t specify operator “new()” , built in operator “new()” will be used by default. Operator “new()” can be defined globally or as a member of class. There should be only one global operator “new()” with particular types of parameter in an executable. Operator “new()” global/local only allocates the memory from heap. Memory allocated on heap is having lifespan beyond its original scope. So to prevent the memory from leaking , programmer explicitly need to release the memory.

  10. MINIMAL DEFINITION of OPERATOR new() : extern "C" void *malloc (size_t); void* operator new(size_tsz) { return malloc (sz); } class A { }; int main() { A*a = new A(); return 0; } This is the minimal definition of operator "new()" taking the argument as size of memory to be allocated and returning the void* of the memory allocated by "malloc".

  11. How we can use DELETE operator? Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator New operator is used for dynamic memory allocation which puts variables on heap memory. Which means Delete operator deallocates memory from heap. Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.

  12. Let’s See a Example: // Program to illustrate deletion of array #include <bits/stdc++.h> using namespace std; int main() { // Allocate Heap memory int* array = new int[10]; // Deallocate Heap memory delete[] array; return 0; }

  13. Allocation and De-allocation of Array : A *p_a1 = new A(); A *p_a2 = new A[10]; delete p_a1; // deleting object delete[] p_a2; // deleting array of object * Operator "new()" and "delete()" can also be used for allocating and de- allocating of memory for array of objects. * When calling the operator "delete()" for the pointer to an array, a programmer has to use [] with "delete()" to free entire arrays’ memory.

  14. This Pointer

  15. C++ provides a keyword 'this', which represents the current object and passed as a hidden argument to all member functions. The this pointer is a constant pointer that holds the memory address of the current object. The this pointer is not available in static member functions as static member functions can be called without any object. static member functions can be called with class name. WHAT IS THIS POINTER !?

  16. How we can use THIS pointer? In example, we have two data members num and ch. In member function setMyValues() we have two local variables having same name as data members name. In such case if we want to assign the local variable value to the data members then we won’t be able to do until unless we use this pointer, because the compiler won’t know that we are referring to object’s data members unless we use this pointer. This is one of the example where we must use this pointer.

  17. Example using THIS pointer #include <iostream> using namespace std; class Demo { private: int num; char ch; public: void setMyValues(int num, char ch){ this->num =num; this->ch=ch; } void displayMyValues(){ cout<<num<<endl; cout<<ch; } }; int main(){ Demo obj; obj.setMyValues(100, 'A'); obj.displayMyValues(); return 0; }

  18. Object Return

  19. WHAT IS OBJECT RETURN ? A function can return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function. The syntax for defining a function that returns an object by value is class_name function_name (parameter_list) { // body of the function } To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. function_name(object_name);

  20. CONDITION FOR OBJECT RETURN • If a method or function returns a local object, it should return an object, not a reference. • If a method or function returns an object of a class for which there is no public copy constructor, such as ostream class, it must return a reference to an object. • Some methods and functions, such as the overloaded assignment operator, can return either an object or a reference to an object. The reference is preferred for reasons of efficiency. • It is an error to return a pointer to a local object. Once the function completes, the local object are freed. The pointer would be a dangling pointer that refers to a nonexistent object.

  21. TYPES OF OBJECT RETURN When a function, either a member function or a standalone function, returns an object, we have choices. The function could return • A reference to an object • A constant reference to an object • An object • A constant object

  22. How we can return a Object? In C++, to do object return we have 2 options: (1) I can use references whenever I need to "return" an object Void calculateThing(Thing & thing) { //do calculations and modify thing } Then use it like this: Thing thing; calculateThing(thing); (2) Or I can return a pointer to a dynamically allocated object Thing* calculaTething() { Thing* thing(new Thing()); //do calculations and modify thing return thing; } Then use it like this: Thing* thing=calculateThing(); delete thing;

  23. LINKS PHOTOS RESOURCES New and delete operators in C++ for dynamic memory Dynamic memory C++ ‘this’ Pointer C++ TUTORIAL - OBJECT RETURNING - 2020 How to “return an object” in C++? How to pass and return object from C++ Functions? Passing and Returning Objects in C++ Surfing Google Screenshots Pinterest

  24. THANK YOU !

More Related