UMBC CMSC 202, Computer Science II, Spring 1999, Sections 0101, 0102, 0103, 0104

Project 5: Polymorphism

Due: Monday May 10, 1999 - No late projects accepted


Objective

The objectives of this project are 1) to use virtual functions and dynamic binding in C++, 2) to gain more experience using references, pointers and dynamic memory allocation, and 3) to continue using C++ I/O features.


Background

An important aspect of inheritance in object-oriented programming is the ability to write generic functions (or entire programs) that are capable of operating on objects of a variety of subclasses, including classes that will be written at some point in the future. The key ingredient in writing such code is to use an array of pointers to the base class type. When polymorphic (virtual) member functions are invoked through a base class pointer, the function from the appropriate class is called based on what type of derived object is actually being pointed to by the base class pointer. This is referred to as run-time polymorphism or dynamic binding.


Assignment

  1. Begin with your StringCollection class from Project 2. Make corrections and improvements as needed. Test your class thoroughly before proceeding. (You do not have to turn in your test program.)

  2. If you are not already using the bool type, change your StringCollection class so that boolean functions have return type bool rather than int.

  3. If any documentation is missing from your StringCollection.H, add it now. Keep all documentation updated as you continue to make changes to the StringCollection class.

  4. Make the following changes in StringCollection.H:

  5. In StringCollection.C, make all necessary modifications to implement the changes in the StringCollection class outlined above. (The basic structure of the code will stay the same, but you will have to use pointers and indirection in many places.) Implement Display() and << as follows:

  6. Test your new StringCollection class using the String class from Project 4. You should be able to adapt your main() from Proj2.C to use as a test program.

  7. Now the "magic" begins. We will make the StringCollection class also work for IString objects, as well as objects of any future class derived from String. Add the keyword virtual in two places in String.H: the declaration for Display() and the declaration for the destructor.

  8. Test your StringCollection class again using some IString objects. It should also work if it contains a mixture of String and IString objects.

  9. Put the following main() in a file called Proj5.C: int main() { int i; const int num_words = 13; char* words[num_words] = {"virtual", "class", "operator", "try", "friend", "this", "delete", "private", "inline", "template", "friend", "const", "new" }; String* temp[num_words]; StringCollection keywords; for (i = 0; i < num_words; ++i) { temp[i] = new IString (words[i], i); if (i == num_words - 1) { break; } if (! keywords.Add (temp[i])) { cout << words[i] << " is a duplicate, not added" << endl; } } keywords.Display(); cout << "-----------------" << endl; keywords.Sort(); cout << "After sort: " << endl; cout << keywords; cout << "----------------" << endl; int index; index = keywords.Contains (*(temp[0])); if (index == -1) { cout << "Not found" << endl; } else { keywords.Display (index); } index = keywords.Contains (*(temp[num_words - 1])); if (index == -1) { cout << "Not found" << endl; } else { keywords.Display (index); } for (i = 0; i < num_words; ++i) { delete temp[i]; } return 0; } The output should be: friend is a duplicate, not added virtual (0) class (1) operator (2) try (3) friend (4) this (5) delete (6) private (7) inline (8) template (9) const (11) ----------------- After sort: class (1) const (11) delete (6) friend (4) inline (8) operator (2) private (7) template (9) this (5) try (3) virtual (0) ---------------- virtual (0) Not found

  10. Submit a makefile and seven source files named as follows:

    The name of your executable must be Proj5.


Last Modified: 22 Apr 1999 18:00:06 EDT by Alan Baumgarten, abaumg1@cs.umbc.edu

Back up to Spring 1999 CMSC 202 Section Homepage