Unlock access to all the studying documents.
View Full Document
1
This lecture introduces container classes from Chapter 3. Before this
lecture, students should know about these items:
❐A container class is a data
type that is capable of
holding a collection of
items.
❐In C++, container classes
can be implemented as a
class, along with member
functions to add, remove,
and examine items.
Container Classes
Data Structures
and Other Objects
Using C++
2
Bags
❐For the first example,
think about a bag.
3
Bags
❐For the first example,
think about a bag.
❐Inside the bag are
some numbers.
4
This bag will be our first example of a container class, which is a class
Initial State of a Bag
❐When you first begin
to use a bag, the bag
will be empty.
❐We count on this to be
5
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
6
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
7
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
❐The bag can hold
many numbers.
8
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
❐The bag can hold
many numbers.
9
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
❐The bag can hold
many numbers.
10
At this point we have two fours and one eight in the bag of numbers.
Inserting Numbers into a Bag
❐Numbers may be
inserted into a bag.
❐The bag can hold
many numbers.
11
Examining a Bag
❐We may ask about
the contents of the
bag.
YES,
12
Removing a Number from a Bag
❐We may remove a
number from a bag.
13
Removing a Number from a Bag
❐We may remove a
number from a bag.
14
How Many Numbers
❐Another operation is
to determine how
many numbers are in a
bag.
IN MY OPINION,
THERE ARE
TOO MANY
NUMBERS.
15
We have talked about four bag operations, but we actually have five
since the process of putting a bag into its initial state counts as an
operation. This slide just summarizes the five bag operations. By the
way, which of these five operations is likely to be implemented via the
bag constructor?
…
Summary of the Bag Operations
➊A bag can be put in its initial state,
which is an empty bag.
16
Let’s start to look at the implementation of a bag as a C++ class. The
class definition begins as shown in this slide.
A question: Suppose this class definition has been completed. How
would a program declare variables for three different bags that the
program uses? Answer:
The Bag Class
❐C++ classes (introduced in
Chapter 2) can be used to
implement a container class
such as a Bag.
❐The class definition includes:
class Bag
✔ The heading of the definition
17
The Bag Class
❐C++ classes (introduced in
Chapter 2) can be used to
implement a container class
such as a Bag.
❐The class definition includes:
class Bag
{
public:
Bag( );
✔ The heading of the definition
✔ A constructor prototype
18
In the public part of the Bag class definition, we begin by listing the
constructor prototype… and then we list the prototypes for the other
The Bag Class
❐C++ classes (introduced in
Chapter 2) can be used to
implement a container class
such as a Bag.
❐The class definition includes:
class Bag
{
public:
Bag( );
void insert(…
void remove(…
…and so on
✔ The heading of the definition
✔ A constructor prototype
✔ Prototypes for public
member functions
19
Important note: The intention with a class is that the only way that an
object is manipulated is through its public operations. Even if we have
The Bag Class
❐C++ classes (introduced in
Chapter 2) can be used to
implement a container class
such as a Bag.
❐The class definition includes:
class Bag
{
public:
Bag( );
void insert(…
void remove(…
};
20
The Bag’s Default Constructor
❐Places a bag in the initial state (an empty
bag)
Bag::Bag( )
// Postcondition: The Bag has been initialized
// and it is now empty.
{
. . .
}