Homework 5

Tom Kelliher, CS23

May 5, 1995

Trees on Wednesday.

Homework 5

Demonstration of:

  1. Class templates.

  2. Polymorphism.

Two files in ~kelliher/pub/cs23/hw5/ :

  1. Makefile

  2. list.h

List Class Template

  1. list.h

  2. Where's the source file?

  3. How do I include, compile?

  4. A list of *T, not T.

  5. 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:

  1. int List::Delete(int pos) --- Delete item pos from the list. Do not delete the item itself.

  2. virtual void List::Display(void) --- Traverse the list of items, calling Display() on each item.

Working with class templates.

Shape Abstract Base Class

Foundation for derived classes:

  1. I've assumed the Class name is Shape.

  2. Declares one method: void Display(void), which should be pure virtual.

  3. Place declaration in shape.h, which should be included in the .h file of each derived class and the main program file.

Classes Derived from Shape

  1. Use IS-A to inherit Shape class.

  2. The constructor takes as parameters those things which characterize the object:
    1. Circle --- A circle is characterized by the x- and y-coordinates of its center, and its radius.

    2. Rectangle --- A rectangle is characterized by the x- and y-coordinates of its lower-left and upper-right corners.

    3. Triangle --- A triangle is characterized by the x- and y-coordinates of its three end-points.

  3. Private data members store the values.

  4. The Display() method prints the object type and the values associated with the object.

  5. Each object needs its own .h and .cc files.

main()

Demonstrate:

  1. You can insert shapes into the list.

  2. You can display a list.

  3. You can delete shapes (re-display the list).

  4. 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