Download presentation
Presentation is loading. Please wait.
Published byMabel Allen Modified over 9 years ago
1
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation
2
Recap of Lecture 6 Friend classes and member functions Constructors Implicit and explicit calling Default constructors Destructors
3
Outline of Lecture 7 Arrays of Objects Pointers to object The this pointer Pointer to class members Dynamic memory allocation
4
Arrays of Objects It is possible to have arrays of objects The syntax for declaring and using an object array is exactly the same as it is for any other type of array. Looks similar to array of structures Special case for single element constructors Intend to create both initialized and uninitialized arrays of objects Class-name object-name [sizeOfArray];
5
Pointers to Objects Just as you can have pointers to other types of variables, you can have pointers to objects. When accessing members of a class given a pointer to an object, use the arrow (->) operator instead of the dot (.) operator. Must initialize the pointer before using it. Pointer arithmetic is relative to the base type of the pointer.
6
The this Pointer The Problem: “Yes, inside of a class you have access to all of the private data, but how do you access the object itself like a client would?” Each object maintains a pointer to itself which is called the “this” pointer. Each object can determine it’s own address by using the “this” keyword. Avoids the overhead of passing parameters but still enforces the rules of good sound software engineering by using the appropriate class functions.
7
Pointers to Class Members Special type of pointer that "points" generically to a member of a class Not to a specific instance of that member in an object It is called a pointer to a class member or a pointer-to- member Since member pointers are not true pointers, the. and -> cannot be applied to them. Pointer-to-member operators.* and –>*.
8
References A reference is essentially an implicit pointer. Three ways it can be used as a function parameter, as a function return value, as a stand-alone reference.
9
Passing References to Objects When an object is passed as an argument to a function, a copy of that object is made. When the function terminates, the copy's destructor is called. If you do not want the destructor function to be called, simply pass the object by reference.
10
Returning References A function may return a reference. This has the rather startling effect Allows a function to be used on the left side of an assignment statement!!
11
Independent Reference Most common uses for references - argument, return value Declare a reference that is simply a variable This type of reference is called an independent reference.
12
Dynamic memory allocation Two dynamic allocation operators: new and delete These operators are used to allocate and free memory at run time. Dynamic allocation is an important part The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new.
13
Syntax of new, delete operators pointer = new type pointer = new type [number_of_elements] delete pointer; delete[] pointer; It is a requested by a program To be allocated by the system from the memory heap. No guarantee that all requests to allocate memory using operator new are going to be granted by the system.
14
How to check memory allocation C++ provides two standard mechanisms to check if the allocation was successful One is by handling exceptions. The other method is known as nothrow Instead of throwing a bad_alloc exception or terminating the program, the pointer returned by new is a null pointer
15
Different uses of dynamic allocation operators Initializing Allocated Memory Allocating Arrays Allocating Objects When it is created, its constructor function (if it has one) is called. When the object is freed, its destructor function is executed.
16
Thank you Next Lecture: Function Overloading
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.