CSCI 112: Programming and Algorithms II
Spring 2006
Program 6: Drawing Program
Code due: 8am Monday April 24 (copied to your ecst turn-in directory, see below)
Weight 3 (the last program has a weight of 5, so it would be good to finish this one early)

Overview:

The goal of this program is to provide practice in implementing inheritance in C++.

You will create 5 shape classes:  Shape (the base class), Square, Point, Triangle, Circle, and a Grid class.  Each shape class will draw itself onto a Grid object.

I will provide the main() so you don't have to worry about parsing the program input.  I will also provide a Makefile.


The main program will read shape descriptions (square, point, triangle, circle) and draw them onto a Grid.  It will then draw the grid.  For example, the following input:

square 0 0 24
square 2 5 4
triangle 10 10
circle 5 16
point 15 3 '?'

Must produce the output:
************************
* *
* *
* ? *
* *
* **** *
* * * *
* * * *
* **** *
* *
* + *
* + + *
* +++++ *
* *
* *
* *
* oo *
* o o *
* o o *
* oo *
* *
* *
* *
************************




Program Requirements:

You must be careful to use the exact structure, functions names and filenames or your program won't compile with the main I provide.

Class Grid contains a private 60x24 (60 wide and 24 high) grid of characters (a 2D array of char).  Initially the grid will be initialized so that all characters are spaces (this is done in Grid's only constructor Grid()).  Class Grid must provide a set function so that the shapes can set  individual characters in the grid.  This function must have the prototype: bool Grid::set(int x, int y, char c).  If the (x,y) values are inside the grid, set the (x,y) character in the grid to character c and return true.  If the (x,y) values are outside the grid, do nothing and return false.  

Class Grid must also provide a print function:  void Grid::print().  This function must draw the grid to standard output.  Grid::print() should first draw all the characters in row 0 followed by an endl.  Then all the characters in row 1 followed by an endl.  And so on.

Class Shape will be used as the base class for classes Square, Triangle, Circle, and Point.  It will store the x and y location that the shape is to be drawn. It will have a single constructor that takes x and y as arguments.  It will have a single member function draw() that has a single argument, a reference to a Grid object (void draw(Grid &grid)).  This function must be a pure virtual function (learn this term, it will be on the next exam).

Class Square will inherit class Shape.  It will provide a single constructor that will have three integer arguments: x, y, and size.  It must also implement the draw() function.  The draw() function will draw a size x size square into the Grid using the character '*'. For example, if size = 4, it would draw:
****
*    *
*    *
****
The x,y will determine where in the Grid the rectangle is drawn.  For example, if x = 5, y = 6, the * in the top left corner of the square would be drawn at grid location 5,6.  The square (and all the other shapes) will draw into the grid by calling Grid::set().  The * in  upper left corner of this square would be drawn by calling grid.set(5,6,'*'). The next * would be drawn by grid.set(6,6,'*').

Class Square can be used like this:

int main()
{
Grid my_grid;
Square my_square(5,7,10);   // is located at x = 5, y = 7 and is 10x10 characters in size
my_square.draw(my_grid);
my_grid.print();
}

Class Triangle works just like class Square, but all triangles are the same size, and it uses the character '+'.  Thus, the constructor only takes x and y: Triangle::Triangle(int x, int y).  Triangles must be drawn as shown below.

  +
+ +
+++++


Position the triangle so its top is in row y and its left most + is in column x.  The # in the next diagram shows where the (x,y) point is.

# +
+ +
+++++



Class Circle works just like class Triangle, but uses 'o' (the lowercase letter o, not the number 0).  All circles are the same size and must be drawn as shown below.  Once again, put its top row in row y, and its leftmost column in col x (just like with triangle).

 oo
o o
o o
oo

Class Point's constructor takes the same (x,y) as the other shapes and a character:  Point(int x, int y, char c).  It will draw the single character (c) at the location (x,y).
  

Hints:

This program has many classes and files.  However, they are all very simple.  Once you get one shape working, creating the other shapes is easy.  Make sure you get one shape (probably best to start with the square) completely working before you start creating the others.

The origin (x = 0, y = 0) of the grid is at the upper left.  This makes the grid easy to draw.  As an example, a square of size 2 drawn at (x,y) = (0,0) would occupy the following spots in the grid:

0,0
0,1
1,0
1,1


General Requirements:
I will deduct 10 percent if your program does not compile using my Makefile on the lab machines.  Thus you must make sure to use the filenames listed below (do not capitalize your filenames), and if you are working on windows, you should compile and test your program on one of the department's suns before turning it in.

We will grade your program using another program, so if your program does not work exactly as specified below you will lose points.

The first lines of all your files (.h and .cpp) must contain:

// last name, first name
// ecst_username@ecst.csuchico.edu
// 112 Program 6: Object-oriented shapes


How to turn in code:
You must turn in the following files:
grid.h
grid.cpp
shape.h
shape.cpp
triangle.h
triangle.cpp
square.h
square.cpp
circle.h
circle.cpp
point.h
point.cpp

copy the above files into the directory:
/user/projects/csci112/p6/USERNAME
where USERNAME is your ecst username.

Note:  You will not be able to access this directory once the deadline has passed.


Late assignments:
If you do not finish by the deadline (see top for the deadline), you may turn your assignment in up to 24 hours late.  You will lose 15% for turning your assignment in 1-24 hours late.

If you turn your assignment in late, copy your assignment into the following directory
:
/user/projects/csci112/p6_late/USERNAME
Assignments will not be accepted if more than 24 hours late.