Homework 5
Tom Kelliher, CS23
May 5, 1995
Trees on Wednesday.
Demonstration of:
- Class templates.
- Polymorphism.
Two files in ~kelliher/pub/cs23/hw5/
:
- Makefile
- list.h
- list.h
- Where's the source file?
- How do I include, compile?
- A list of *T, not T.
- The type T must have a void Display(void) method
associated with it.
Using:
List<Shape> MyList;
const Shape *sPtr;
...
MyList.Insert(new Circle(...), 4);
...
MyList.Retrieve(&sPtr, 4);
MyList.Delete(4) // Delete item 4 from list.
delete sPtr; // Delete item 4.
You need to implement:
- int List::Delete(int pos) --- Delete item pos from the
list. Do not delete the item itself.
- virtual void List::Display(void) --- Traverse the list of
items, calling Display() on each item.
Working with class templates.
Foundation for derived classes:
- I've assumed the Class name is Shape.
- Declares one method: void Display(void), which should be pure
virtual.
- Place declaration in shape.h, which should be included in the
.h file of each derived class and the main program file.
- Use IS-A to inherit Shape class.
- The constructor takes as parameters those things which characterize
the object:
- Circle --- A circle is characterized by the x- and y-coordinates
of its center, and its radius.
- Rectangle --- A rectangle is characterized by the x- and
y-coordinates of its lower-left and upper-right corners.
- Triangle --- A triangle is characterized by the x- and
y-coordinates of its three end-points.
- Private data members store the values.
- The Display() method prints the object type and the values
associated with the object.
- Each object needs its own .h and .cc files.
Demonstrate:
- You can insert shapes into the list.
- You can display a list.
- You can delete shapes (re-display the list).
- The provided copy constructor works --- deep copy rather than shallow
copy.
Use the Makefile.
Thomas P. Kelliher
Mon May 5 10:12:16 EDT 1997
Tom Kelliher