15-CS106-Library-Classes

15-CS106-Library-Classes - CS106X Winter 2008 Handout 15...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 15 Winter 2008 January 18, 2008 CS106 Library Classes Kudos to Julie Zelenski for this fantastic summary of the CS106 utility and container classes. This winter I’m going to try something that another lecturer successfully tried last quarter. After we cover the basics of class interface design, publicity versus privacy, constructors, destructors, and the mechanics of implementation, we’re going to put on our client caps and learn some of the utility classes written specifically for the CS106 courses. In the past, we’ve focused on both the design and the implementation of each class, one at a time, alternating between user and implementer. This time, we’re going to learn all the classes up front, internalizing their interface, their behavior, their runtime characteristics, and the situations where they come in most handy. The ideas modeled by these classes are common enough that all modern programming languages provided similar library classes—Java provides things like StringTokenizer , ArrayList , and HashMap , whereas the C++ STL provides classes like vector , set , map , and hash_map . In principle, were could just learn the STL classes, but they’re cumbersome and difficult to use for someone programming in C++ for the very first time. So we instead go with some simpler versions of the very same ideas. This way we can emphasize concept instead of syntax. Unfortunately, the reader hasn’t been updated to reflect our new stance on teaching these classes, so you’ll have to rely on this handout, the reader’s appendix, and Jerry’s brilliant explanations for a few days until we start talking about implementation. Scanner The Scanner class provides functionality for dividing a string into separate words or tokens. A scanner comes in quite handy when doing any sort of string or file processing. Here's a few ideas to get you thinking about its uses: breaking apart a command entered at the console (e.g. " turn left ", or " find price < 5 ") dividing a document into component words for textual analysis, such as a word frequency count, preparing an index, or building a concordance separating a file pathname into its components (e.g. the sub-directories within /usr/class/cs106x/WWW/handouts/01-Course-Information.pdf ) evaluating an arithmetic expression, such as 3 + 4 * 7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The basic public interface of the Scanner class is listed below: class Scanner { public: Scanner(); // constructor (invoked when allocated) ~Scanner(); // destructor (invoked when deallocated) void setInput(string str); string nextToken(); bool hasMoreTokens(); void saveToken(string token); enum spaceOptionT { PreserveSpaces, IgnoreSpaces }; void setSpaceOption(spaceOptionT option); spaceOptionT getSpaceOption(); // other advanced options excerpted for clarity }; The idiomatic pattern for using a Scanner is to create a new object, set the string to be scanned, and then enter a loop that calls ReadToken while MoreTokensExist returns true . Here's a simple example that uses the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

15-CS106-Library-Classes - CS106X Winter 2008 Handout 15...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online