Comp 2673, Spring 2002
April 8, lecture notes
Reading: Continue reviewing Chapters 6-8 in Deitel and Deitel Case Study: Here's a declaration of the class Maze: maze.h You can store a two-dimensional array using a singly-indexed array: The elements in the 2-D array are listed in the singly-indexed array in the following order: list the first row in order, then continue on to the second row, third, etc, until you are done. How do you get at the ith row, jth column of the 2-D array? If there are num_cols columns, then array[i][j] is the same as array[i*num_cols+j] Dynamically allocating memory: (See setion 8.10 for another detailed example) Note that the data member for the maze itself is just a pointer to a char, which means that space to store the array must be allocated dynamically. This should be done as soon as you know how big the maze is, for example in the read function, and in the generate_maze function. We also need to be very thorough with releasing (deleting) memory that we are done with. This should be done in the destructor. Also, to avoid memory leaks, any function that allocates memory for the data member "maze" should delete the existing memory before allocating space for a maze of a different size (for example, in the read function and in the generate_maze function) Since the Maze class has a data member that points to dynamically allocated memory, it's important for us to implement a destructor, an overloaded assignment operator, and a copy constructor. The destructor should release the dynamically allocated memory, the assignment operator should allocate space to store a copy of the maze, and the copy constructor should do the same. The assignment operator also has to release any memory that has been previously allocated. The copy constructor doesn't have to do this because the object is being created for the first time.