21
The Insert Function
Inserts a new number in the bag
void Bag::insert(int new_entry)
// Precondition: The Bag is not full.
// Postcondition: A new copy of new_entry has
// been added to the Bag.
{
. . .
}
22
The Size Function
Counts how many integers are in the bag.
int Bag::size( ) const
// Postcondition: The return value is the number
// of integers in the Bag.
{
. . .
}
23
The Size Function
Counts how many integers are in the bag.
size_t Bag::size( ) const
// Postcondition: The return value is the number
// of integers in the Bag.
{
. . .
}
24
The Occurrences Function
Counts how many copies of a number occur
size_t Bag::occurrences(int target) const
// Postcondition: The return value is the number
// of copies of target in the Bag.
{
. . .
}
25
The Remove Function
Removes one copy of a number
void Bag::remove(int target)
// Postcondition: If target was in the Bag, then
// one copy of target has been removed from the
// Bag; otherwise the Bag is unchanged.
{
. . .
}
26
Let’s take a quick look at how a program might declare and use a bag.
In this example, the program declares a bag called ages, and inserts
the three numbers 4, 8, and 4 into the ages bag.
Question: What include statement would be needed for this program to
declare and use a bag?
Using the Bag in a Program
Here is typical code from a
program that uses the new
Bag class:
Bag ages;
// Record the ages of three children:
ages.insert(4);
ages.insert(8);
ages.insert(4);
27
Let’s look at some details of the bag’s header file and implementation
file.
The Header File and
Implementation File
The programmer who writes
the new Bag class must write
two files:
bag1.h, a header file that
contains documentation and
the class definition
bag1.cxx, an implementation
file that contains the
implementations of the Bag’s
member functions
Bag’s documentation
Bag’s class definition
Implementations of the
Bag’s member functions
28
Documentation for the Bag Class
The documentation gives
prototypes and
specifications for the bag
member functions.
Specifications are written as
precondition/postcondition
contracts.
Everything needed to use the
Bag class is included in this
comment.
Bag’s documentation
Bag’s class definition
Implementations of the
Bag’s member functions
29
The Bag’s Class Definition
After the documentation,
the header file has the class
definition that we’ve seen
before:
Bag’s documentation
Bag’s class definition
Implementations of the
Bag’s member functions
class Bag
{
public:
Bag( );
void insert(…
void remove(…
30
The Implementation File
As with any class, the
actual definitions of the
member functions are
placed in a separate
implementation file.
Bag’s documentation
Bag’s class definition
31
A Quiz
Suppose that a Mysterious
Benefactor provides you
with the Bag class, but you
are only permitted to read
the documentation in the
Yes I can.
No. Not unless I see the
class declaration for the
Bag.
32
A Quiz
Suppose that a Mysterious
Benefactor provides you
with the Bag class, but you
are only permitted to read
the documentation in the
header file. You cannot
read the class definition or
implementation file. Can
you write a program that
uses the Bag data type ?
Yes I can.
You know the name of the
new data type, which is
enough for you to declare
Bag variables. You also
know the headings and
specifications of each of
the operations.
33
Just to finish things off, let’s look at some details of how the Bag class
could be implemented. This isn’t the only way to implement the class,
but it is a simple approach.
Implementation Details
The entries of a bag
will be stored in the
front part of an array,
as shown in this
example.
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .
An array of integers
484
We don’t care what’s in
this part of the array.
34
Implementation Details
The entries may
appear in any order.
This represents the
same bag as the
previous one. . .
An array of integers
448
We don’t care what’s in
this part of the array.
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .
35
Implementation Details
. . . and this also
represents the same
bag.
An array of integers
We don’t care what’s in
this part of the array.
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .
448