Meeting 02: Bag ADT

Today we will take a look at our first ADT, how to define an interface for it in C++, and discuss possible implementations. Along the way we will get an introduction to CMake and testing using Catch.

Bag ADT

A Bag holds a finite number of objects of the same type, not necessarily distinct, with no particular ordering. It supports the following:

construct()

construct an empty bag

destroy()

destroy the bag and any contents

add( Item )

add an Item to the bag, returns true on success, else false

remove( Item )

remove a single instance of Item from the bag, returns true on success, else false

isEmpty()

returns true if the bag has no contents, else false

getCurrentSize()

returns the number of items in the bag as an integer

clear()

removes all items in the bag

getFrequencyOf( Item )

the number of times item appears in the bag

contains( Item )

returns true if at least one Item is in the bag, else false

In-class Exercise

Consider a simplified version of the Bag ADT (without toVector).

  1. Download the starter code.
  2. Define a templated class for the simplified Bag ADT in the file bag.hpp.
  3. Implement stubs for these methods in the same header file.
  4. Build your code locally as you work.
  5. Submit your bag.hpp via Canvas at the Assignment "Exercise for Meeting 2".

Requirements: