The objective of this assignment is to practice writing C++ template classes.
Often times we wish to store a set of objects in such a way that they are classified according to a certain category or categories. For a particular category, objects are classified in that category by mapping each one to a certain classification value. For instance, a set of objects representing students in a course may be classified according to year of education (freshman, sophomore, junior, or senior), or according to grade in the course (A, B, C, D, F). Typically, we may wish to retrieve all of the objects which share the same classification with respect to a particular category (e.g. retrieving all students who are juniors or all students who received a B in the course). Or we may simply wish to determine the number of objects which share a particular classification in a certain category (e.g. how many students are juniors, or how many students got a B in the course). Or we may wish to ask for a particular object's classification values over all categories (e.g. ask for the grade and year of a particular student). Since we may wish to implement such a container for different data types, as well as having different types to represent the classification value, it is appropriate to use a template class.
You will implement the class whose definition is shown below. You may add whatever private members and functions you see fit to this class; you may not change the public section of the class in any way (except that you may omit or change member function parameter names, and you need not include exactly the same comments). We will be linking your compiled code in with our own main function for testing purposes, so you need not submit a main function definition; however, you will probably want to write one for yourself to help with testing your class implementations.
Classifier Template Class
This class will be parameterized by 2 types: the type of object being classified (ValueType) and the type representing the classification value (CValType). A single Classifier object will be able to classify according to a number of categories. A category is simply represented by its classifier function, i.e. a pointer to a function which maps a value of type ValueType to a classification of type CValType. Your Classifier object will accept new categories at run time and manage all of its objects according to each category. Each time a category function is added, your Classifier object will return a unique integer ID value which can be used later to identify the category. Also, your class will implement an iterator type that will be used to access the values in the categories. Finally, note that all ValueType objects stored in a Classifier object must be unique.
template < class ValueType, class
CValType >
class Classifier
{
// You may assume that the ValueType class implements the < , << , ==, and != operators.
// You may assume that the CValType class implements the ==, and != operators.
public:
typedef CValType (*CategoryFunc) ( const ValueType & ); // Specification for
classifier
// function type
Classifier ( ); // constructs a new, empty Classifier,
with no values or categories
~Classifier( ); // destroys any dynamically-allocated memory
int NumElements ( ) const; // returns the number of stored values
int NumCategories ( ) const; // returns the number of stored categories
int AddCategory ( CategoryFunc f ); // return an ID value to later identify the function
// IDs must be sequential (0, 1, 2, ...)
bool Insert ( const ValueType& value ); // Insert a value, if the value isn't already
// stored in the Classifier object.
// Return value indicates whether insertion could be
// performed.
bool Remove ( const ValueType& value ); // Remove the given value, if it exists in the
// Classifier object currently.
// Return success / failure as above.
bool Replace ( const ValueType& old_value, const ValueType& new_value);
// Replace the old value, if it exists, with the
// new value. If it does not exist, simply insert.
// Return true if an old value was replaced;
// otherwise false.
bool GetClassifications ( const ValueType& value, CValType cvals[] );
// Fill the array passed in cvals with the
// classifications for value; index i represents
// category with ID i. If value is not already
// stored in the Classifier object, store it first and
// return false; otherwise, return true.
bool Has ( const ValueType& value ) const; // returns true if and only if value exists in the
// Classifier object
int NumValues( int categoryID, CValType cval ); // Returns number of objects classified as
// cval in category given by categoryID.
// You may assume that categoryID is valid
typedef / class ??? Iterator;
// Define your own Iterator type; this can be a fundamental type or
// a user-defined class, depending on how you implement your Classifier
// object. This will be used below to allow access to the values with a
// particular classification in a particular category; it must have at least:
// - a prefix increment operator ( operator++ ( ) )
// - a prefix decrement operator ( operator-- ( ) )
// - a dereference operator ( operator* ( ) ) which
// returns by const reference to ValueType (cannot change
// values in Classifier object)
// - an equality operator ( operator== ( ) )
// - an inequality operator ( operator!= ( ) )
// Iterator must also be able to point to one-past end
// Iterator must point to objects in increasing order of ValueType
// Note: your Iterator need not still be valid if changes are made to the
Classifier object
// after an Iterator is constructed from it.
pair <Iterator, Iterator> GetValues( int categoryID, CValType cval );
// returns beginning and ending Iterators
// into the range of all values classified
// as cval in the category given by
// categoryID. First Iterator in pair is the
// beginning iterator; ending iterator must
// point to one-past-end.
// You may assume that categoryID is valid.
};
The homework portion of this project will be worth 10% of the project grade. For the homework, create a plain text file (end the filename in .txt) which includes the prototypes of any functions you intend to use that are not listed here, the class definition you intend to use (include private members/functions), and a one- to two-paragraph (approximately) description of how you intend to implement the member functions. You may use pseudocode if you wish. (This file is not intended to be compiled; it is only for you to express the design of your program.) Your homework will be graded both on the merits of your design and on the extent to which you follow that design in the implementation of your program. Remember to submit your homework file by the homework due date. Late homework submissions are not accepted.
You must include the following statement in a comment section of your program:
I have read and I understand the course policy on cheating. By submitting the following program, I am stating that the program was produced by my individual effort.
Turn in your homework and program using the UNIX submit utility, by using the following command at the unix prompt:
submit cs202_02 < hw5 | proj5 > filename
where filename is the name of the file you wish to submit. Remember to submit a plain text file for the homework assignment, your C++ source files for the project, and your makefile. After entering this command, you should get a confirmation that submit was successful:
Submitting filename...OK
You can check your submission by entering the command:
submitls cs202_02 < hw5 | proj5 >
This will show the names of all files you have submitted for this homework / project, along with the size and submission date and time of each.
This project is due by midnight (1 minute after 11:59pm) on Sunday, 12 May 2002. We will use the system clock as the final arbiter of time and date. Projects turned in up to 24 hours late will receive a 10% penalty; projects submitted between 24 and 48 hours late will receive a 25% penalty. Projects will not be accepted past 48 hours after the due date. Late homework submissions are not accepted.