CS-321 Computer Graphics
Lab 2: Derived and Container Classes


Course Outcomes Addressed

Overview

The purpose of this lab is to review and renew basic skills in the programming of C++ derived and container classes. Mastery of these is important in order to complete all future labs in this course.

Preparation

Review your course materials from CS1030 that pertain to the C++ mechanisms for base classes, derived classes, inheritance, and the use of the C++ Standard Template Library (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 an abstract base class called shape that might be appropriate for generic graphical shapes. It contains the following methods.
 

Method Description
Constructors Copy - Creates a duplicate shape from another that already exists; the newly created shape is independent of the earlier one.
Single-parameter - Creates a shape from a specifying value
Destructor For removing dynamically allocated data components
operator= For assigning the value of one shape to another
Draw Commands the shape to draw itself in a graphical area
Read Reads a shape from a data file
Write Writes the shape to a data file
Clone Allocates a new shape object that is a copy of the current object and returns a pointer to the new 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 later 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.  For the purposes of this assignment you shouldn't make any changes to shape.h or shape.cpp, except for one additional data attribute.

The file image.h contains part of the declaration for a container class (called image) 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. This means 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 its copy of the pointer

The methods for the image class are listed below.

Method Description
Constructors No-argument - creates a default image (empty, containing no shapes)
Copy -  Creates a duplicate image from one that already exists; the newly-created image contains deep copies of the earlier image's shapes
Destructor Delete all the shape objects from the container (the STL destructor will not do this automatically since it only holds pointers to the shapes) and delete all other dynamic objects.
operator= Assigns an image to contain the same shapes as another image. The image object must first completely destroy it's current collection of shapes, and then create deep copies of the shapes it is being assigned from the other image.
Add Add a new shape (via a pointer) to the container
ReDraw Ask all the shapes to draw themselves by iterating through the image's collection of shapes, and invoking each shape's Draw() method via the pointer
Write Output all the shapes to a data file
Read Read a set of shapes from a data file
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 and return any dynamic memory they occupy

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 term. Note that the definition of appropriate data members has 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, but 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 and often 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 UML diagram below) which should always appear first in the data file. The Read method in this case will have to first input the type field and then use a construct like switch-case to determine which type of shape it is to create and then input the remaining data. This requires that the container class Read method be updated every time you create a new kind of shape. (Typically a factory class's job would be to do this for the container class and thus isolate the container class from this level of detail.) 

Lab Activity

For this lab assignment, follow the instructions below. The green instructions are required to get an "A"-level grade; otherwise, the maximum grade you'll be able to achieve for this assignment will be a "B". 
  1. You will find it useful to use a qmake project file to facilitate compiling and linking. Copy lab1.pro from lab1 to your local working directory for lab 2 (renaming it lab2.pro), make the necessary changes to use the files for the current lab. Compile and link your program using the commands qmake lab2 and make. Use this cs321lab2.cpp file as your main program and the basis for writing tests for your classes.
     

  2. Derive at least two different classes from the shape class. (point and line are two mandatory ones). Derive a third class such as rectangle, ellipse, circle, etc).

  3. Implement the image container class.
  4. Develop a driver program for testing purposes. The driver program should
  5. Using Enterprise Architect, complete the UML class diagram above, including all attributes and operations in all of the classes. Demonstrate at least the incomplete diagram above as an EA model during the 2nd week of this lab.

    Thoroughly test all of the methods in the image class to make sure they work correctly. Pay special attention to the cases of when you copy an image (either through the copy constructor or operator=) and then destroy the original, because if you don't correctly implement deep copying, you'll see big problems. This will help test your Clone() methods; i.e., to make sure you didn't just copy a pointer (shallow copy), but rather copied the entire object (deep copy).
     

Lab report and submission (due 11:00 A.M. day of Lab 3)

This is a 2 week lab. You must demonstrate the first two features indicated above during the 2nd lab period. During lab 3, you must demonstrate your finished working program as well as your completed EA model.

You must submit a quality lab report along with your program. Use this Word document as a template for your report, carefully following the instructions within. Note that you may be able to use OpenOffice (under Kubuntu) to edit this document and use file sharing to transfer the report and your source files to Windows.

Submit your assignment following these instructions:
  1. Navigate to your project directory where you have your source files. Besides your report, I only want the .cpp, .h, and .pro files.
  2. Use webCT to submit your assignment ("Lab 2").
  3. Record the time (in minutes) you spent on this lab in the FAST system for weeks 2 and 3. Make sure you make an additional entry for last week's lab while you're at it.
Be sure to keep copies of all your files, in case something gets lost.

Your lab grade will be determined by the following factors:

Program quality Report quality

If you have any questions, consult me.


Glossary

You may find some of these terms useful in reviewing derived classes and inheritance.

This lab was originally developed by Prof. Henry Welch.

This page was last updated on 09/26/2006.