Homework 7 (Extra Credit) - Iterators of Template Classes
Due: Thursday March 17, 2011 at noon.
NOTE: Since this is an extra credit assignment, no late submissions
will be accepted.
Coding Conventions
Use the same coding conventions as described in
Homework 1. Additionally, make sure to indent each
section of the class in the class definition.
Assignment
The purpose of this assignment is to create an iterator for the GenericList
template class that you created in Homework 6.
This will be similar to the C++ iterator for the vector class from the
standard template library (STL) described in Section 16.5 of the book.
An iterator is used to access individual elements within a container class
(a class that contains a set of values). They are essentially wrappers around
pointers to elements in the container class. Since GenericList is a template
class, the iterator for GenericList will also be a template class. Iterators
can be initialized to a specific address (element in the container class) and
can then be incremented or decremented to move along the elements in the
container class (iterate over the elements, hence the name iterator).
The iterator class will have the following features:
- A private member variable for the address of the current element. This
will be a template pointer.
- The class will have following constructors:
- Default constructor - Sets the iterator's pointer to NULL.
- Constructor that takes a pointer - Sets the iterator's pointer to the given
pointer.
- Copy constructor - Copies the source's pointer to the iterator's pointer.
Both iterators will then be pointing at the same element, which is allowed
for iterators.
- There is NO destructor for the iterator. It merely points at elements, but
never actually allocates elements. Since it doesn't allocate anything with
new, it does NOT need to call delete.
- The class will have the folowing operators:
- = (assignment) - Copies the source's pointer to the iterator's pointer. NO
delete is needed since the iterator just points to existing memory and does
not allocate any memory.
- * (unary dereference) - The dereferencing operator returns the value of
the element that the iterator is pointing to (as opposed to the address of
that element).
- << (output) - Output the address of the element that the iterator is
pointing to.
- ++ (postfix increment) - Go ahead to the next address by incrementing the
iterator's pointer.
- -- (postfix decrement) - Go back to the previous address by decrementing
the iterator's pointer.
- < (less than) - Compare the pointers of the two iterator objects. Return
true if left's pointer is less than right's pointer.
The GenericList class will also need to be modified by adding two new public
member functions to support iterators. These are:
- iterator begin() - Return an iterator that contains the address of the
first element in the array (e.g. the base address of the array).
- iterator end() - Return an iterator that contains the address of the last
element in the array.
Test your iterator with a main() function that implements the following
pseudocode. This is similar to what the book does with vector iterators in
Program 16-14.
create a GenericList<int> object called list
create an Iterator<int> object called iter
for i = 0 to i < 10
call list.push_back(i)
end-for
for iter = list.begin() to iter < list.end()
cout the dereferenced iter
end-for
decrement the iter object
cout the iter object (this should print the address of the last element)
Email the completed assignment to me.