CSC 172
Homework 4
Assigned March 11, due April 3, to be done solo or in teams of two
-
PSP recording: As usual. Here are the
PSP forms.
- The Programming Part:
This is a modification of your "bookstore" program from homework 3.
You are to enhance that program in two ways:
- Modify the Item class so that, instead of having a
"kind" instance variable, it has several subclasses for different
kinds of items: a Textbook subclass, a Notebook
subclass, etc. Item itself will become an "abstract
class", i.e. a class that you can't create instances of,
because it doesn't make sense to have an Item that isn't any
particular kind of Item.
- Each of these subclasses should have appropriate instance
variables: for example, the Notebook class might have two
instance variables pages and sections, to
distinguish between a 100-page 2-subject notebook and a 200-page
5-subject notebook.
However, it doesn't make sense
for the Sweatshirt class to have a pages
instance variable.
- Each of these subclasses should have a constructor that
initializes its instance variables appropriately. Your
testItems() function should call these various
constructors to create various kinds of items.
- Each of these subclasses should have a description()
or toString() method (pick a name, whichever you prefer)
that returns a string describing all the relevant
information about the item (including, for example, what kind
of item it is and the values of all of its instance variables).
In addition, the Item class should have an abstract
virtual description() method. Your
testItems method should store several different kinds
of items in Item * variables, and print the
results of their description() methods so you can see
easily whether they're correct.
- Modify the Inventory class to be a little more realistic:
- Write a description() method that returns a
string describing all the items in the inventory in
an informative and useful format. (Hint: use the
description() method you've already written for the
Item classes.)
- Write an add method that takes an Item *
parameter and adds it to the array. This will require building a
new array, one element larger than the old one, copying the contents
of the old array (together with the additional item) into the
new one, deleting the old one, renaming so future references to "the
array" refer to the new one, and updating the size. This isn't the
most efficient way to do it, but it's reasonably simple; we'll learn
more efficient ways later in the semester.
- Write a remove method that takes in the index of an
element in the array of Item pointers, and removes that
element from the array (if it exists; if not, your remove
method should do nothing at all.) One way to do this is to create a
new array one element smaller, copy all but the element to be
removed into the new array, delete the old array, rename, and update
the size. Another way is to just move things around within the old
array and update the size; this leaves some memory allocated and
unused, but it's faster.
- Add to your testInventory function several test cases
to make sure description, add, and remove
all work correctly.
- Extra credit: The description() method is
really a Java-style way of doing things. It allows you to write
Item *myNotebook = new Notebook(100, 2, 5.00); // a $5.00, 100-page,
2-subject notebook
...
cout << "My notebook is " << myNotebook->description();
A real C++ programmer would instead
write a function overriding the built-in << operator, so one
could write
Item *myNotebook = new Notebook(100, 2, 5.00); // a $5.00, 100-page,
2-subject notebook
...
cout << "My notebook is " << *myNotebook;
Read pages 612-613 (and perhaps pages 261-300) of the
Stroustrup book and write << operators for both the Item
class (and all of its subclasses) and the Inventory class. Test
them by putting statements like the above into the
testItems and testInventory functions.
You can also use my March 11
inheritance examples as a model.
-
How to turn this in:
The PSP forms are sent to me automatically when you use the on-line
forms, so don't worry about them.
Put the whole program into one folder, zip the folder, and
attach the resulting zip file to your
e-mail.
Last modified:
Tue Mar 12 14:53:05 EST 2002
Stephen Bloch / sbloch@adelphi.edu