Lecture 10

advertisement
Lecture 10



Log into Linux
Copy files on csserver in
/home/hwang/cs215/lecture10/*.*
Need schedule next week's practical exam.
Wednesday 3-5pm?

Project 2 posted to the course webpage.

Questions about Project 1 or Homework 5?
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
1
Outline

Container classes

Example class: bag

Using typedef

Static member constant

Algorithm analysis
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
2
Container Types



A common ADT is a container that holds a
collection of objects.
Typically, a container ADT has operations for
adding and removing elements from its
collection, for counting its elements, for
searching for an element, and so forth.
The difference in container ADTs is related to
the relative speed of the operations and the
restrictions on how elements are added or
removed.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
3
Example: Bag ADT



A bag object is a container that models a realworld bag. Like a real-world bag, it may contain
more than one instance of the same item.
Unlike a real-world bag, all of the items must be
of the same type. For example a bag of
integers, or a bag of strings.
Like a real-world bag, today's particular bag
object has a limited capacity. When it becomes
full, a bag will not accept any new elements
without removing some first.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
4
Bag ADT Operations

The bag ADT has the following operations:




Default constructor that creates an empty bag
size – a member function that returns the number of
elements in the bag
insert – a member function that places a new element into
the bag, if there is room
count – a member function that receives an target value and
returns the number of copies of the target value that are in
the bag
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
5
Bag ADT Operations




erase_one – a member function that receives a target value
and attempts to remove one copy of the target value. If it
succeeds, it returns true; otherwise it returns false.
erase – a member function that receives a target value and
removes all copies of the target value and returns the
number of copies that were removed
operator+= – a member function that receives another bag
and adds the elements of the received bag to this bag
operator+ – a free function that receives two bags and
returns a new bag that is the union of the received bags
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
6
Bag Class Attributes

Today's bag class will be implemented using
the partial array technique in which a static
array is used to hold the elements of the bag.
Thus there are two attributes:


data – an array of the element type of a fixed size
(called CAPACITY) to hold the collection
used – an integer that keeps track of the number of
used positions as elements are added to and
removed from the collection
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
7
Using typedef



To increase the reusability of a container class,
the type of the elements must easy to change.
This is done using a typedef. A typedef
statement defines a synonym for another type.
The syntax is: typedef <type> <new name>;
Since the C++ Standard Library uses
value_type for its container classes, our
example bag of integers class will, too. E.g.,
typedef int value_type;
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
8
Using typedef


A second place where typedef is useful is in
defining a size type. While any integer type
may be used, the Standard Library defines a
type called size_t (defined in <cstdlib>) that
is guaranteed to be the largest integer available
on a machine.
Once again, since the C++ Standard Library
container classes define size_type to be the
type of size variables, we will, too.
typedef std::size_t size_type;
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
9
Static Member Constant


In order to make it easier to change the
capacity of a bag, we define a named constant
CAPACITY.
The best way to implement this is by using a
static member constant. The declaration of
the constant is prefixed with the keyword static.
static const size_type CAPACITY = 10;

This is useful because all of the class's objects
will share the same constant rather than each
having its own copy.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
10
Bag Class Definition




Examine file bag1.h
Any typedefs or static constants are declared in
the public section of the class definition before
the operation prototypes.
Even though value_type currently is int, we
still pass it as a const reference parameter,
since later on it could be an class object type.
Note that size( ) and count ( ) are const
functions.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
11
Bag Class Usage




Examine file bag_demo.cpp
Demo program can be written without having
the implementation of bag class.
Demo program just asks user for integers, adds
them to a bag, and then asks the user for the
integers again and removes them from the bag.
Note the use of the CAPACITY constant.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
12
Bag Class Implementation



Examine file bag1.cpp
Static constant must be redeclared without
initialization in the source file. Note that all the
identifiers must be prefixed with the scope
operator (bag::).
Errors in usage are checked using assert()
(defined in <cassert>). It receives a boolean
expression. If the expression is true, execution
continues. If the expression is false, the
system displays a message and aborts the
program.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
13
Bag Class Implementation

The erase functions are implemented by
moving the last element into the position of the
removed element and decrementing used.

insert( ) just adds element at the end.

count( ) does a linear search.


operator+= makes a copy of the addend's used
value. This is to handle b1 += b1;
operator+ is implemented using operator+=,
which is why it can be a free function.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
14
Algorithm Analysis


Intuitive run-time analysis is covered in Section
1.2 of textbook. This explanation is slightly
more formal.
Define T(n) to be the number of steps executed
by an algorithm for input of size n. For this
class, n is the number of elements in the
bag/array, i.e. value of used variable.
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
15
Algorithm Analysis

Consider the Count operation. How many steps are
executed?








answer initialization: 1 time
for-loop initialization: 1 time
for-loop condition: n+1 times (n true, one false)
for-loop increment: n times
if-statement condition: n times
answer increment: n times in worst case
return answer: 1 time
T(n) = 4n + 4
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
16
Algorithm Analysis



T(n) is not particularly interesting. What we
really want to understand is order of
magnitude idea of how an algorithm performs.
Notation is called "Big O" and is O(f(n)) where
f(n) is usually a simple function like 1
(constant), n (linear), n2 (quadratic), n3
(cubic), log2n (logarithmic), 2n (exponential).
Formal definition is: An algorithm has
complexity of O(f(n)), if c*f(n) > T(n) for
sufficiently large n.
Generally, O(f(n)) is the largest term of T(n).
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
17
Algorithm Analysis


Since Count's T(n) = 4n + 4, it is said to be an
O(n), or a linear time, algorithm.
Look at the other operations:




Constructor, size, insert – T(n) = 1 step => O(1)
erase, erase_one – T(n) worst case is to access
every element => O(n)
operator+= - T(n) access all elements of addend
=> O(n)
operator+ - T(n) access all elements of both
operands => still O(n)
Wednesday, February 2
CS 215 Fundamentals of Programming II - Lecture 10
18
Download