Unformatted text preview:

Lecture 16OutlineDestructorCopy ConstructorAssignment OperatorRules for Dynamic ClassesStatic Multi-dimensional ArraysDynamic Multi-dimensional Array 1Dynamic Multi-dimensional Array 2Dynamic Multi-dimensional Array 3Dynamic 2D AllocationDynamic 2D AccessDynamic 2D DeallocationIn-class ExerciseWednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 1Lecture 16No code files for todayHomework 8 posted, due next Wednesday.Reminder: Homework 7 due today. Project 3 due on Monday.Engineers' Week Banquet next Tuesday. Sign ups in KC-250 end today.Questions?Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 2OutlineReview: Rules for dynamic classesDestructor, copy constructor, assignment operatorNote: this material is not in the textbookStatic multi-dimensional arraysDynamic multi-dimensional arraysWednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 3DestructorPrototype is ~ClassName();Automatically called before an object is destroyed.Deallocates the dynamically-allocated data in the opposite order of construction.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 4Copy ConstructorPrototype is: ClassName(const ClassName & source);Automatically called to create a value parameter copy, a return value copy, or a variable with initialization.Dynamically-allocates a new data structure to hold a copy of the dynamic data of the source.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 5Assignment OperatorA member function called when there is an assignment. Prototype isClassName & operator= (const ClassName & source);Must check for se lf-assignment. Deallocates the dynamically-allocated data in the opposite order of construction, then dynamically-allocates a new data structure to hold a copy of the dynamic data of the source. Returns *this.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 6Rules for Dynamic ClassesAt least one attribute is a pointer variable.Member functions allocate and release memory as needed.The class will have custom destructor, copy constructor, and assignment (operator=) functions. Note: without a destructor, the class will create garbage, but will otherwise be correct. Without the copy constructor or the assignment operator, the class will be incorrect when trying to make copies.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 7Static Multi-dimensional Arrayint array2D[NUM_ROWS][NUM_COLS];[0][0] [1] [2] [3] ...[NUM_COLS-1][1][2][3]:[NUM_ROWS-1]Access: array2D[i][j]Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 8Dynamic Multi-dimensional ArrayCannot allocate a multi-dimensional array directly, since the new operator only can create a one-dimensional array.But we can think of a two-dimensional array as a one-dimensional array with one-dimensional array elements.Applying this idea to dynamically-allocated arrays, we get a pointer to an array of pointers to arrays of integer.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 9Dynamic Multi-dimensional Array[2][3][0][1]::[NUM_ROWS-1]...[0] [1] [2] [3] ...[NUM_COLS-1]............array2DPtrWednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 10Dynamic Multi-dimensional ArrayWhat is the type of array2DPtr? It is a pointer to an array of pointers to integer arrays.The type of the outer array elements is int *.So the type of a pointer to the outer array is int **.How to allocate? Since dynamic, can ask the user for the dimensions, numRows and numCols. First allocate the outer array, then allocate the individual rows.Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 11Dynamic 2D AllocationTo allocate the outer array say:int **array2DPtr = new int *[numRows];To create each individual row, need to allocate an integer array and make an outer element point to it.for (int i = 0; i < numRows; i++) array2DPtr[i] = new int [numCols];type of the elements in each rowtype of the elements of the outer arrayWednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 12Dynamic 2D AccessHow to access an element of this data structure? Indexing is the same as with a static multi-dimensional array: array2DPtr[i][j]Why does this work?*(array2DPtr+i) => array2DPtr[i] => address of rowarray2DPtr[i]+j => address of an element*(array2DPtr[i]+j) => array2DPtr[i][j]Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 13Dynamic 2D DeallocationDeallocation needs to be done in the opposite order of allocation. Delete the rows first, then the outer array.// Delete the rows firstfor (int i = 0; i < numRows; i++) delete [] array2DPtr[i];// Delete the outer arraydelete [] array2DPtr;Wednesday, February 16 CS 215 Fundamentals of Programming II - Lecture 16 14In-class Exercise / Homework 8, Problem 2(a). Write a declaration for a pointer variable array3DPtr that will point to a dynamic 3D boolean array.(b). Write the code for allocating storage for numRows x numCols x numDepth elements with array3DPtr pointing to the outer array.(c). Write the code to initialize the storage allocated in (b) to be true if the sum of the indexes of an element is even and false if the sum of the indexes of an element is odd. That is, element array3DPtr[i][j][k] is true if i+j+k is even, etc.(d). Write the code for deallocating the storage allocated in


View Full Document

UE CS 215 - Lecture 16

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 16
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 16 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 16 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?