Chapter9 - Chapter 9 An Improved IntBag In the original version of class IntBag bags always have the same capacity(10 values though some other

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 9 – An Improved IntBag In the original version of class IntBag, bags always have the same capacity (10 values, though some other value could easily have been chosen). This is obviously not very desirable. The fixed capacity might be too small in some cases, and too large (resulting in wasted space) in others. It would be much better if it was possible to specify bag capacities on a bag by bag basis, as shown below. IntBag bag1(20); // bag1 has a capacity of 20 values IntBag bag2(35); // bag2 has a capacity of 35 values IntBag bag3; // bag 3 has the default capacity (whatever that is) Two constructors are now needed. In addition to the default constructor (worth keeping – it doesn’t cost much to provide this option), we also need a constructor which accepts the capacity of the bag to be created. Within the class, dynamic array allocation is called for. Instead of containing an actual array, objects will contain a pointer to an array. The actual creation of this array will be looked after by the constructors. The default constructor will create an array of the default size, and the second constructor will create an array of the specified size. It is also necessary to have objects contain their capacity (i.e. another member variable is required). This is because functions would otherwise have no way of determining the capacity of an object. The “add” function, for example, would have no way of telling whether a bag was or was not full. The relevant portions of a modified header file are shown below. Note that constant “MAXVALUES” has disappeared. class IntBag { private: int *data; // pointer to dynamically allocated array int capacity; // the size of this array int count; // number of values currently in the bag bool walkInProgress; int walkPosition; // position of last value returned public: // Constructs an empty bag having the default capacity. IntBag (); // Constructs an empty bag having the specified capacity. // Throws a range_error exception if the capacity is unreasonable. IntBag (int capacity); // the remainder of the header file is unchanged …. .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Because the default capacity isn’t actually used anywhere in the header file, there is no need to define it there. Instead the constant can be declared in the implementation file. The “static” in the declaration below has exactly the same meaning it had in the case of “helper” functions. The constant is only accessible from within the implementation file, and no conflict will result if the same name is used elsewhere (i.e. in other files). static const int DEFAULT_CAPACITY = 10; This approach assumes that we wish to hide the default capacity from class users. If we wanted to make the default capacity visible, we would declare DEFAULT_CAPACITY in a public section of the class definition (as discussed in chapter 7). The default constructor creates an array of the default size and appropriately initializes
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 11

Chapter9 - Chapter 9 An Improved IntBag In the original version of class IntBag bags always have the same capacity(10 values though some other

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online