Professional Documents
Culture Documents
CSC212
Data
Data Structure
Structure
Lecture 4
Container Classes
2
Container
Container Classes
Classes
4
Bags
Bags
For the first example,
think about a bag.
Inside the bag are
some numbers.
5
Initial
Initial State
State of
of aa Bag
Bag
When you first begin
to use a bag, the bag
will be empty.
We count on this to be
the initial state of any
bag that we use.
THIS BAG
IS
EMPTY.
6
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
I AM
PUTTING THE
NUMBER 4
INTO THE
BAG.
7
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
THE 4 IS
IN THE
BAG.
8
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
The bag can hold
many numbers.
NOW I'M
PUTTING
ANOTHER
NUMBER IN
THE BAG --
AN 8.
9
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
The bag can hold
many numbers.
THE 8 IS
ALSO IN
THE BAG.
10
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
The bag can hold
many numbers.
We can even insert
the same number
NOW I'M
more than once. PUTTING A
SECOND 4
IN THE
BAG.
11
Inserting
Inserting Numbers
Numbers into
into aa Bag
Bag
Numbers may be
inserted into a bag.
The bag can hold
many numbers.
We can even insert
the same number NOW THE
BAG HAS
more than once. TWO 4'S
AND AN 8..
12
Examining
Examining aa Bag
Bag
We may ask about
the contents of the
bag. YES,
I HAVE
TWO OF
HAVE THEM.
YOU GOT
ANY 4's
?
13
Removing
Removing aa Number
Number from
from aa Bag
Bag
We may remove a
number from a bag.
THIS
4 IS
OUTTA
HERE!
14
Removing
Removing aa Number
Number from
from aa Bag
Bag
We may remove a
number from a bag.
But we remove only ONE 4 IS
GONE, BUT
one number at a THE OTHER
4 REMAINS.
time.
15
How
How Many
Many Numbers
Numbers
Another operation is IN MY OPINION,
to determine how THERE ARE
TOO MANY
many numbers are in a NUMBERS.
bag.
16
Summary
Summary of
of the
the Bag
Bag Operations
Operations
17
The bag
The bag Class
Class
C++ classes (introduced in class bag
Chapter 2) can be used to
implement a container class
such as a bag.
The class definition includes:
The heading of the definition
18
The bag
The bag Class
Class
C++ classes (introduced in class bag
Chapter 2) can be used to {
implement a container class public:
such as a bag. bag( );
The class definition includes:
The heading of the definition
A constructor prototype
19
The bag
The bag Class
Class
C++ classes (introduced in class bag
Chapter 2) can be used to {
implement a container class public:
such as a bag. bag( );
void insert(...
The class definition includes: void erase(...
The heading of the definition ...and so on
A constructor prototype
Prototypes for public
member functions
20
The bag
The bag Class
Class
C++ classes (introduced in class bag
Chapter 2) can be used to {
implement a container class public:
such as a bag. bag( );
void insert(...
The class definition includes: void erase(...
The heading of the definition ...and so on
A constructor prototype private:
Prototypes for public
Well
Welllook
lookat
atprivate
private
member functions
members
memberslater.
later.
Private member variables
}; 21
The bags
The bags Default
Default Constructor
Constructor
bag::bag( )
// Postcondition: The bag has been initialized
// and it is now empty.
{
...
22
The insert
The insert Function
Function
...
}
23
The size
The size Function
Function
...
}
24
The size
The size Function
Function
...
}
25
The count
The count Function
Function
...
}
26
The erase_one
The erase_one Function
Function
...
}
27
The
The Header
Header File
File and
and
Implementation
Implementation File
File
The programmer who writes
the new bag class must write bags documentation
two files:
bag1.h, a header file that
contains documentation and bags class definition
precondition/postcondition
contracts.
Everything needed to use the
Implementations of the
bag class is included in this bags member functions
comment.
29
The bag
The bag s
s Class
Class Definition
Definition
After the documentation,
the header file has the class
bags documentation
definition that weve seen
before:
bags class definition
class bag
{
public:
bag( );
void insert(... Implementations of the
void erase(...
...and so on
bags member functions
private:
30
};
The
The Implementation
Implementation File
File
As with any class, the
actual definitions of the
bags documentation
member functions are
placed in a separate
implementation file. bags class definition
The implementations of
the bags member
functions are in bag1.cpp.
Implementations of the
bags member functions
31
A
A Quiz
Quiz
32
A
A Quiz
Quiz
33
Using the bag
Using the bag in
in aa Program
Program
Here is typical code from a
program that uses the new
bag class:
bag ages;
34
Implementation
Implementation Details
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] ...
4 8 4
An array of integers
We don't care what's in
this part of the array. 35
Implementation
Implementation Details
Details
The entries may
appear in any order.
This represents the
same bag as the
previous one. . .
[0] [1] [2] [3] [4] [5] ...
4 4 8
An array of integers
We don't care what's in
this part of the array. 36
Implementation
Implementation Details
Details
. . . and this also
represents the same
bag.
8 4 4
An array of integers
We don't care what's in
this part of the array. 37
Implementation
Implementation Details
Details
We also need to keep track of how
many numbers are in the bag.
3 An integer to keep
track of the bag's size
8 4 4
An array of integers
We don't care what's in
this part of the array. 38
An
An Exercise
Exercise
One solution:
class bag
{
public:
...
private:
int data[20];
size_t used;
};
40
An
An Exercise
Exercise
class bag
{
public:
static const size_t CAPACITY = 20;
...
private:
int data[CAPACITY];
size_t used;
};
41
An
An Example
Example of Calling insert
of Calling insert
8 4
b.data
2
b.used
42
An
An Example
Example of Calling insert
of Calling insert
void bag::insert(const
Bag::insert(int new_entry)
int& new_entry)
We make a function call
b.insert(17)
2
b.used
43
An
An Example
Example of Calling insert
of Calling insert
void bag::insert(const
Bag::insert(int new_entry)
int& new_entry)
8 4 8 4 17
b.data
2 3
b.used
44
Pseudocode for bag::insert
Pseudocode for bag::insert
45
Pseudocode for bag::insert
Pseudocode for bag::insert
data[used] = new_entry;
used++;
46
Pseudocode for bag::insert
Pseudocode for bag::insert
47
The Other bag
The Other bag Operations
Operations
Read Section 3.1 for the implementations of the
other bag member functions
such as operators append (+=) and union (+)
Remember: If you are just using the bag class
then you dont need to know how the operations are
implemented.
Later we will reimplement the bag using more
efficient techniques.
Well also have a few other operations to
manipulate bags.
48
Append
Append Operator
Operator +=
+=
void bag::operator+=(const bag& addend)
// Precondition: size( ) + addend.size( ) <= CAPACITY.
// Postcondition: Each item in addend has been added to this bag.
// Library facilities used: cassert
{
size_t i;
assert(size( ) + addend.size( ) <= CAPACITY);
for (i = 0; i< addend.used; ++i)
{
data[used] = addend.data[i];
++used;
}
}
50
Union
Union Operator
Operator ++
// NONMEMBER FUNCTION for the bag class:
bag operator+(const bag& b1, const bag& b2)
// Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY.
// Postcondition: The bag returned is the union of b1 and b2.
// Library facilities used: cassert
{
bag answer;
answer += b1;
answer += b2;
return answer;
}
55
Whats
Whats the
the most
most important,
important, then?
then?
Concept of Container Classes
the bag class is not particularly important
Other kinds of container classes
sequence similar to a bag, both contain a bunch of
items. But unlike a bag, the items in a sequence is
arranged in order.
will be the topic of our second assignment paying
attention to the differences
index have current, next, last, etc
member functions and their implementation (e.g. insert, attach)
time analysis (insert)
56
After
After Class
Class
57
Summary
Summary
A container class is a class that holds a collection of items.
Container classes can be implemented with a C++ class.
The class is implemented with
a header file (containing documentation and the class definition)
bag1.h and
an implementation file (containing the implementations of the
member functions) bag1.cpp.
Other details are given in Section 3.1, which you should
read, especially the real bag code
58
Presentation copyright 1997, Addison Wesley Longman
For use with Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club
Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).
Students and instructors who use Data Structures and Other Objects Using C++ are
welcome to use this presentation however they see fit, so long as this copyright notice
remains intact.
TTHE
HE E
END
ND
59