Bag ADT using Dynamic Array – 5

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)

count ++;



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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: