This assignment will give you practice writing constructors and destructors, static data members and static member functions, and friend functions. You are to create a Dictionary application to store words. You will allocate memory dynamically to store each word. Before you store each word, you need to check in the Dictionary to make sure that the word is not already in the Dictionary. Follow all directions listed below.
Create a Word class with the following:
- The class should contain two data members:
- a char* data member, ptrWord.
- a static int member, WordCount that contains the number of words added to the Dictionary.
- Three constructors:
- A default constructor that allocates memory for a one element char array, initialized to '\0'. (this will not get used in your final program)
- A constructor with a const char* argument. This constructor should allocate memory dynamically to store the argument. This is the constructor that will be used to store your words.
- A copy constructor. (this will not get used in your final program)
- A destructor to perform the necessary release of memory.
- A print() function the displays the word. It should be a const member function and define it inline.
- Another const member function, GetWord() that returns the char*, ptrWord.
- A static member function, GetWordCount() that returns the WordCount.
Create a Dictionary class with the following:
- One data member, words, that is a 100 element array of pointers to Word.
- A default constructor that initializes the 100 Word pointers to 0.
- A destructor that releases memory for each Word added to the Dictionary.
- A const member function, FindWord(char*), that returns a pointer to the Word if it is in the Dictionary, otherwise a null pointer.
- A function, AddWord(char*), that is used to add words to the Dictionary. AddWord should allocate memory dynamically for each word to be added. (Hint: a Word constructor should help you out here) Use the FindWord() function to make sure that you are not adding a word into the Dictionary that is already there. AddWord should return an int, 1, if the word is successfully added, otherwise 0.
- A print() const member function that prints out all words stored in the Dictionary.
- A friend function, void print(const Dictionary&, int n), that prints out the nth word in the Dictionary.
Use the following main() to test your program:
int main (void)
{
Dictionary Webster;
int i;
char temp[25];
cout << "Enter 10 words separated by whitespace\n";
for (i = 0; i < 10; i++) {
cin >> temp;
Webster.AddWord(temp);
}
Webster.print();
cout << "The fifth word is " ; print(Webster,4);
cout << "There are " <<Word::GetWordCount()<<" words in the Dictionary\n";
return 0;
}
The output should look something like this:
Enter 10 words separated by whitespace
dog cat bird mouse goat horse dog pig fish Dog
* Error: duplicate word: dog
The Dictionary contains:
dog
cat
bird
mouse
goat
horse
pig
fish
Dog
The fifth word is goat
There are 9 words in the Dictionary
Extra Credit (1 point each)
Do not attempt this unless you first complete the required assignment and totally understand what you did.
- Modify the Dictionary print() function to print the words out in sorted order.
- Implement the Dictionary class as a linked list. The Word class will need to be modified to add a Word* member. You will have to modify/add other Word class member functions to treat it as a "node". The main() function should not have to be changed. The Dictionary class member function arguments should stay the same, but the code should change.
沒有留言:
張貼留言