DOC PREVIEW
Yale CPSC 427 - Chapter 14: Derivation and Inheritance
School name Yale University
Pages 16

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

14 Derivation and Inheritance14.1 Playing 14.1.1 The Hangman Application14.1.2 Hangman: The Main Program14.1.3 Call Graphs14.1.4 UML Diagrams: A View of the Class Relationships14.1.5 The Hangman Data Structures14.2 Hangman: The Game and Board Classes14.2.1 The Game Class14.2.2 The Board Class14.3 The Word Classes14.3.1 The BaseWord Declaration14.3.2 The Derived Word Classes14.4 RandString Adapts a Reusable Data Structure14.4.1 The RandString DeclarationChapter 14: Derivation and InheritanceIn life and in understanding a complex program. . .A picture is worth a thousand words.This chapter consists of one interactive game program that uses templates, derivation, a makefile, a stringstore,a flexarray, and some C coding “tricks” that are worth knowing. The game output is presented first to familiarizeyou with how the game works. Following that are the makefile, main program, and pairs of .hpp and .cpp files,with notes on each. These are documented by several kinds of “pictures”, each of which illustrates a differentaspect of the application: a module dependency chart, a data structure diagram, UML class diagrams, and afunction call chart.14.1 PlayingThis program plays an interactive word-guessing game called hangman. In this game, the leader (the computer)selects a secret word and displays a line of dashes with one dash for each letter. The player must guess letters,one at a time, and try to figure out what the hidden word is. A sample game is included here, for those whoare unfamiliar with it. Suppose the computer chose “hippopotamus” as the secret word. The player would see:---------Constructing Hangman ----------Please enter name of vocabulary file (or ENTER to quit): vocab2.in---------- Welcome to Hangman ----------You win if you can guess the hidden word.You lose if you guess 7 wrong letters.Puzzle is: <[ _ _ _ _ _ _ _ _ _ _ _ _ ]>Letters left--> a b c d e f g h i j k l m n o p q r s t u v w x y zBad guesses---> _ _ _ _ _ _ _Guess a letter:After one wrong guess (e) and one correct guess (a), the board would look like this:You guessed ’a’. You scored!Puzzle is: <[ _ _ _ _ _ _ _ _ a _ _ _ ]>Letters left--> b c d f g h i j k l m n o p q r s t u v w x y zBad guesses---> e _ _ _ _ _ _After several more guesses (i,o,y,u,t,p,m, and finally s), the game is won and you see:Puzzle is: <[ h i p p o p o t a m u s ]>Letters left--> b c d f g j k l n q r v w x zBad guesses---> e y _ _ _ _ _Congratulations -- you win!You won 1 time out of 1 try.Type p to play another round, q to quit: q----------- Have a good day! -----------14.1.1 The Hangman ApplicationA makefile defines its application: it lists the required parts and describes the relationships among them. Theinformation in the makefile is presented graphically in Figure 14.1.165166 CHAPTER 14. DERIVATION AND INHERITANCEThe makefile.1 # Rule for building a .o file from a .cpp source file -------2 .SUFFIXES: .cpp3 .cpp.o:4 c++ -c $(CXXFLAGS) $<56 # Compile with debug option and all warnings on. ------------7 CXXFLAGS = -g -Wall89 # Object modules comprising this application ----------------10 OBJ = main.o game.o board.o sstore.o rstrings.o words_d.o tools.o1112 game: $(OBJ)13 c++ -o game $(CXXFLAGS) $(OBJ)1415 # Delete .o and exe files and force recompilation. ----------16 clean:17 rm -f $(OBJ) game1819 # Use tools source file from grandparent directory ----------20 tools.o: tools.cpp tools.hpp21 c++ -c $(CXXFLAGS) tools.cpp -o tools.o2223 # Dependencies ----------------------------------------------24 main.o: main.cpp game.hpp board.hpp flexT.hpp25 game.o: game.cpp game.hpp board.hpp rstrings.hpp sstore.hpp flexT.hpp26 board.o: board.cpp board.hpp words_d.hpp27 words_d.o: words_d.cpp words_d.hpp words.hpp tools.hpp28 rstrings.o: rstrings.cpp rstrings.hpp sstore.hpp flexT.hpp29 sstore.o: sstore.cpp sstore.hpp tools.hppmain.cppg++ -o game -Wall main.o game.o board.o words.o rstrings.o sstore.o tools.ogamegame.hppgame.cpp board.cppSourcesObjectsExecutableLinkingCompilation#inclusion#inclusiontools.otools.hpptools.cppboard.hpp baseWord.hppwords.hppwords.cppmain.o game.o board.o words.ostandard C and C++ library .o filesC & C++ .h filesrstrings.hpp sstore.hpp flexT.hpprstrings.cpp sstore.cpprstrings.o sstore.oFigure 14.1: Makefile graph for Hangman: The files and their dependencies.14.1.2 Hangman: The Main ProgramThe vocabulary file. This program is designed to construct a hangman game then play one or more roundsof it. Line 46 asks whether the user has given the name of a vocabulary file as a command-line argument. If so,that file name is sent to the Game constructor. If not, the NULL pointer signals the Game constructor to usea default file.A conditional operator is used appropriately in the argument list for the Game constructor. It tests acondition and returns something (a pointer) in either case. C syntax requires that the same type of object mustbe returned by both clauses of a conditional operator. In this program, the ? asks whether the user typed morethan one thing (the program name) on the command line. If so, the additional command field is returned (it14.1. PLAYING 167should be a file name). If not, a NULL is returned. The overall code is simplified considerably by using theconditional operator instead of an if-else statement.30 // ======================================================================31 // Hangman program: Let the user guess words from the vocabulary file.32 // A. Fischer, May 13, 2001 file: main.cpp33 #include "tools.hpp"34 #include "game.hpp"35 //-----------------------------------------------------------------------36 int main( int argc, char* argv[] )37 {38 char response; // For query, "Play again?"39 int wins = 0, rounds = 0; // For keeping score.40 const char* timeword; // For grammatical output: time, times.41 const char* tryword; // For grammatical output: try, tries.4243 cout << "\n--------- Constructing Hangman ----------\n";44 Game g( argc>1 ? argv[1] : NULL ); // Get optional file name.4546 cout << "\n---------- Welcome to Hangman ----------\n"47 "You win if you can guess the hidden word.\n"48 "You lose if you guess " << HANG_MAX << " wrong letters.\n";49 do {50 wins += g.play(); // Play one round of game.51 rounds++;52 timeword = (wins == 1) ? "time" : "times";53 tryword = (rounds == 1) ? "try" : "tries";54 cout << "\nYou won " << wins << " " << timeword55 << " out of " << rounds << " " << tryword56 << ".\n\nType p to play another round, q to quit: ";57 cin >> response;58 } while


View Full Document

Yale CPSC 427 - Chapter 14: Derivation and Inheritance

Download Chapter 14: Derivation and Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 14: Derivation and Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 14: Derivation and Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?