CS 23
60 pts., due May 14 by 12:00pm
This programming assignment involves the use of class templates and polymorphism. You will complete a List class template and design a shape abstract base class and create three derived classes (using IS-A inheritance) from it to represent a circle, rectangle, and triangle.
You will then write a program which demonstrates that you can create a List of Shapes, insert circles, rectangles, and triangles into it, and then have each object on the list display its type and position (polymorphism).
Each of you is to complete this assignment on your own, without assistance from others, so that I can assess your programming skills as the semester draws to a close. If you need assistance, see me during office hours or schedule an appointment.
All files are in the directory ~kelliher/pub/cs23/hw5/
on keystone.
The List class template is in the file list.h. It contains the declarations and method implementations (i.e., there is no list.cc). Note that two class templates are declared: ListItem and List. The ListItem template is complete. The List template is complete except for the Display() and Delete() functions, which you must write. The Display() function visits each object on a list, invoking each object's Display() function. The Delete() function deletes the p'th object from the list (the first object is numbered 1). Use the provided Iterate() function to traverse the list (See Insert() for an example of how the Iterate function is used).
After careful reading of list.h, you will realize that the List class
template is not a List of T, but a List of *T
. Thus,
insertions are done like this:
List<Shape> MyList; ... MyList.Insert(new Circle(...), 4);The Delete() method, then, removes an object from the list, but does not delete the object. If an object is to be removed from the list and deleted, it is accomplished thusly:
const Shape *ptr; // Note carefully the required use of const. MyList.Retrieve(&ptr, 4); MyList.Delete(4); delete ptr;See the relevant sections of Carrano (end of Chapter 8), Lippman (Chapter 7), the other functions within list.h, and my solution to the simulator for descriptions, examples, and ideas.
Your Shape class should have one public method and nothing else. Since there are no data members, the class can make use of the default constructor. The class needs to declare the following function:
void Display(void)and declare it as a pure virtual function. This ensures that the function is implemented in derived classes.
The shape class should go into its own declaration file, shape.h. Since none of the methods are implemented, there is no need for an implementation file (i.e., there's no shape.cc).
From the Shape class, derive three classes:
See the relevant sections of Carrano (end of Chapter 8), Lippman (Chapters 8 and 9), and my solution to the simulator for descriptions, examples, and ideas.
Write a small program which demonstrates that you can create a List of Shapes, insert circles, rectangles, and triangles into it, and then have each object on the list display its type and characteristic data.
Your program should also demonstrate that your delete function works and that the provided copy constructor performs a deep copy rather than a shallow copy.
Again, use a Makefile (one is in the assignment directory) to build your program. Follow the same instructions given in Homework 4 for electronically submitting your program.