cs1ch10sec1.htm
  CHAPTER 10

ANOTHER VISIT WITH THE CONTRACT PROGRAM
OR
WORKING WITH DYNAMICALLY SIZED LISTS
 


Section I: An Expandable List of Contracts Section II: Declaring the New ListofContracts Section III: Defining the New ListOfContracts Section IV : Re-Evaluating the Contract Program Design
Section V: Adding String Properties to the Contracts Class Section VI: Declaring the New Contract Class Section VII: Defining the New Contract Class Section VIII: Modifying the Main Program

  


Table of Contents

Learning C++:
An Index of Entry Points


2. The

of C++

A reference document on the basic elements of C++.



3. The Patterns



Index!



In the last chapter we learned about dynamic memory, and we discovered that one can request memory just when one needs it. Using this, we can now write code to increase the size of our list of contracts whenever we need to. We also learned how to work with strings so now we can deal with names, addresses etc. in contracts. It is time to add these abilities to the contract program. However, let's make the changes one at a time, focusing first on allowing the list to grow.

The problem narrative remains almost exactly the same. The only change is the removal of any reference to a limit on the number of contracts in the list. It was the 'limit' data member in 'ListOfContracts' that represented the maximum number of contracts so it should be removed. Since this property was part of 'ListOfContracts', we can assume that there are no changes to the Contract class.

The data members 'limit' and 'numContracts' in the previous version of 'ListOfContracts' were special in that they were not properties that the users of the ListOfContracts class had any reason to know about. They were not like, for example, 'numDesks' - which was of interest to users. For that reason, there were no member functions to allow users of the class to retrieve or change the value of 'limit' or 'numContracts'. Notice also that these properties were never mentioned in the problem narrative. Rather, their existence was determined as part of the analysis of how to create a list structure.

In Section IV of this chapter you will study more such data members - data members that exist because they make a data structure work. In this case, the data structure is a list and we now use it to meet the goal of having a collection of contracts that can grow (or shrink, although we will not worry about that here). As already noted, part of our task here is to keep track of how full the list is. To do that we need to keep track of two things - the maximum number of elements the list can hold at the present time and the actual number of elements in the list at present. These become data members!

Of course, the class declaration already has the data member 'numContracts' to keep track of the actual number of elements on the list at present. The class also had, up to a moment ago, a data member to keep track of the maximum number of elements. It looks like we need it back but let's change its name slightly to 'presentMax' to emphasize that this maximum is not fixed in stone.

The earlier version also had an array to hold the elements. Our new version will still have an array but this time the data member will not be the array itself but a pointer to the array. This way, when the program needs a bigger list, it can ask the operating system for a bigger array and have the pointer point to this new array. We will use the same name 'listOfContracts' to represent this pointer.

Perhaps a set of algorithms for three key member functions will make the processes involved clearer.

1. The Constructor:

  1. Set numContracts to 0
  2. Set presentMax to some initially decided upon value
  3. Request enough memory for an array of contracts with presentMax elements and have 'listOfContracts' point to it.


This algorithm is similar to that for the constructor for the previous version of this class except that we have to explicitly ask for memory for the array since we have declared 'listOfContracts' to be a pointer type.

2. The function member 'Add'

    1. Get the Contract ID
    2. Search for the contract in the list
    3. If the contract is not found
      3.1 If the array is full
        3.1.1 Increase its size
            End If
      3.2 Put the contract on the list
      3.3 Increment numContracts
    Else
      3.4 Announce that a contract with this ID already exists on the list

This function is again similar to the one found in the previous version of the class except that it increases the size of the array if needed instead of reporting a failure because the list was full.

3. The function to increase the size of the list

  1. Add some amount to presentMax (We will use a constant for the amount to add.)
  2. Declare a temporary pointer to an array of contracts - call it tempArray
  3. Request enough memory for an array of contracts with presentMax elements (remember, presentMax now has a larger value) and have 'tempArray' point to it
  4. Copy the contracts in the array pointed to by 'listOfContracts' to the larger array pointed to by tempArray
  5. Return the memory used by 'listOfContracts' to the system
  6. Have 'listOfContracts' point to the memory pointed to by 'tempArray'

Step five of this last algorithm introduces a new idea. Not only can a program request memory from the system; it can also return memory. This allows a program to make use of some memory for awhile and later return it to the system, so that it can perhaps be used in some other way later in the program. That is exactly what step five does. The memory returned to the system may later become part of a yet larger array of contracts.

Note that this last function uses pointers exactly as we used them earlier in this chapter except that now the pointer variable is pointing to a whole array. Pictures help make pointer manipulations clear and you should use them in your own work. Here is a set of figures demonstrating this function in action.

The first figure represents memory just before the algorithm starts and assumes that the list is full with three contracts (the value in 'presentMax'). Note that tempArray has been declared but contains garbage.


Figure 10.1

The second figure represents memory just after step three of the algorithm. At this point 'presentMax' has been incremented to six and 'tempArray' points to enough memory for this number of contracts.


Figure 10.2

In the next figure we see that the contracts that were in the array pointed to by 'listOfContracts' have been copied into the array pointed to by 'tempArray'. Note that there is still room in this new array.


Figure 10.3

The final figure represents memory after the last line of the algorithm. Now 'listOfContracts' also points to the new array. When the function that implements this algorithm is completed, the variable 'tempArray' will disappear since it is a local variable. Things will be back to the way they were before the function began except that 'presentMax' now holds a 6 instead of a 3 and 'listOfContracts' points to the larger array.


Figure 10.4

Top of Section Main Menu Next Section