Assignment #6, COP-3337
A dictionary stores ordered pairs containing
a key and value.
Your assignment is to implement a template class that supports the
dictionary operations.
The class interface looks as follows:
template <class KeyType, class ValueType>
class Dictionary
{
public:
// Constructors
// Destructors
// Accessors
// Mutators
private:
// Copy constructor
// Data members
Vector<KeyType> TheKeys;
Vector<ValueType> TheValues;
int NumberOfItems;
const ValueType ItemNotFound;
};
Class Specifications
The class will be templated with an arbitary KeyType
and an arbitrary ValueType.
Clearly state in a comment preamble any conditions
that must be satisfied by the template types.
One of the requirements will be that both template types
must have zero-parameter constructors.
You must also be extremely careful to use correct parameter
passing and return mechanisms, and use const
member functions when appropriate.
Data Members
The data members are an int that specifies the
number of elements in the dictionary, a ValueType
object named ItemNotFound, and parallel vectors
that store the key and corresponding values.
For 5 points extra credit, define a struct
that stores a pair, with appropriate constructors,
and consolidate the two vectors into one.
Constructors and Destructor
The constructor will be invoked with a parameter that specifies
the value that should
be returned when a Lookup fails; it uses
this parameter to set ItemNotFound.
Disable the copy constructor.
Take a reasonable course of action for the destructor, and
comment what you have done.
The initial size of both vectors is 5.
Accessors
Provide a member function named Lookup that
takes as parameter an object of type KeyType and
returns the corresponding object of type ValueType that is found in
the dictionary.
If no match is found, return
ItemNotFound.
Provide a member function named Size that returns
the current number of items in the dictionary.
This value is precisely NumberOfItems.
Mutators
Provide a member function named SetValue that
takes two parameters: a KeyType and a ValueType.
This pair is added to the dictionary.
If the KeyType that is passed to SetValue
is already in the dictionary, then the corresponding
ValueType is replaced by the new value.
SetValue may require an array expansion.
Provide a member function named RemoveValue that
takes a KeyType as a parameter and removes both it
and the corresponding ValueType from the dictionary.
The simplest way to do this is to find out where in the
array the removed items are, and then overwrite them with
the last item in the vector(s).
After that is done, decrement NumberOfItems.
Remove Value returns 1 if the remove succeeded
and 0 if it failed (which would be the case if the item
was not found).
Testing The Class
I will provide a data file
that will consist of two strings
per line (one in English, the other in Spanish).
I'll also ask you to look up some English words, and return
the corresponding Spanish word.
Your program should prompt for a name of a file, read
it line by line assuming two strings per line,
inserting entries into the dictionary,
and then prompt the user to enter
words that will be translated and output.
The dictionary would be instantiated in the following manner:
// Various #includes
int
main( )
{
Dictionary<String,String> MyDictionary( "" );
...
}
You should test the RemoveValue member separately
and make sure it works.
Separate Compilation Rules
You must do everything by the book:
create a Dictionary.h,
Dictionary.cpp, and a program6.cpp.
Provide a Makefile.
you may not using the #include statement
to include .cpp files.
NOTE: A major problem with other compilers is that
they do not correctly handle separate compilation
of templates.
You might have development problems if you use
Borland or Visual C++ compilers, espcially old versions.
What to Submit
Submit your dictionary class, your test program,
and sample output.