Class Design Exercises Solution



This programming assignment focuses on three aspects of class design: operator overloading, use of inheritance, and design of function and class templates.


  • A Computer Science Tapestry: section 9.4 (operator overloading), chapters 11 (templates), 13 (inheritance), and appendix E (operator overloading).

  • C++ Program Design: sections 9.3 through 9.5 (operator overloading), 10.6 (iterators), 12.9, and chapters 13 (inheritance) and 14 (templates and polymorphism) except for material on multiple inheritance.

  • Computing Fundamentals with C++: section 11.2 (iterators) and chapters 16 (inheritance), 17 (templates), and 18 (operator overloading).

It is important to understand the difference between using a friend function and using a member. If you are still unclear about their uses and syntactic differences, This page might help.

Starter Code

Please download the starter code from above.

Your Task

Do all two of the programming exercises described below.

Exercise 1: Cat and Mouse

You are to complete the project based on the flow-of-control project you wrote by adding a module that uses inheritance.

You are to provide files animals.h and animals.cpp that define classes Mouse, Cat, and Person. A Mouse object doesn’t “chase” anything; it merely moves counterclockwise around the statue, one meter per call to Chase. Therefore, use the mouse’s radius as the statue’s radius. A Cat object has a Mouse object as its target. If the cat sees its target, it moves one meter toward the statue; otherwise it circles 1.25 meters counterclockwise around the statue. A Person object is trying to photograph the situation. It doesn’t try to capture anything. If it sees its target, it doesn’t move; otherwise it circles 2 meters clockwise around the statue.

Mouse, Cat, and Person are to be derived from class Animal. Provide enough diagnostic output in each member function so that a tutor can see whether it’s performing as specified. Test your program in the same way you did for the flow-of-control project. Your program should also adhere to standards described in the section “Style guidelines” distributed earlier this semester.

All starter files provided for this exercise are listed as below:

  • cat+mouse.cpp: This contains the main program, which constructs a “scene” of animals running around the statue and runs the simulation. The simulation stops when one of the animals in the scene captures its target. The code doesn’t do much error checking. Don’t worry about that; just use it unmodified.

  • park.h and park.cpp: These comprise the module for the scene and the basic animal object. The Scene constructor prompts the user for the name, animal type, starting position, and target object for each animal in the simulation. The Animal class is the base class for simulation animals. It defines a virtual Chase method that Animal objects use to move.

  • positions.h: This comprises a module for positions similar to what you wrote for the “Flow of Control Project” assignment. ( You should make very minimal changes to position.h to finish this assignment. Our solution makes only one change to this header.)


  • Correctly working code.

  • No changes to existing code.

  • Inheritance used as specified.

  • Testing as specified.

  • Adherence to CS 9F style standards:

    • appropriate use of indenting and white space;

    • avoidance of “forbidden C++”;

    • variable and function names that reflect their use;

    • informative comments at the head of each function;

    • no routine more than twenty-four lines of code.

Exercise 2: SortedList

This exercise has three parts, involving modifications to code in sorted.lists.h.

  1. The headers for theSortedList member functions have been omitted. Supply them.

  2. Add a member function that overloads the assignment operator (“=”) for sorted lists. Your function should first delete all the ListNode objects in the variable being assigned to (the left hand side of the =). It should then construct a copy of the list on the right hand side of the = to assign to the variable on the left hand side. (Be prepared to explain to a tutor how this behavior differs from the default assignment behavior.)

  3. Provide a SortedListIterator class that allows elements of a sorted list to be returned, one by one. The SortedListIterator class will have three member functions: (1) a constructor; (2) a boolean MoreRemain function, which says whether more elements of the list remain to be generated; (3) a Next function, which returns the next element in the list.

You’ll also need to make it a friend to the SortedList and ListNode classes. (Tutors may ask you why.)

Here’s an example of how the SortedListIterator functions should work. Suppose that the contents of the variable list are as shown in the diagram below.

Then the code

iter = new SortedListIterator<int>(list); while (iter->MoreRemain()) {   cout << " " << iter->Next(); }

should produce the output

1 5 7

The SortedListIterator class is separate from the SortedList class to simplify having multiple iterators.

The file sltest.cpp contains a test program that exercises all this code. A listing of the program appears on the following pages. It should produce the output given below; your output must match it.

List 1: 3 5 7 10 List 2: 4 6 8 11 Should get operator= call here deleting elements of list 1. *** in operator=, destroying:  3 5 7 10 List 1 after replacement: 4 6 8 11 Error msg should result here from assigning list 2 to itself. *** Assigning a list to itself. Should get copy constructor call here. *** in copy constructor Should get operator= call here deleting old elements of list 1. *** in operator=, destroying:  4 6 8 11 List 1 after assignment from list2: 4 6 8 11 List 2 after assignment: 4 6 8 11 Pairs of list 2 elements: (6 4)  (8 4)  (8 6)  (11 4)  (11 6)  (11 8) Should now get three destructor calls, one each for list1, list2, and list3. *** in destructor, destroying:  4 6 8 11 *** in destructor, destroying:  4 6 8 11 *** in destructor, destroying:  4 6 8 11

In case you’re wondering why functions appear in a .h file, it’s because of the class template. The designer of C++, Bjarne Stroustrup notes in The Design and Evolution of C++ (Addison-Wesley, 1994) that “templates don’t fit neatly into this picture [the division of programs into .h and .cpp files]. … A template isn’t just source code (what is instantiated from a template is more like traditional source code), so template definitions don’t quite belong in .cpp files. On the other hand, templates are not just types and interface information, so they don’t quite belong in .h files either.” For CS 9F, we decided that putting template code in a .h file was less confusing than #include‘ing a .cpp file.


  • Correct function headers.

  • Correct overload of =, and explanation of why overloading assignment is necessary to produce the desired behavior.

  • Correctly implemented SortedListIterator class.

  • Output that matches the output given above.

  • Listings of program text, tests, and test results.

  • Adherence to CS 9F style standards:

    • appropriate use of indenting and white space;

    • avoidance of “forbidden C++”;

    • variable and function names that reflect their use;

    • informative comments at the head of each function;

    • no routine more than twenty-four lines of code.