CSC 112
Lecture 13
More on class templates
- In the second part of Hw 5, you rewrote Stack as a class
template. Remember that (for debugging only) you had a function
to Print the contents of a stack: could you template that
function? Why, or why not?
- Specialized templates. Suppose you have a template
that works for all data types but one. For example, the Stack
will work for integers, characters, floats, but not for CustRec,
because the general templated method Print doesn't work in that
case.
Situations like this require specialized templates. It is
as if you define a new class, say CustRecStack, but instead of
giving it a special and different name, you do instead:
class Stack<CustRec>
{
... // a complete declaration of the class members
}
The compiler would know that this version of the Stack is
intended to be used for the CustRec data type. Any time an object
is declared of the type Stack<CustRec>, the compiler will
use this template to generate the code, and not the general
templated Stack class.
- Example. To make it easier to
follow the code, I put everything in one big file, instead of
splitting it into several ones which would have been included.
Using tested object files; Libraries
- Thoroughly testing the IntStack class. Example: the
BadStack. Then, the
GoodStack.
- Using a good stack, without ever seeing the actual
code. Example:
ObjectStack.
- What are libraries? Why do we include header files?
Example, my own stack library
StackLibrary.
There will be an exam question about this. I recommend that you
study the examples done in class and make sure you understand
what is going on.
In class exercise
-
Copy the IntStack files from this directory (use the Unix cd and cp
commands to get there and to copy the files):
../../handout/Stack/
or
~112b/handout/Stack
- Use the testdriver you wrote for Hw5, put it in the same
directory with these files.
- Compile using the given object files,
using only one compilation line to construct the
executable code. You will design the Makefile, using the model we
did in class.
- Execute, test. Does it run correctly? Is my implementation
similar to yours? Does it matter?
Midterm review: topics to be covered for the midterm
exam. Summary: everything we did in class, including labs and
homeworks.
If time allows, I will answer specific questions about the
midterm exam that was made available to you in
Lecture 11.
The rest of the class is devoted to working on Hw6.
Ileana Streinu