CSC 161 Program #3

Basic Shapes
Assigned: Weds Apr 30, 2003 Due: Wed May 14, 2003
Grade: 80 points  

Concepts

The "new" concepts covered in this program are:

  • Inheritance

  • Pure virtual functions and abstract base classes

  • Vectors

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

BTW, there is an error in the specification of the problem in the book. Can you find it?

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

Um, do the vector, reading of BasicShapes, and reporting of your BasicShapes in main(). My solution has read and report functions in my main C++ file, and I pass around vectors of BasicShapes.

 


Grading

Your program will be graded on three areas: design, quality, and function. Their description and weighting in your grade is given below.

1. Program Design (25%)

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 (25%)

I'm working on a more comprehensive style guide, but here's what I will be looking for:

  • File comments - at the top of the file, list the file name (for printing purposes), your name, the date, "CSC 161", and what the file contains
  • Class comments - describe the purpose of the class
  • Function comments - describe the purpose of each function, it's parameters and return value if any
  • Variable comments - describe the purpose of each variable
  • Block comments - describe large or complex blocks of code in your functions
  • Well-chosen names - descriptive names for classes, functions, variables, etc.
  • Proper indentation - consistent indentation, just use the built-in facilities in Visual C++, and you'll be golden.
  • Readability - nice spacing of major sections and functions, clear flow and design

3. Program function (50%)

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 leave a README file in your prog01 folder with notes you want me to read.

Good luck.

Notes

All is well... Since you are defining the specific nature of your BasicShape file, you must create at least 3 files to exercise your program.

May 1, 2003

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;

Congrats to Kim Bulmer... winner of Bill's candy as the first to find the error in the specification of the problem in our text. Can you find it? But no more candy... sorry.

May 5, 2003

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

May 9, 2003

There is 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' ;    // Require for Visual C++ bug

return ss.str() ;   // Now C-string is OK

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