Lecture 17 No code files for today Reminder: Project 3 due today. - - PowerPoint PPT Presentation

lecture 17
SMART_READER_LITE
LIVE PREVIEW

Lecture 17 No code files for today Reminder: Project 3 due today. - - PowerPoint PPT Presentation

Lecture 17 No code files for today Reminder: Project 3 due today. Homework 5 (!) due on Monday. Questions? Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 1 Outline Rules for dynamic classes Destructor,


slide-1
SLIDE 1

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 1

Lecture 17

 No code files for today  Reminder: Project 3 due today. Homework 5 (!)

due on Monday.

 Questions?

slide-2
SLIDE 2

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 2

Outline

 Rules for dynamic classes

 Destructor, copy constructor, assignment operator

 Static multi-dimensional arrays  Dynamic multi-dimensional arrays

slide-3
SLIDE 3

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 3

Destructor

 Prototype is

~ClassName();

 Automatically called before an object is

destroyed.

 Deallocates the dynamically-allocated data in

the opposite order of construction.

slide-4
SLIDE 4

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 4

Copy 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.

slide-5
SLIDE 5

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 5

Assignment Operator

 A member function. Prototype is

void operator=(const ClassName & source);

 Called for assignments

ClassName obj1, obj2; :

  • bj1 = obj2;

 Must check for self-assignment. Deallocates

the dynamically-allocated data in the opposite

  • rder of construction, then dynamically-allocates

a new data structure to hold a copy of the dynamic data of the source.

slide-6
SLIDE 6

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 6

Rules 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.

slide-7
SLIDE 7

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 7

Static Multi-dimensional Array

int array2D[NUM_ROWS][NUM_COLS];

[0] [0] [1] [2] [3] ...[NUM_COLS-1] [1] [2] [3] : [NUM_ROWS-1]

Access: array2D[i][j]

slide-8
SLIDE 8

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 8

Dynamic Multi-dimensional Array

 Cannot allocate a multi-dimensional array

directly, since the new operator can create only 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.

slide-9
SLIDE 9

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 9

Dynamic Multi-dimensional Array

[2] [3] [0] [1] : : [NUM_ROWS-1] ... [0] [1] [2] [3] ...[NUM_COLS-1] ... ... ... ... array2DPtr

slide-10
SLIDE 10

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 10

Dynamic 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.

slide-11
SLIDE 11

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 11

Dynamic 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 row type of the elements of the outer array

slide-12
SLIDE 12

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 12

Dynamic 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 row array2DPtr[i]+j => address of an element *(array2DPtr[i]+j) => array2DPtr[i][j]

slide-13
SLIDE 13

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 13

Dynamic 2D Deallocation

 Deallocation needs to be done in the opposite

  • rder of allocation. Delete the rows first, then

the outer array.

// Delete the rows first for (int i = 0; i < numRows; i++) delete [] array2DPtr[i]; // Delete the outer array delete [] array2DPtr;

slide-14
SLIDE 14

Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 14

In-class Exercise

 On a piece of paper, answer the following questions:

 1. Write a declaration for a pointer variable array3DPtr

that will point to a dynamic 3D boolean array.

 2. Write the code for allocating storage for numRows x

numCols x numDepth elements with array3DPtr pointing to the outer array.

 3. Write the code to initialize the storage allocated in (2) 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] is true if i+j is even, etc.

 4. Write the code for deallocating the storage allocated in

(2).