About Hangman
  1. Question: What does it mean and why would class Alphabet be declared like this:
            class Alphabet : public Base_Word { ??

    Answer: Class Alphabet is derived from class Base_Word by public derivation. This means that:

    • The first part of an Alphabet object is a Base_Word object.
    • In addition, Alphabet could have more members.
    • Public derivation means that the public/protected/private status of all the members in class Alphabet is the same as in class Base_Word.
    • In this case, two new classes are derived from the old one so that the two new classes can have the same structure but different print functions.
  2. Question: In the hangman program, there are two instances of class Alphabet and one of Hang_word in class Board:
            Alphabet alpha;
            Alphabet errors;
            Hang_word puzzle;

    The Board::Board constructor has a ctor for all three:
            Board::Board(const char* a, const char* puz) :
                 Errcnt(0), Found(0), Alpha(a, true), Errors(a, false), Puzzle(puz){ }

    The constructor for class Alphabet has a ctor for Base_word??? Or maybe this isn't a ctor.
            Alphabet(string st, bool on_off) : Base_word(st) { set_all(on_off); }

    Why would a derived class need its parent class in the constructor?

    The Base_word component of the Alphabet object must be constructed and initialized at the very beginning of the process of constructing and initializing the Alphabet object. That means that the constructor for Base_word must be run. There is only one constructor for this class:
            Base_word::Base_word(string st) : len_(strlen(st)), w_(st),
            mask_(new bool[strlen(st)+1]) {}

    and this constructor requires a string argument. But we do not call the Base_word constructor directly, we call the Alphabet constructor, and IT calls the base_word constructor. When Alphabet::Alphabet calls Base_word::Base_word, it must pass in an argument. The ctor in the Alphabet constructor passes an appropriate string to the Base_word constructor.

  3. Question: In front of Board::guess, what is Board::status doing there?

    Answer: That is the type of the return value from Board::guess. I normally write the return type on a line above the function name because these types get complicated. In this case, it means that the return value is a symbol from the private enumeration named "status" that was defined inside the class Board. Since this is a private type, it cannot be used outside the Board class. In fact, it is used in the switch statement in Board::move.

  4. Question: In the Hangman program, on the top of page 7, there are three operator extension declarations: +=, [ ], and >>. I suppose the effective range for these declarations is the class String_base, and its derivations, is that right?

    Answer: If you look at the strings.hpp, you will see that += and [ ] are both public class functions. Thus, they are globally visible and can be called from anywhere in the program. In contrast, operator>> is an associated function but it is not even inside the class. It is defined in the global area of the program and is, therefore, global.

  5. Question: The only place I found possibly using "+=" is in the middle of page 8, within operator>>: vocab += buf.

    Answer: That is correct. It is used to add a new word to the existing vocabulary list.

    Question: Although the "buf" matches the required type "string s", the "+= buf" returns a string, then how does it work with the "vocab" in front of it?

    Answer: Everything defined within a class has an object of the class type as its implied parameter. When a binary operator is extended within a class, its left-hand operand must be a class object, its right-hand operand must be the type named in the prototype. Thus, operator+= must have a String_base on the left and a const string on the right.

    In the definition of operator>>, the += operator is called like this: (vocab += buf) Now, buf is a string (a char array) and vocab is a Rand_string (declared in class Game) and since class Rand_string derived from class String_base, a vocab is a kind of a String_base. So the expression makes sense and fits the prototype:
            const string String_base::operator+= ( const string s );
    The "const string" on the left is the return type; it is not the left-operand of +=.

    Question: Is the ">>" being used in the bottom of page 2 at "source >> ..." ?

    Answer: Yes, it is used to read a list of words from a source file into a vocabulary table: source >> vocab;

Last updated: 1/4/04