Assignment 2, C++ Intro - Word Class
This page last updated on: Jan. 14, 2002
Contents:
Due Date
Feb. 4, 2002 by end of day
Description
Implement a Word class. This will be one of the fundamental low-level
building blocks of the dictionary spell checker project and will store
data and provide operations (including Soundex functionality) for a single
word. A Word class will have the following operations associated
with it:
-
Constructors - initialize a Word object, either
with a given initial string or without parameters
-
Destructor - deconstruct a Word object
-
IsEqual - compare two Word objects for equality (contents
are the same)
-
Compare - compare two Word objects for sorting purposes
(returns int, either less than 0, equal to 0, or greater than
0)
-
IsSoundexSame - compare two Word objects for similar
sounding pronunciation (Soundex values)
-
GetLength - return the length of the internal word string
-
GetSoundex - return a pointer to the internal soundex value
-
DebugPrint - print the internal data of the word for debugging
(non-user-interface) purposes
Other operations (including operator overloading of assignment, equality,
and stream output), will be added in the next assignment.
Requirements
There will two parts to this assignment - the source code which implements
the Word class, and a test program which tests the Word member
functions. The Word source will be separated into word.h,
which contains the class declaration, and word.cpp, which contains
the member function implementations. The test program will be called tstword.cpp.
The test data will come from one or more command line arguments. The
test program will display each of the Word objects created from
the command line arguments. The test program will exercise all of the class
public member functions (the public interface), and test boundary conditions
as well as functionality. Create Word objects on both the stack
and from the heap (using the new operator). Destroying the objects
will be performed either automatically by the compiler (if on the stack),
or explicitly using the delete operator (if on the heap). Create
at least one empty Word object and compare it with a non-empty
Word object.
Const will be used appropriately in all cases - const parameters (both
pointer and reference parameters), const member functions, and const return
types. bool return types will be used for true / false return
types.
Internals
A Word object will be represented by a class containing the following
member data:
-
Internal word length (integer)
-
Pointer to dynamic sized word string (character pointer)
-
Soundex value (fixed size character array - note that #define MAXSDX
is in soundex.h)
Internally the character pointer points to the actual word string, which
should be null character terminated (although it's not necessary to be
null character terminated, if the appropriate logic is implemented). During
object construction, non-alphabetic characters should be ignored. Case
insensitive logic should be performed for comparisons. NOTE - the easiest
and most efficient way to implement the case insensitive comparisons is
to shift all of the characters to upper or lower case during object construction,
and then do regular comparisons in the comparison functions.
The soundex value is computed by calling the soundex function
(contained inside the soundex.c
file, and declared in the soundex.h
header file). (There are alternative implementations of phonetic processing
- discuss with the instructor if interested.) (In addition to the hypertext
links in this paragraph to the source code, there are links in the teaching
index Web page.) Be sure to handle null character termination appropriately
as well as soundex function call error returns. The soundex value
should be computed in the constructor.
To allocate space for the internal word character array, the new
operator
can be used as follows in the constructor (note the square brackets in
both the new and delete operators):
mWordPtr = new char[length+1];
The delete operator will need to be used in the destructor:
delete [] mWordPtr;
The getSoundex function will return a const char pointer
to the mSoundexValue array, which could be used as an external
hash value in a Word object collection, or as a key in some other
associative array container. More usage of this soundex value in Word
object collections will be performed in future assignments.
The length will be computed in the constructor, which speeds up the
rest of the functions. isEqual should first compare lengths before
doing a strcmp on the internal word strings. The design tradeoff
is extra space for each Word
object, but faster comparison checking.
Since the soundex function is a C function, 'name-mangling'
needs to be turned off. This is performed by wrapping the #include
(or function declaration) with the keyword extern "C", as follows:
extern "C" {
#include "soundex.h"
}
Topics Learned
C++ features and general software development abilities used in this assignment
include class declarations and implementations, object instances, constructors,
destructors, new and delete
operators, const parameters
and member functions, string handling, dynamic memory handling, and pointer
handling.
This page constructed by Cliff Green,
Copyright © 1998-2000.