Chapter7 - Chapter 7 IntBag(a container class A container...

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

View Full Document Right Arrow Icon
Chapter 7 – IntBag (a container class) A “container” class is a class whose main purpose is to store data items of some sort. All container classes support two basic operations - add item to container and remove item from container, and other operations (e.g. get the number of items in the container) are also normally provided. Container classes come in handy in many applications, and the C++ class libraries include a number of them. Container classes come in a variety of forms, some of which are listed below: Collection or Bag Order is unimportant Duplicate items allowed Set Order is unimportant No duplicate items Sequence or List Order is important Duplicate items may or may not be allowed As an example of the use of a container class, imagine that we are writing an academic support program which, amongst other things, must keep track of the Dean’s Honour Roll. We could, of course, use an array for this purpose, but this would be somewhat of a pain. We would have to pick an array size, for example, and somehow look after things if the list got too long for the array. And since the list is be sorted (by student name), insertions would require some effort on our part. If a new addition to the list goes anywhere but at the end, one or more students have to be shuffled down to make room. Deletions (due perhaps, to students failing SYSC 2002) will normally also require some shuffling. Now suppose a better world in which a friend (perhaps the same one you wrote the Date class) has written a list class. This allows us to do the kinds of things shown below, and is an obvious improvement (as least from our point of view – the dirty work is still has to be done somewhere). // create an initially empty list List deansHonourRoll; // to add a new student to the list deansHonourRoll.add (newStudent); // delete a student from the list (for having failed SYSC 2002) deansHonourRoll.remove (failureStudent); // output the number of students on the Dean’s Honour Roll cout << “There are ” << deansHonourRoll.size() << “ students on the honour roll.\n”; We will begin our study of container classes by considering a “bag”. This is pretty well the simplest possible container, making it a good place to start. We’ll also, for the moment, stick to a bag that can only contain integer values. In general, we’d like
Background image of page 1

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

View Full Document Right Arrow Icon
containers to be able to hold anything that we might want to put into them. In the Dean’s Honour List example, the list must contain student names (string objects). C++ makes it possible, using “templates”, to define generic containers that can be specialized for particular data types. We’ll get to templates in due course, but starting with them would be a clear case of trying to run before we can walk. A bag contains a collection of values.
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.

{[ snackBarMessage ]}

Page1 / 11

Chapter7 - Chapter 7 IntBag(a container class A container...

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