|
This is the instruction I was given from my instructor; Write the bag.cpp file, given bag.h and test11a.cpp, main program
Here is the total instructions from the text book; Write a menu-driven program that allows the user to add as many elements as desired and to see the collection of elements at any time.
This is what I have so far
#include <iostream> #include <string> using namespace std; typedef string BAG_ELEMENT_TYPE; #include "bag.h";
int main() bag(); // post: Size of this bag is 0. // Initial capacity == DEFAULT_INITIAL_BAG_CAPACITY
bag(int initCapacity); // pre: initCapacity >= 1 // post: size of this bag is bag to 0 with the capacity // to store initCapacity BAG_ELEMENT_TYPE objects
//--modifiers void add(BAG_ELEMENT_TYPE newElement); // post: Add newElement to this bag and increase // the size of this bag object increased by +1.
// Note: If capacity < size, the bag doubles it capacity
bool remove(BAG_ELEMENT_TYPE removalCandidate); // post: If found, removalCandidate is removed from this bag.
void sort (); // post: sort in ascending order //--accessors int capacity() const; // post: return the maximum number of elements that could be stored in this bag
int size() const; // post: return the number of elements that are currently in this bag // the number of objects added but not removed.
bool isEmpty () const; // post: Returns true if there are zero items in the bag. // Returns false if there is one more added elements
//--iterator functions
void first() const; // post: my_index points to the first item, // or if the bag is empty, isDone would now return true // Make sure there is at least one item in the bag //if my_size greater than 0
//Because the first() and next() are const, you need to use a // pointer to change the value of my_index so that the const // method will not complain. This is the only situation this // trick should be used to subvert the meaning of const //((bag*)this)->my_index = 0; void next() const; // post: my_index points to the next item, or isDone now returns true // Cast away const so this appears to not modify the object // This is the only situation this trick should be used to subvert the meaning of const // ((bag*)this)->my_index++;
bool isDone() const; // post: Returns true if the collection has been traversed
BAG_ELEMENT_TYPE currentItem() const; // pre: ! isDone && my_size > 0 // post: Returns the item pointed to by the my_index int occurencesOf(BAG_ELEMENT_TYPE matchValue);
|