A10: Getting Classy

On This Page


Overview

During this assignment, you will:

  • Code a definition of a class
  • Create instances (objects) of the class
  • Use class functions to access and operate on an objects data
  • Use filestreams and formatting functions

Operation

When you run the program you see output like the following:

My products:
Name               Price   Qty     Value
Milk                3.95    40    158.00
Bread               2.99    30     89.70
Cheese              4.98    20     99.60
Baseball caps       7.99    50    399.50
Traditional mugs    5.99    15     89.85
Large T-shirts     12.99    60    779.40

Specifications

  1. In a file named Product.cpp, write a declaration and a definition for a class named Product. The Product class represents an item you would find in a store and contains three instance variables: a string called name, a double called price and an int called quantity. You are NOT allowed to have any more instance variables in the class than these three.
  2. Within this class, implement all the following functions following the standard naming conventions:
    1. One "get" function for each private variable that returns the current value. The name of the function must following the naming standards for "get" functions.
    2. One "set" function for each private variable that sets a new value after appropriate validation. The name of the function must following the naming standards for "set" functions. You must define the "set" functions outside the class declaration.
    3. Function getValue() that has no parameters and returns the price times the quantity.
    4. Function showData() that has no parameters and returns no values, but displays the data of the class all on one line like this:
    5. Milk                3.95    40    158.00
    6. Function loadData(ifstream& fin) that has an ifstream parameter passed by reference, reads data from the input filestream and loads it into the Product object.
  3. In same Product.cpp file, write a main() function that does the following:
    1. Instantiates at least six (6) Product objects
    2. Sets values (name, price and quantity) for 3 objects using the "set" functions or constructors
    3. Loads the data for 3 more objects from the products.txt file
    4. Displays output for all 6 objects like that shown in the Operation section.
  4. Note that there is no user input for this assignment. Input to the program is hard-coded in main and from files. Do NOT use any keyboard input streams, like cin, in this assignment. If you have a cin statement in your program, you will get a low score.
  5. You must display at least six different products of which three are from the specified file. However, you may choose any name, price and quantity for the hard-coded products as long as they are appropriate for a family-oriented store.
  6. You must use string variables for this assignment and not char[] variables.
  7. Specify all numerical values as constants. Do not use any "magic numbers".
  8. Make sure that every function declaration (prototype) in the class declaration has a block comment.

Hints:

  1. Remember that Windows text files end in "\r\n" (see lesson 9.1.3) and that getline() stops reading when it sees a '\n' (see lesson 9.2.2). Thus, if you use Windows, I recommend that you save your products.txt file using TextPad with a UNIX file format.
  2. Do not forget about using: fin.ignore(1000, '\n');
  3. Manipulators may be useful for formatting the output (see lesson 9.4.2).

Extra Credit

The following are worth extra credit points:

  1. Complete the assignment using pair programming. (1 point)
  2. Add two constructors to your program:
    1. A no-arg constructor that sets default values for all the instance variables. (1 point)
    2. An overloaded constructor that sets all three instance variables to the value of the arguments passed to the parameters. (1 point)
  3. In the main() function, use an array to store all the product objects and then use a loop to call their showData() functions. (2 points)

Make certain that your README.txt file lists any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course information page.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 2: Source code compiles with warnings
  • 0: Does not compile or wrong file turned in

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lesson(s) appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • No errors encountered during operation
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 0: Does not execute or no specifications met

Code Documentation

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

REAME.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. Product.cpp

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.

Home | WebCT | Announcements | Day Schedule | Eve Schedule
Course info | Help | FAQ's | HowTo's | Links

Last Updated: May 17 2006 @11:30:35