CSC 161 Program #3

Basic Shapes
Assigned: Fri Jul 11, 2003 Due: Fri Jul 25, 2003
Grade: 75 points  

Concepts

The "new" concepts covered in this program are:

  • Inheritance

  • Pure virtual functions and abstract base classes

  • Vectors

  • strstream objects

Description

Complete "Programming Challenge" 8 on page 906 of our textbook, that I like to call "BasicShapes"... plus a couple tiny additions:
  • PC 8 says to "create a driver program that"... Instead of that, let's read in a file of BasicShapes. In addition to reporting the area of each, also report the total area of all the BasicShapes in the file.
  • Please also define a Square class, that is derived from the Rectangle. Use a consistent approach when adding new BasicShapes.
  • Please add another BasicShape of your choosing: rhombus, trapezoid, oval, isosceles right triangle, whatever... 
  • Please add another virtual function to the BasicShape class with the following prototype:

string description() ;

The return value of description should be a string adequately describing what the BasicShape is and its parameters. For example, the description of a Square might be:

"Square with side length of 10"

So, when reporting the area of a BasicShape use both the description and the getArea function, like this:

Square with side length of 10 has area of 100

The user interface to your program is very simple:

  1. Read in a file of BasicShapes
  2. Report the description and area of each BasicShape
Reading BasicShape Files

So, you're going to read in a file of BasicShapes. You can define your own file format and function(s), within the following guidelines:

  • The format of the file must be CSV, or comma separated values.
  • Don't put the number of BasicShapes at the top of the file. Just read until you hit EOF.
  • Store all the BasicShapes that are created during your read in a vector object, as defined in the C++ Standard Template Library (STL). Vectors are a kind of automatically expanding array. You should read up on these guys on pages 540-552 in out text. 

Once you have a vector full of BasicShapes, then you should report the description and area of each.

Implementation

When using the vector for this assignment, create a vector of BasicShape pointers, not the actual objects. Since BasicShape is an abstract data class, it can't be used in a vector, and the compiler will go berzerk if you try (it's fun). So, this is probably what most people will want:

vector<BasicShape *> shapes;

In the virtual function description, that I asked for, you can return a C-string, rather than a C++ string, if you like.

BTW, strstream objects are a handy way to implement your description functions for each BasicShape derived class.

There is, however, a bug in the Microsoft Visual C++ implementation of the strstream object. The C-string returned by the str member function of strstream is not correct. You need to terminate that C-string with a '\0' character after all your formatting is complete. Here's an example:

strstream ss ;
// do your formatting
ss << '\0' ;    // Required for Visual C++ bug
return ss.str() ;   // Now C-string is OK

 


Grading

Your program will be graded on three areas: design, quality, and function. Please reference the "CSC 161 C++ Design and Coding Guidelines" posted on the site for the specific details on each of these three areas.

1. Program Design (20%, 15 points)

It is essential to design your program before pounding it out in front of Visual C++. The design for this program requires:

  • A header each class that you design with member variables and functions defined and commented.
  • A pseudo-code description of main().

You should include all the class interface files in your main() and be able to compile, but not build (or link) because your class member functions are not yet written.

2. Program quality (20%, 15 points)

Please reference the 10-step quality checklist.

3. Program function (60%, 45 points)

If your program doesn't compile or link, your grade for this section will automatically be cut in half and then I'll start from there.

Please create at least 3 Shapes files to exercise your program.

Good luck.

Notes

Jul 11, 2003

I know that we have some people with "older" versions of the text book. Given this, I have copied to pages in the book describing the assignment and put them on top of the file cabinet outside my office door. I'll hand these out in class as well.