DOC PREVIEW
Duke CPS 006 - Designing and Using Classes

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:

Designing and Using ClassesImplementing ClassesPlaying Hangman, toward a prototypeNouns, scenarios, verbsImplementing WordSourcewordsource.h, wordsource.cppGuessing lettersletters.hTesting and implementing letters.cpphang.cpp, the main/testing programProgramming Tips, Heuristics, HelpCommon interfaces are a good thingReuse concepts as well as codeRandom walksWalking behavior (see frogwalk2.cpp)Two-dimensional walkerA Computer Science Tapestry7.1Designing and Using ClassesClass implementation, summary of what we’ve seenData is private and is accessible in each member functionEach object has it’s own data, so that each of five Dice objects has its own mySides and myRollCountMember function implementations are in a .cpp file, interface is in a .h fileCompiling and linking, interface and implementationClient programs #include a .h file, this is the interfaceClient programs link the implementation, which is a compiled version of the .cpp file (.o or .obj suffix), implementations are often combined in a library, e.g., libtapestry, and the library is linkedA Computer Science Tapestry7.2Implementing ClassesDetermining what classes are needed, and how they should be implemented is difficult; designing functions is difficultExperience is a good teacher, failure is a good teacherGood design comes from experience, experience comes from bad designDesign and implementation combine into a cyclical process: design, implement, re-visit design, implement, test, redesign, …•Grow a working program, don’t do it all at the same timeOne design methodology says “look for nouns, those are classes”, and “look for verbs or scenarios, those are member functions”Not every noun is a class, not every verb is a methodA Computer Science Tapestry7.3Playing Hangman, toward a prototypeHangman is a word game, a player tries to guess a secret word one letter at a time, each missed letter counts against the player, after 8 or 10 or 12 misses the player is “hung”. Usually each miss results in drawing a body part on a gallows.Diagram shows four missesPart of 10-letter word is guessedWhat are nouns?What are verbs?What are scenarios? a m o s n t_ _ t _ _ a t _ _ _A Computer Science Tapestry7.4Nouns, scenarios, verbsGet a word to guessFrom another player, a dictionary, the webFrom a WordSourceShow the word to the player, let the player guess lettersThe word is displayed, then letters are revealed as guesses are madeClass Word, methods Display, Reveal, Guess, …Guess is also a noun, a letter is guessed, missed letters count against, good letters reveal, duplicate guesses don’t countGuessedLetters? Letters? Alphabet? Which is the noun?A Computer Science Tapestry7.5Implementing WordSourceWhat’s the simplest way to get a word from a WordSource so that we can test the rest of the programCan we design a class that’s simple to test with at first, but easy to make more realistic later (essence of prototyping)How can we guess pick one of several words at random once we’re ready to move towards a more realistic implementation?•Alternatives using small number of strings and a Dice?•Alternatives using a file of words?What should we do to test the WordSource class?Can we test without implementing the whole program?Test each class separately when possible, isolate mistakesA Computer Science Tapestry7.6wordsource.h, wordsource.cppWordSource will return a word, later add “from a file”#include <string>class WordSource{ public: WordSource(); string GetWord();}; // here’s the .cpp file#include "wordsource.h"WordSource::WordSource(){}string WordSource::GetWord(){ return "literature";}A Computer Science Tapestry7.7Guessing lettersPlayer guesses a letter, it’s in the word, or a miss, or has been guessed alreadyCreate a class Letters, have it report whether a letter has been guessed already, or a letter is in the word, or a missShould Letters report a miss/correct? If so, does Letters need to know the word? What are alternatives?Don’t worry about implementation, worry about behavior, or the interfaceEventually you’ll need to worry about implementing, what will be hardest/harder, how can we test without implementing hard part first?A Computer Science Tapestry7.8letters.hWe’ll construct an instance of Letters from a secret word/stringAsk Letters to display the “to be guessed word”Guess a letter, have Letters report if it’s in the wordOptionally report duplicate guesses, add this laterclass Letters{ public: Letters(const string& s); bool GuessLetter(const string& letter); void Display(); private: string myDisplay; // show this string string myString; // the secret word};A Computer Science Tapestry7.9Testing and implementing letters.cppGuessLetter uses string::find to determine miss/correctMust also “save state” so that Display shows guesses (and later so that duplicate guess detection works)Initially we can just return true/false to test, no state savedWe’ll test this version, but be thinking about what Letter s::GuessLette r must doChange state so that display shows guessed lettersUltimately, remember guesses to not penalize twiceWhat about determining when game is over?What about determining # misses so far? Who tracks?A Computer Science Tapestry7.10hang.cpp, the main/testing program#include <string>#include "prompt.h"#include "letters.h"#include "wordsource.h"int main(){ WordSource ws; string s = ws.GetWord(); Letters letters(s); while (true) { letters.Display(); s = PromptString("guess a letter"); if (letters.GuessLetter(s)) { cout << "that's in the word!!" << endl; } else { cout << "that's a miss" << endl; } }}A Computer Science Tapestry7.11Programming Tips, Heuristics, HelpDevelop a core working program, add to it slowlyIterative enhancement, test as you go, debug as you goDo the hard part first, or do the easy part firstWhich is best? It depends.Concentrate on behavior first when designing classes, then on stateState is useful for communicating between method callsIf you’re using several classes, you’ll need to modify the Makefile or your project in an IDE: Codewarrior/Visual C++A Computer Science Tapestry7.12Common interfaces are a good thingThe class WordStreamIterator iterates over a file returning one word/string at a


View Full Document

Duke CPS 006 - Designing and Using Classes

Download Designing and Using Classes
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 Designing and Using Classes 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 Designing and Using Classes 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?