You receive a memo from your boss indicating that management is delighted with your work on HW2, and you have been promoted to Chief Assistant to the Assistant Chief. Hooray!
However, management has decided that it would be better if the essence of your program were converted into a standalone, reusable, C++ class, so other employees can use your work. Also, they’ve demanded many changes, because they are shiftless weasels who have no idea how much work programming really is.
For this assignment, you will write a class Words.
The interface will be in Words.h,
and the implementation will be in Words.cc.
You will turn in both files. Neither file will contain main.
This class has the concept of many guesses active at once.
The following public methods are required. You may not change their declarations.
Words(); Words(const string &dictionary_file_name); Words(const string &dictionary_file_name, const string &guess); Words(const Words &); Words &operator=(const Words &); ~Words();
void set_dictionary(const string &dictionary_file_name);
string get_dictionary() const; Words &operator=(const string &guess); Words &operator+=(const string &guess); Words &operator=(bool flag); flag is true, then matches are case-independent.
If flag is false, then matches are not case-independent.
int match_count() const; string operator[](int n) const; size().
string operator()(int n) const; operator[], but returns a reversed string,
for no reason that anyone can explain.
Its behavior is only defined for 0≤n<size().
This test program:
#include "Words.h"
#include <iostream>
using namespace std;
void dump(const Words &ww) {
cout << "There are " << ww.match_count() << " matches in "
<< ww.get_dictionary() << ":\n";
for (int i=0; i<ww.match_count(); i++)
cout << '\t' << ww[i] << '\n';
}
int main() {
Words w("common-words.txt", "_n____l_"); // dictionary & guess
dump(w); // Show the matches.
w = "e__er"; // change the guess
dump(w); // Show the matches.
w += "__z_"; // Add a guess
const Words w2 = w; // Copy the whole object
dump(w2); // Show the matches.
// Show the list of matches, with each word reversed:
cout << "And now, backwards:\n";
for (int i=0; i<w2.match_count(); i++)
cout << '\t' << w2(i) << '\n'; // w2(i), not w2[i]
return 0;
}
Should produce this output (which I’ve indented), when used with common-words.txt:
There are 3 matches in common-words.txt:
unlikely
entirely
annually
There are 3 matches in common-words.txt:
enter
eager
elder
There are 6 matches in common-words.txt:
jazz
gaze
enter
eager
elder
size
And now, backwards:
zzaj
ezag
retne
regae
redle
ezis
Words objects, and use them at
the same time. They mustn’t interfere with each other.
cout or cerr.
#include anything except Words.h. If your code
needs a given header file, then you #include it.
const-correct. For example, it must be possible
to assign a const object to a non-const object,
call match_count on a const object, etc.
Words.h must not pollute the global namespace with
using declations.
Words.h must have #include guards.
remove any temporary files.
.eof() method, you will lose several ponts.
Your code must be sufficiently commented. This includes:
int i =--j; might have a high-level comment to explain its purpose,
but not to explain predecrement.
If you have any questions about the requirements, ask. In the real world, your programming tasks will almost always be vague and incompletely specified. Same here.
Follow the directions on the homework page.
Turn in exactly two files, Words.h & Words.cc.
Do not turn in a tar or zip file.
Turn in someone else’s work.