|
CSE 143 Spring 2001
Homework 4
Due: Electronic submission by 10 pm, Wednesday May
2. Paper receipt and written report due in quiz section on
Thursday May 3.
|
Overview
Graphics, colors, and shapes are a natural application of object-oriented
programming. For this assignment, you will work with a simple graphic package to create and draw shapes
on the screen. The technical goal is to learn more about classes using a hierarchy of shapes
as an example. We will provide you with most of the graphics code.
As with all assignments, please read the instructions all the way
through before beginning. Read them once again before you turn
in your assignment, to be sure you've left nothing out.
Concepts
- Review (from CSE142) of basic graphics, event loops
- Inheritance and derived classes
- Virtual function calls and dynamic dispatch
This assignment is intended to be relatively short. In a future
assignment, we'll build on these ideas to create a more full-featured graphics drawing
program, and there will be lots of room for creative extensions then. For
now, it is probably best to keep it simple and start thinking about more
elaborate things you might do later.
Program Specification
For this assignment, we have provided a small starter program as well as a
modified version of the GP142 graphics package that has an object-oriented
interface (more about all of these files below). When the starter program
is executed, it opens a graphic window and displays a menu or palette showing
several shapes.
The menu contains three shapes: square, triangle and circle. Then the
program enters an infinite loop, waiting for mouse clicks and
processing them.Mouse clicks
fall into two categories, depending on where on the screen they occur.
Your job is to add to the program so the program reacts appropriately.
- If the user clicks on one of the shapes in the menu, the
program should remember which shape button was clicked: a square, a triangle, or a circle.
- If the user clicks elsewhere in the window, this is a request to draw a new shape.
Draw the most recently clicked shape, centered at the location of the mouse
click, with a fixed size and color. The size and color are not
specified - pick something you like. If no shape has been chosen yet by
clicking in the menu, draw a square.
Implementation Requirements
The key goal of this assignment is to learn how to build a class
hierarchy using inheritance and how to use virtual methods. Here is the
specification of the classes you must implement.
- An abstract class Shape that defines the interface of a generic shape and contains
any code that will
be shared by all kinds of shapes. Shape should
include at least the following members:
- x and y coordinates of the center of the shape;
- virtual method draw that draws the shape in the window;
- virtual method print that prints (displays) the kind of
shape and its coordinates in the lower right corner of the drawing window.
- Derived classes Square, Triangle and
Circle which inherit from Shape and include
appropriate implementations of Shape's virtual methods.
These classes must provide constructors that have the x and y
coordinates of the center as parameters plus any other arguments that are
needed (if any).
From this assignment on, all concrete classes in your program must provide a
default constructor, a copy constructor, an assignment operator, and a
destructor. Each dynamically allocated class instance or any other
memory you may allocate dynamically must be deleted properly
after you are done using it. You may not use any global non-constant variables.
Once you have implemented the shape classes, you must implement functions to
process mouse clicks in the main part of the window. The idea here is to
test that your shape classes work properly; not to create a full-featured
graphics program. First, create a function whose parameter is a pointer to
a Shape. This function should call the draw and print methods of that Shape
so it draws itself at the appropriate location on the screen and prints a
message in the corner of the screen (methods draw and print of class Shape). The specification of this function
is:
// draw Shape s and display a message with the shape kind and coordinates
void drawPrintShape(Shape *s);
Now, implement a function to process mouse clicks in the main part of the
screen (outside the shape menu). This function should create a new,
dynamically allocated instance of the appropriate shape class (as
described in the Program Specification section, above), with x and y
coordinates taken from the location of the mouse click. After creating the
shape, draw and print the shape by calling drawPrintShape with a pointer to the
new shape as an argument. This will verify that the virtual functions in
your shape classes are working properly.
Be sure to eventually delete any
dynamically allocated data at some appropriate place in your program.
Do
not modify the GP142 files. Your program should work with the
unmodified GP142 package, and, in fact, you should not turn in the GP142 files.
Implementation Hints
Start with planning
As usual, do some planning first. Design your hierarchy of shape
classes; sketch class declarations.
Download and install the graphics code
Next download and install the graphics code that we provide for this
homework. It includes the
GP142 graphics package
and the starter code. Download all of the following files:
If you are using MSVC, create a new project which must be of type
Win32 Application. Then add all the above files to that
project. Now you should be able to compile and run the project as-is.
If you are not under MSVC, you will need to follow the instructions at
the bottom of the GP142 page following the above link. Your
installation is successfull if you are able to compile and run the program.
Familiarize yourself with the graphics code
Start by looking at hw4_starter.cpp. It contains
main() that opens a graphic window, draws a simple menu with
three shapes and enters a loop, processing mouse click events when
they occur. Check out the code that draws the shapes inside
draw_shape_menu(). For explanation and details look at the
GP142 page.
Plan the implementation of the required functionality
Given the starter code, plan what other functions you will need to
write and invoke. Only after you have done all of this should you
start coding.
GP142 hints
Note that now coordinates are integers, not doubles. Use int
for the type of all coordinate variables.
Writing to cout is not supported. Use the write
functions of the GP142Display class declared in gp142display.h.
Other hints
You will need to add code inside the handle_menu_item()
function to process the menu mouse clicks, and inside main()
to process other clicks (create your own function for that). These
places are marked with a comment.
Feel free to modify the existing code as needed, for example, to pass
additional arguments to existing functions.
As usual, if the specification is not clear, make reasonable
assumptions. Feel free to discuss issues about the specification on
the course newsgroup, in sections, with your colleagues, etc.
Testing
You should run your program to make sure it works properly.
Be sure to test each method that your classes implement; you
may need to write some additional code for that.
Report
Look back at your program and see what you have learned. What did you
find interesting? unexpected? Write a brief report answering these questions.
Electronic Submission
When you've finished your program, turn in the source program (.cpp
and .h files only) using the turnin form. You should turn in
your shape classes and program files only. Don't turn in the GP142
files. Print out
the receipt that appears, staple it and your written report together,
and hand it in during quiz section.