Bag ADT using Dynamic Array – 5
February 13, 2011 Leave a comment
We now move to the constant member functions of the Bag class.
8. size_t size ( ) const; was implemented within the class definition as an inline function.
9. size_t occurrences (const Item& target) const;
This function returns the number of copies of target present in the bag.
Implementing it is straightforward.
size_t Bag:: occurrences (const Item& target) const
size_t count = 0;
for (size_t i =0; i < used; i++)
if (data[i] == target)
10. our final function is the print_bag function, which is also fairly straightforward.
void Bag :: print_bag ( ) const
cout << “Members of the bag” << endl;
for ( size_t i =0; i < used; i++)
cout << data[i] << endl;
This completes our discussion of Bag ADT using a dynamic array implementation.
For more on ADTs and related topics, one is referred to the excellent book : Data Structures and Other Objects in C++, by Savitch and Main.