[MSOE Homepage]

Dr. Taylor's MSOE Homepage

Courses

Unix is a Four
Letter Word

My Photo Album

My Personal Homepage

CS-321 Main page

CS-321 -- Lab 2 - Derived and Container Classes

Summer Quarter 1999



Electrical Engineering and Computer Science Department
Dr. Christopher C. Taylor

S-331, 277-7339
t a y l o r@m s o e.e d u (remove spaces)
www.msoe.edu/~taylor/

Acknowledgment

This lab was originally developed by Dr. Henry L. Welch.

Purpose

The purpose of this lab is to review and develop basic skills in the programming of C++ derived and container classes.

Preparation

Review your course materials from CS183 that pertain to the C++ mechanisms for base classes, derived classes, inheritance, and the use of the STL. You may find this glossary useful.

Background

The field of computer graphics lends itself quite well to the use of base classes and derived classes. The ability of derived classes to inherit properties from a base class so that they do not have to be rewritten is quite powerful. In addition the ability to override and customize methods from the base class and to add new methods allows a derived class to customize itself for its own needs.

The files shape.h and shape.cpp declare and implement a base class called shape that might be appropriate for generic graphical shapes. It contains the following methods.


 
Method Description
Constructors The usual host of constructors: 
No Argument 
Copy 
Assignment =
Destructor For removing dynamic data components
Draw Asks the shape to draw itself in a graphical area
Read Reads a shape from data file
Write Writes the shape to a data file
Xform Transforms the shape
Clone Allocates a new shape object, copies the current object, a returns a pointer to that object

A quick review of the code shows that it is very much a skeleton for what is needed to truly implement a shape. We will be using the shape class primarily as a convenient class type in which to refer to all shapes that have been derived from this class. As you progress through CS321 you may find it necessary to add new methods to the shape class or modify the existing ones.

The file image.h contains part of the declaration for a container class designed to hold pointers to shapes. By having it hold the base class shape pointer we can in effect have it contain any derived class shape pointer as well. Further, if we use the STL to build our data structure, we must remember that it will only be storing copies of the pointers and not the objects themselves. In effect we will be requiring that all shapes must be allocated on the heap (via new), and that once added to the container, the container will take responsibility for managing and deleting them in its destructor since the STL object will only delete the copy of the pointer.

The methods for this class are listed below.


 
 
Method Description
Constructors The usual host of constructors: 
No Argument 
Copy 
Assignment =
Destructor Delete all the shapes from the container (the STL destructor will not do this) and delete all other dynamic objects.
Add Add a new shape pointer to the container
Expose Ask all the shapes to redraw themselves
Write Output all the shapes to a data file
Read Read a set of shapes from a data file (see below)
Xform Modify all the shapes in a image the same way
Zoom Zoom in on an image
Unzoom Zoom out on an image
Reset Revert an image to its "original" state
Erase Remove all the shapes from an image

By organizing things within a container class it will make it easier for other objects to conveniently manipulate an image. Again the methods are defined in a rather sketchy form since we as yet do not know exactly what information will be needed to implement the various methods in a real graphic environment. You can expect to add to and modify this class throughout the quarter. You will notice that the data members have been left entirely up to you. One of the goals of this assignment is to have you choose an appropriate STL type for storing shapes. You may find that there are better choices than the vector or list.

Of all these methods the Read method may turn out to be the most difficult to implement. In most cases C++ can automatically pick the proper virtual method for derived class objects. All of this presupposes that the type of object is already known and created. When reading an object from a file or the terminal, we do not know what kind of object we have until after we begin reading it. This presents somewhat of a dilemma. There have been a number of suggestions on how to solve this particular problem. Often they revolve around a special set of classes called factory or foundry classes. This is a nice abstract way to do this, but you may be able to approach the problem in a simpler fashion. Essentially each shape should contain a type field (see the XEvent as one example) which should always appear first in the data file. The Read method in this case will have to input the type field and then use a construct like the switch-case to determine which type of shape it is to create and then input. This requires that the container class Read method be updated every time we create a new kind of shape. Typically then, the factory class' job would be to do this for the container class and thus isolate the container class from this level of detail.

Assignment

For this lab assignment you must do the following:

  1. Derive at least two different classes from the shape class. Have each contain different data members and different versions of the virtual methods. Implement all of the member functions. (For Draw just have a suitable message sent to cout and have Xform make some change to derived shape.)
  2. Implement the image container class. For now have Zoom, Unzoom, and Reset do nothing. Have Expose call all the shape Draws and Xform call all the shape Xforms.
  3. Develop a testing program which instantiates at least one image object and then adds a mix of the derived shapes to the container. You may find that a small menu-driven approach works best. Test all of the methods to make sure they work correctly.
  4. Maintain a log of your activities so that you can document how you are spending time on this lab assignment. When you are done you should be able to report on how long you spent on each aspect of the assignment from planning to design to implementation, documentation and testing.
  5. You will find it useful to use a Makefile to compile and link your code.

Lab report (due 5pm, June 10, 1999)

The lab report should be self-contained. That is, it should be possible for someone to understand what you did and why without seeing anything other than your report. Your report should include:

  • Purpose
  • Problem Statement
  • Procedure -- what approach you used to solve the problem
  • Documented source code (you may wish to include this at the end of your report)
  • Discussion including:
    • An explanation for your choice of STL container for the image class. Be sure to identify the strengths and weaknesses of your choice. Also explain how you solved the container Read problem discussed above.
    • A description of your two derived shape classes so that someone else knows what they are used for.
    • A tally of the number of Non-commented new Lines Of Code (NLOC) written for this lab assignment. You may use the CLC perl script on your code. It will report how many lines and statements of code you wrote. NOTE: For completely accurate results you should run it on the provided code first and then subtract that since you didn't write that code.
    • A summary of your activity log indicating how much time you spent on each phase of the assignment.
    • A narrative describing any specific problems you encountered and how you solved them.
  • Conclusions (what you learned, suggestions of how the lab could be improved, things you would have done differently, etc.)

As with any report you submit, correct spelling and grammar are required. In addition, your report should be submitted electronically following the Electronic Submission Guidelines. (You may wish to consult the sample report before submitting your report.) Be sure to keep copies of all your files, in case something gets lost. It may be wise to keep a diskette backup as well.

If you have any questions, consult the instructor.


This page was created by Dr. Christopher C. Taylor.