{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter9 - Chapter 9 An Improved IntBag In the original...

Info icon This 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 …..
Image of page 1

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

View Full Document Right 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).
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern