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?
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,
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 1
No code files for today Reminder: Project 3 due today. Homework 5 (!)
Questions?
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 2
Rules for dynamic classes
Destructor, copy constructor, assignment operator
Static multi-dimensional arrays Dynamic multi-dimensional arrays
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 3
Prototype is
Automatically called before an object is
Deallocates the dynamically-allocated data in
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 4
Prototype is:
Automatically called to create a value
Dynamically-allocates a new data structure to
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 5
A member function. Prototype is
Called for assignments
Must check for self-assignment. Deallocates
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 6
At least one attribute is a pointer variable. Member functions allocate and release memory
The class will have custom destructor, copy
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 7
[0] [0] [1] [2] [3] ...[NUM_COLS-1] [1] [2] [3] : [NUM_ROWS-1]
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 8
Cannot allocate a multi-dimensional array
But we can think of a two-dimensional array as
Applying this idea to dynamically-allocated
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 9
[2] [3] [0] [1] : : [NUM_ROWS-1] ... [0] [1] [2] [3] ...[NUM_COLS-1] ... ... ... ... array2DPtr
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 10
What is the type of array2DPtr? It is a pointer
The type of the outer array elements is int *. So the type of a pointer to the outer array is
How to allocate? Since dynamic, can ask the
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 11
To allocate the outer array say:
To create each individual row, need to allocate
type of the elements in each row type of the elements of the outer array
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 12
How to access an element of this data
Why does this work?
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 13
Deallocation needs to be done in the opposite
Friday, October 1 CS 215 Fundamentals of Programming II - Lecture 17 14
On a piece of paper, answer the following questions:
1. Write a declaration for a pointer variable array3DPtr
2. Write the code for allocating storage for numRows x
3. Write the code to initialize the storage allocated in (2) to
4. Write the code for deallocating the storage allocated in