Lecture 4 | Programming Abstractions (Stanford)
-
0:00 - 0:11
-
0:11 - 0:14This presentation is delivered by the Stanford Center for Professional Development.
-
0:14 - 0:24
-
0:24 - 0:26Any administration questions on your mind?
-
0:26 - 0:28How many people have actually successfully installed a compiler?
-
0:28 - 0:31Have stuff working - okay, so that's like a
-
0:31 - 0:34third of you, good to know.
-
0:34 - 0:36Remaining two thirds, you
-
0:36 - 0:40want to get on it. Okay,
-
0:40 - 0:45so we started to talk about this on Monday, and I'm gonna
-
0:45 - 0:48try to finish off the things that I had started to get you thinking about;
-
0:48 - 0:52about how input/output works in C++. We've seen the simple
-
0:52 - 0:53forms of
-
0:53 - 0:57using stream insertion, the less than less than operator to push things on to cout,
-
0:57 - 0:59the Console Output Stream.
-
0:59 - 1:02A C-Out is capable of writing all the
-
1:02 - 1:06basic types that are built into C++, ants and doubles and cars and strings,
-
1:06 - 1:06right,
-
1:06 - 1:09by virtue of just sort of putting the string on the left and the thing you want on
-
1:09 - 1:10the right, it will kind of
-
1:10 - 1:14take that thing and push it out onto stream. You can chain those
-
1:14 - 1:17together with lots and lots of those < < to get a whole bunch
-
1:17 - 1:20of things, and then the endl is the - what's called stream
-
1:20 - 1:24manipulator that produces a new line, starts the next line of text, a line
-
1:24 - 1:25beneath that.
-
1:25 - 1:30The analog to that on the reading side is the stream
-
1:30 - 1:33extraction operator, which is the > >. And then when applied
-
1:33 - 1:36to an input stream it attempts to sort of take where the cursor position is in
-
1:36 - 1:42the input stream and read the next characters using the expected format
-
1:42 - 1:46given by the type of the thing you're trying to extract. So in this case
-
1:46 - 1:50what I'm saying, CN > > extract an integer here, X being an integer.
-
1:50 - 1:53What it's gonna look for in the input stream is it's going to skip
-
1:53 - 1:55over white space. So by default
-
1:55 - 1:58the stream extraction always skips over any leading white space. That means tabs,
-
1:58 - 1:59new lines,
-
1:59 - 2:02and ordinary space characters. So
-
2:02 - 2:05scans up to that, gets to the first non-space character
-
2:05 - 2:09and then starts assuming that what should be there is a number, and so
-
2:09 - 2:12number being, a sequence of digit characters. And in this case, because it's
-
2:12 - 2:16integer, it shouldn't have a dot or any of the exponentiations sort of things that
-
2:16 - 2:17a real number
-
2:17 - 2:20would. If it runs into something that's not integer, it
-
2:20 - 2:24runs into a character, it runs into a punctuation, it runs into a
-
2:24 - 2:2639.5,
-
2:26 - 2:29what happens is that the screen goes into a fail state
-
2:29 - 2:33where it says, I - you told me to expect an integer. What I read next wasn't an
-
2:33 - 2:33integer.
-
2:33 - 2:36I don't know how to make heads or tails of this. So it basically just
-
2:36 - 2:38throws up its hand.
-
2:38 - 2:39And so it -
-
2:39 - 2:43at that point the stream is - it requires you to kind of intervene,
-
2:43 - 2:45check the fail state, see that something's wrong,
-
2:45 - 2:47clear that fail state,
-
2:47 - 2:50decide what to do about it, kind of restart, and kind of pick up where you left off. It
-
2:50 - 2:52makes for kind of messy handling
-
2:52 - 2:53
-
2:53 - 2:57to have all that code kind of in your face when you're trying to do that reading,
-
2:57 - 3:01and that's actually why we've provided the things like get integer, get line and get
-
3:01 - 3:03wheel, and the simple I/O library
-
3:03 - 3:05to just manage that for you.
-
3:05 - 3:09Basically what they're doing is in a loop they're trying to read that integer
-
3:09 - 3:09
-
3:09 - 3:13off the console. And if it fails, write resetting the stream,
-
3:13 - 3:16going back around asking the user to type in
-
3:16 - 3:20- give it another try, until they get something that's well formed. So
-
3:20 - 3:22typically we're just going to use these,
-
3:22 - 3:24because they just provide conveniences. You could certainly use this, but it would just
-
3:24 - 3:28require more effort on your part to kind of manage the error conditions and retry
-
3:28 - 3:31and whatnot. So
-
3:31 - 3:35that's why it's there.
-
3:35 - 3:38The C++ file I/O; so
-
3:38 - 3:42the console is actually just a particular instance of the stream. Cout and cin
-
3:42 - 3:47are the string that's attached to the users interface console there.
-
3:47 - 3:51That the same sort of mechanism is used to read files on disks, so text files on
-
3:51 - 3:53disks that have contents you like to
-
3:53 - 3:56pull into a database, or you want to write some information out to a file, you
-
3:56 - 3:58use the file stream for that.
-
3:58 - 4:01There is a header called fstream, standard C++ header
-
4:01 - 4:02in this case, so
-
4:02 - 4:04enclosed in < >,
-
4:04 - 4:08that declares the isstream and the osstream. The input file stream for reading,
-
4:08 - 4:12the output file stream for writing.
-
4:12 - 4:14Declaring these variables; this [inaudible]
-
4:14 - 4:18just sets up a default stream that is not connected to anything on disc.
-
4:18 - 4:22Before you do anything with it you really do need to attach it to some
-
4:22 - 4:25named location, some file by name on your disk
-
4:25 - 4:27to have the right thing happen, to read from some
-
4:27 - 4:29contents, or to write the contents somewhere.
-
4:29 - 4:32The operation that does that is open,
-
4:32 - 4:34so the isstream and the osstream are objects,
-
4:34 - 4:39so dot notation is used to send messages to it. In this case, telling the
-
4:39 - 4:40input stream
-
4:40 - 4:45to open the file whose name is "names.txt."
-
4:45 - 4:48The behavior for open is to assume that you meant the file in the current
-
4:48 - 4:52directory if you don't otherwise give a more fully specified path. So
-
4:52 - 4:56this is almost always the way we're going to do this, we're just going to open a file by name. It's going to look
-
4:56 - 4:59for it in the project directory, where your code is, where you project is, so
-
4:59 - 5:01kind of right there locally.
-
5:01 - 5:04Now this will look for a file whose name is exactly names.txt,
-
5:04 - 5:09and then from that point the file positions, the kind of cursor we
-
5:09 - 5:12call it, is positioned at the beginning of the input stream. The first character read
-
5:12 - 5:16will be the first character of names.txt, and as you move forward
-
5:16 - 5:18it will read its way all the way to the end.
-
5:18 - 5:21Similarly, doing an outopen,
-
5:21 - 5:25it opens a file and kind of positions the writing at the very beginning
-
5:25 - 5:27that will - the first character written will be the first character then when
-
5:27 - 5:32you finish. And that file, they'll be written in sequence.
-
5:32 - 5:36So this is one of those places, actually, probably the only one that this
-
5:36 - 5:39direction is going to be relevant for. I talked a little bit last time about C-strings
-
5:39 - 5:42and C++ strings, and you might have been a little bit
-
5:42 - 5:43worried about why
-
5:43 - 5:45I'm telling you you need to know that both exist.
-
5:45 - 5:47And so last time I talked a little about
-
5:47 - 5:51one way in which C-strings don't do what you think, in that one case of
-
5:51 - 5:55concatenation, and how you can do a - force a conversion from the old to the new.
-
5:55 - 5:57Now, I also mentioned that there was a conversion that went in the
-
5:57 - 6:01opposite direction. You had a new string, and you wanted the old one.
-
6:01 - 6:02And
-
6:02 - 6:04one of the first questions you might ask is well why would I ever want to do that? Why
-
6:04 - 6:08would I ever want to go backwards? Why do I want to move back to the older yucky thing?
-
6:08 - 6:10This is the case that comes up;
-
6:10 - 6:12the open operation
-
6:12 - 6:15on isstream and osstream
-
6:15 - 6:19expects its argument to be specified as an old style string.
-
6:19 - 6:22This is actually just an artifact; it has to do with it -
-
6:22 - 6:24the group that was working on
-
6:24 - 6:27designing the string package. The group that was designing the string package were
-
6:27 - 6:31not in sync, and they were not working together. The string package was
-
6:31 - 6:33finalized before the string package was ready
-
6:33 - 6:37and so it depended on what was available at the time and that was only the old style
-
6:37 - 6:37string.
-
6:37 - 6:41So as a result, it wants an old style string, and that's what it takes, and you
-
6:41 - 6:43can't give it a C++
-
6:43 - 6:47string. So in double quotes - so this is the case where the double quotes
-
6:47 - 6:49are actually old style strings,
-
6:49 - 6:52in almost all situations gets converted on your behalf automatically.
-
6:52 - 6:55In this case it's not being converted and it's exactly what's wanted.
-
6:55 - 6:59So if you have a name that's a string constant or a literal, you can just pass it
-
6:59 - 7:01in double quotes to open.
-
7:01 - 7:03If you have a C++ variable,
-
7:03 - 7:07so you've asked the user for what file to open, and you've used getline to
-
7:07 - 7:08read it into a string,
-
7:08 - 7:13if you try to pass that C++ string variable to open, it will not match
-
7:13 - 7:14what it's expecting.
-
7:14 - 7:18I do need to do that conversion asking it to go .c_str
-
7:18 - 7:21to convert itself into the old style format.
-
7:21 - 7:24So that was sort of where I was getting to when I kind of
-
7:24 - 7:26positioned you to realize this was gonna
-
7:26 - 7:29someday come up. This is the one piece of the interface that will interact with this
-
7:29 - 7:31quarter that requires that old string,
-
7:31 - 7:33where you'll have to make that effort to
-
7:33 - 7:36convert it backwards.
-
7:36 - 7:39Both of these operations can fail.
-
7:39 - 7:43When you open a file and [inaudible] - question here? So how hard
-
7:43 - 7:44[inaudible]?
-
7:44 - 7:48You know it's obviously extremely easy to do it;
-
7:48 - 7:50the issue has to do with compatibility.
-
7:50 - 7:54They announced it this way, people wrote code that expected it this way
-
7:54 - 7:57and then you change it out from under them and all this code breaks that used
-
7:57 - 7:58to work.
-
7:58 - 8:01And so as a result of this [inaudible]
-
8:01 - 8:02compatibility an issue of
-
8:02 - 8:04once we kind of published it and we told people this was how it works, we can't
-
8:04 - 8:07really take it away from them. And so part of that's - sort of part of what we're doing within C++2,
-
8:07 - 8:10which is things that used to work in C still need to work in C,
-
8:10 - 8:11and so as a result
-
8:11 - 8:14there's a certain amount of history that we're all carrying forward with us in a very
-
8:14 - 8:16annoying way. I totally agree
-
8:16 - 8:19that it seems like we could just fix it, but we would break a lot of code in the process
-
8:19 - 8:21and anger a lot of
-
8:21 - 8:24existing programmers.
-
8:24 - 8:28So both of these open calls could fail; you might be able to - try to open a file and it
-
8:28 - 8:31doesn't exist, you don't have the permissions for it, you spelled the name wrong.
-
8:31 - 8:34Similarly trying to open it for writing, it's like you might not have write
-
8:34 - 8:36permission in the directory.
-
8:36 - 8:38And
-
8:38 - 8:41in either situation you need to know, well did it open or did it not?
-
8:41 - 8:44There's not a return value from open that tells you that.
-
8:44 - 8:47What there is is a member function called
-
8:47 - 8:52.fail, that you can ask the stream at any point, are you in a fail state. So for
-
8:52 - 8:54operations that actually kinda have a chance of succeeding or failing in the
-
8:54 - 8:57string, you'll tend to actually almost write the code as a
-
8:57 - 8:57try it
-
8:57 - 9:01then check in .sale. So try to read this thing, check in .sale. Try to
-
9:01 - 9:03open this file check in .sale as your way
-
9:03 - 9:07of following up on did it work and making sure that you
-
9:07 - 9:09have good contents before you keep going.
-
9:09 - 9:12If the in .open has failed,
-
9:12 - 9:14then every subsequent read on it will fail.
-
9:14 - 9:17Once the string is in a fail state, nothing works. You can't read
-
9:17 - 9:20or write or do anything with it until you fix the error,
-
9:20 - 9:23and that's the in .clear
-
9:23 - 9:26command that kind of resets the state back into a known good state,
-
9:26 - 9:29and then you have a chance to retry. So for example, if you were trying to open a
-
9:29 - 9:32file that the user gave you a name for,
-
9:32 - 9:35they might type the name wrong. So you could try in .openit, check
-
9:35 - 9:36in .dot fail.
-
9:36 - 9:40If it failed, say no, no, I couldn't open that file, why don't you try again, get a new
-
9:40 - 9:41name,
-
9:41 - 9:45and then you'd clear the state, come back around and try another in .open
-
9:45 - 9:51to - until you get one that succeeds.
-
9:51 - 9:53Once you have one of those guys open
-
9:53 - 9:55for reading or writing,
-
9:55 - 9:56there are three
-
9:56 - 10:01main ways that you can do your input/output.
-
10:01 - 10:06We have seen this form a little bit, this one with the insertion/extraction,
-
10:06 - 10:09these other two are more likely to be useful in the file reading state as
-
10:09 - 10:12opposed to interacting with the user state, and they have to deal with just
-
10:12 - 10:15breaking down the input
-
10:15 - 10:17more
-
10:17 - 10:19fine graindly.
-
10:19 - 10:21Let's say this first one is reading and writing single characters. It might be
-
10:21 - 10:24that all I want to do is just go through the file and read it character by character.
-
10:24 - 10:27Maybe what I'm trying to write is something that will just count the characters and
-
10:27 - 10:30produce a frequency count across
-
10:30 - 10:33the file, tell me how many A's and B's and C's are in it,
-
10:33 - 10:35or just tell me how many characters are in the file at all.
-
10:35 - 10:37In .get
-
10:37 - 10:40is the number function that you send to an input file stream
-
10:40 - 10:42that will retrieve the next character.
-
10:42 - 10:46If [inaudible] the next character from the stream it returns EOF when there are no
-
10:46 - 10:49more characters. EOF is the end of file marker, it's actually capital
-
10:49 - 10:53EOF, it's the constant that's defined with the class. And
-
10:53 - 10:56so you could read till EOF as a way of just getting them character by
-
10:56 - 10:58character.
-
10:58 - 11:02Similarly there is a put on the other side, which is when you're writing, do you just
-
11:02 - 11:04want to write a single character.
-
11:04 - 11:06You could also do this with
-
11:06 - 11:09out << ch, which writes the character. This actually just does a
-
11:09 - 11:11put of the character, just
-
11:11 - 11:15kind of a matching function in the analog to get input
-
11:15 - 11:19that do single character io.
-
11:19 - 11:22Sometimes what you're trying to do is process it line by line. Each line is the
-
11:22 - 11:25name of somebody and you're kind of putting those names into a database. You
-
11:25 - 11:28don't want to just assemble the characters by characters, and you don't know how
-
11:28 - 11:30many
-
11:30 - 11:31tokens there might be,
-
11:31 - 11:35that the white space might be that there's Julie Diane Zelenski, sometimes
-
11:35 - 11:38there might be Julie Zelenski, you don't know how many name pieces might appear to be
-
11:38 - 11:39there.
-
11:39 - 11:42You can use getline to read an entire line in one chuck.
-
11:42 - 11:45So it'll read everything up to
-
11:45 - 11:49the first new line character it finds. It actually discards the new line and advances
-
11:49 - 11:52past it. So what you will get is -
-
11:52 - 11:56the sequence of characters that you will have read will be everything up to and not including
-
11:56 - 11:59the new line. The new line will be consumed though so that reading will
-
11:59 - 12:00pick up
-
12:00 - 12:02on the next line and go forward.
-
12:02 - 12:06Getline is a free function.
-
12:06 - 12:12It is not a member function on the stream. It takes a stream as its first
-
12:12 - 12:12argument.
-
12:12 - 12:16It takes a string by reference as its second argument,
-
12:16 - 12:20and it fills in the line with the text of
-
12:20 - 12:24the characters from here to the next line read in the file.
-
12:24 - 12:26
-
12:26 - 12:29If it fails the way you will find out is by checking the fail
-
12:29 - 12:31states. You can do a getline
-
12:31 - 12:34inline and then in .fail after it to see, well did it write something
-
12:34 - 12:39in the line that was valid? If it failed, then the contents of line are
-
12:39 - 12:40
-
12:40 - 12:44unchanged, so they'll be whatever nonsense they were. So
-
12:44 - 12:46it's a way of just pulling it line by line.
-
12:46 - 12:51This name has the same words in it as
-
12:51 - 12:52rgetlineGL
-
12:52 - 12:55in the sympio, which shows that it's kind of a reasonable name for the kind
-
12:55 - 13:00of thing that reads line by line, but there is a different arrangement to how it's - what
-
13:00 - 13:03it's used for and how it's it used. So rgetline takes no arguments and returns a line read
-
13:03 - 13:04for the console.
-
13:04 - 13:08The lower case getline takes the file stream to read from and the string to write
-
13:08 - 13:10it into
-
13:10 - 13:11and
-
13:11 - 13:13does not have a return value.
-
13:13 - 13:16You check in .fail if you
-
13:16 - 13:18want to know how it went. So write the entire line out there, [inaudible] a put line equivalence, so
-
13:18 - 13:22in fact you could just use the out
-
13:22 - 13:26stream insertion here, stick that line back out with an nline to kind of reproduce
-
13:26 - 13:28the same line your just read.
-
13:28 - 13:32And then these we've talked a little about, this idea of formatted
-
13:32 - 13:35read and write, where it's expecting things by format. It's expecting to see a character,
-
13:35 - 13:38it's expecting to see an integer, and it's expecting to see a
-
13:38 - 13:39string.
-
13:39 - 13:43It uses white space as the default delimiter between those things. So it's kind of
-
13:43 - 13:46scanning over white space and discarding it and then trying to pull the next thing out.
-
13:46 - 13:47
-
13:47 - 13:51These are definitely much trickier to use because if the format that you're
-
13:51 - 13:54expecting doesn't show up, it causes the stream to get new fail state, and you
-
13:54 - 13:56have to kind of fix it and recreate it.
-
13:56 - 14:00So often even when you expect that things are going to be, let's say, a sequence of
-
14:00 - 14:02numbers or a name fall by number,
-
14:02 - 14:05you might instead choose to pull it as a string
-
14:05 - 14:09and then use operations on the string itself to kinda divide it up
-
14:09 - 14:12rather than depending on stream io because stream io is just a little bit harder to get
-
14:12 - 14:14that same effect.
-
14:14 - 14:17And then in all these cases write in .fail.
-
14:17 - 14:19There is also -
-
14:19 - 14:22you could check out.fail. It's just much less common that the
-
14:22 - 14:25writing will fail, so you don't see it as much, but it is true for example, if
-
14:25 - 14:28you had wanted a disk space and you were writing, a
-
14:28 - 14:31write operation could fail because it had
-
14:31 - 14:35wanted a space or some media error had happened on the disk, so
-
14:35 - 14:36both of those
-
14:36 - 14:40have reasons to check fail.
-
14:40 - 14:43So let me do just a little bit of
-
14:43 - 14:45live coding
-
14:45 - 14:49to show you that I -
-
14:49 - 14:52it works the way I'm
-
14:52 - 14:54telling you. Yeah? So the
-
14:54 - 14:57fail
-
14:57 - 14:58function, is it
-
14:58 - 14:59going
-
14:59 - 15:02to always be the stream that's failing and not
-
15:02 - 15:04the function that's failing? Yes,
-
15:04 - 15:08pretty much. There are a couple rare cases where the function actually also
-
15:08 - 15:11tells you a little bit about it, but a general fail just covers the whole general
-
15:11 - 15:14case of anything I have just got on the stream fail
-
15:14 - 15:16so any of the operations
-
15:16 - 15:20that could potentially run into some error condition will set the fail in such a way
-
15:20 - 15:22that your next call to in .fail will tell you about it.
-
15:22 - 15:26And so that's the - the general model will be; make the call, check the fail,
-
15:26 - 15:28if you know that there was a chance that something could have gone
-
15:28 - 15:34wrong and then you want to clean up after it and do something [inaudible].
-
15:34 - 15:38So I'm gonna show you that I'm gonna get the name of the file from the
-
15:38 - 15:40user here,
-
15:40 - 15:43I'm going to use in .open of that,
-
15:43 - 15:46and I'm going to show you the error that you're gonna get when you forget to convert
-
15:46 - 15:47it, while I'm at it.
-
15:47 - 15:50And then I'll have like an in
-
15:50 - 15:52
-
15:52 - 15:53.fail error
-
15:53 - 15:55wouldn't -
-
15:55 - 15:57file didn't open.
-
15:57 - 16:02First I just want to show you this little simple stuff; I've got
-
16:02 - 16:06my ifstream declared, my attempt to open it and then my check for seeing that it
-
16:06 - 16:09failed. I'm gonna
-
16:09 - 16:13anticipate the fact that the compiler's gonna be
-
16:13 - 16:16complaining about the fact that it hasn't heard about fstream, so I'm gonna tell it about
-
16:16 - 16:16fstream.
-
16:16 - 16:20And I'm gonna let this go ahead in compiling, although I know it has an error in it,
-
16:20 - 16:22because I want to show you sort of the things that are happening. So the first thing
-
16:22 - 16:25it's complaining about actually is this one, which is
-
16:25 - 16:29the fact that getline is not declared in the scope, which meant I forgot one more of my
-
16:29 - 16:31headers that I wanted. Let
-
16:31 - 16:36me move this up a little bit because it's sitting down a little far.
-
16:36 - 16:39And then the second thing it's complaining about is right here.
-
16:39 - 16:43This is pretty hard to see, but I'll read it to you so you can tell what it says; it says error,
-
16:43 - 16:45there's no matching function call
-
16:45 - 16:48and then it has sort of some gobbly gook that's a little bit scary,
-
16:48 - 16:51but includes the name ifstream. It's actually - the full name for ifstream is a
-
16:51 - 16:53lot bigger than you think,
-
16:53 - 16:54but it's saying that there's -
-
16:54 - 16:58the ifstream is open, and it says that it does not
-
16:58 - 17:02have a match to that, that there is no open call on the ifstream class, so no
-
17:02 - 17:05member function of the ifstream class whose name is open,
-
17:05 - 17:07whose argument is a string.
-
17:07 - 17:11And so that cryptic little bit of information is gonna be your reminder
-
17:11 - 17:15to jog your memory about the fact that open doesn't deal in the
-
17:15 - 17:18new string world, it wants the old string world. It will
-
17:18 - 17:20not take a new string,
-
17:20 - 17:21and I will convert it
-
17:21 - 17:26to my old string,
-
17:26 - 17:30and then be able to get this thing compiling.
-
17:30 - 17:35And so when it runs if I enter a file name of I say [inaudible],
-
17:35 - 17:37it'll say error file didn't open, some file that
-
17:37 - 17:40I don't have access for. It happens that I have one sitting here, I think, whose name is
-
17:40 - 17:41
-
17:41 - 17:45handout.txt. I took the text of some handout and then I just
-
17:45 - 17:47left it there. So
-
17:47 - 17:48let me
-
17:48 - 17:49doing something with that file. Let's
-
17:49 - 17:53just do something simple where we just count the number of lines in it. Let's say - actually I'll make
-
17:53 - 17:55a little function that -
-
17:55 - 17:58just to talk a little bit about one of the things that's a little quirky about
-
17:58 - 18:00ifstreams
-
18:00 - 18:01is that
-
18:01 - 18:04when you pass an ifstream you will
-
18:04 - 18:07typically want to do so by reference.
-
18:07 - 18:08Not only is this kind of a good idea,
-
18:08 - 18:12because the ifstream is kind of changing in the process of being read. It's
-
18:12 - 18:15updating its internal state and you want to be sure that we're not
-
18:15 - 18:19missing this update that's going on. It's also the case that most
-
18:19 - 18:22libraries require you to pass it by reference. That it doesn't have a model for how
-
18:22 - 18:25to take a copy of a stream and make another copy that's distinct. That it really
-
18:25 - 18:28is always referring to the same file, so in fact in most libraries you have
-
18:28 - 18:31to pass it by reference.
-
18:31 - 18:34So I'll go ahead and pass it by reference. I'm gonna go in here and I'm just gonna do a line-by-line
-
18:34 - 18:39read and count as I go. I'm
-
18:39 - 18:43gonna write this as a wild [inaudible],
-
18:43 - 18:45and I'm gonna say
-
18:45 - 18:46read the next line
-
18:46 - 18:48from the file into the variable,
-
18:48 - 18:53and then if in .fail - so if it was unable to read another line,
-
18:53 - 18:57the - my assumption here is gonna be that
-
18:57 - 19:00we're done, so it will fail as eof . It's the most common reason it could
-
19:00 - 19:04fail. It could also fail if there was some sort of more catastrophic error, you're leading a file from a
-
19:04 - 19:06network and the network's gone down or something like that. In our
-
19:06 - 19:09case its right, the in .fail is going to tell us yeah, there's nothing more to read
-
19:09 - 19:11from this file, which means we've gotten to the end.
-
19:11 - 19:14We've advanced the count. Whenever we get a good line we go back
-
19:14 - 19:15around, so we're
-
19:15 - 19:18using kind of the wild true in this case because we have a little bit of work to do
-
19:18 - 19:20before we're ready to decide whether to keep going,
-
19:20 - 19:22in this case, reading that line.
-
19:22 - 19:28And then I return the count at the end,
-
19:28 - 19:30and then I can then down
-
19:30 - 19:33here print it nom lines
-
19:33 - 19:37= mi call to count lines of n
-
19:37 - 19:39and l. Okay. Let
-
19:39 - 19:40me move that up a little bit.
-
19:40 - 19:45Last time I posted the code that I wrote in the
-
19:45 - 19:49editor here, and I'll be happy to do that again today, so you
-
19:49 - 19:52shouldn't need to worry about copying it down, I will post it later if you want to
-
19:52 - 19:53have a copy of it for your records, but
-
19:53 - 19:56just showing, okay, yeah, we're just a line by line read,
-
19:56 - 19:59counting, and then a little bit more of the how do you open something, how do you
-
19:59 - 20:03check for failure. And
-
20:03 - 20:06when I put this together, what does it complain about? Well I think it complains about the fact that
-
20:06 - 20:16I told it my function returned void, but then I made it return it. And that
-
20:16 - 20:19should be okay now. And so if I read the handout.txt file,
-
20:19 - 20:23the number of lines in it happens to be 28. It's just some text I'd cut out of the handout, so
-
20:23 - 20:25there are 28 new line characters
-
20:25 - 20:31is basically what it's telling me
-
20:31 - 20:32there.
-
20:32 - 20:36So I can just do more things, like I could use - change this loop and instead use like get to
-
20:36 - 20:39do a single character count. I could say how many characters were in there.
-
20:39 - 20:41If I used
-
20:41 - 20:42the
-
20:42 - 20:45tokenization and I said, well just tell how many strings I find using string
-
20:45 - 20:48extraction, it would kind of count the number of non-space things that it found and
-
20:48 - 20:49things like that.
-
20:49 - 20:51Typically the
-
20:51 - 20:55IO is one of those errors I said where there's like a vast array of nuances to all
-
20:55 - 21:00the different things you can do with it, but the simple things actually are
-
21:00 - 21:03usually fairly easy, and those are the only ones that really going to matter to us as being
-
21:03 - 21:05able to do a little bit of simple reading and
-
21:05 - 21:09file reading/writing to get information into our programs. How do
-
21:09 - 21:11you feel about that? Question? Sorry, why do
-
21:11 - 21:12have getline
-
21:12 - 21:14an empty string?
-
21:14 - 21:16So getline,
-
21:16 - 21:19the one that was down here? This one? No,
-
21:19 - 21:21the one that - Oh, the
-
21:21 - 21:26one that's up here. So yeah, let's talk about that. The getline that's here is -
-
21:26 - 21:29the second argument to getline is being passed by reference, and so it's
-
21:29 - 21:32filling in that line with the information it read from the file.
-
21:32 - 21:36So I just declared the variable so I had a place to store it
-
21:36 - 21:37and I said,
-
21:37 - 21:40okay, read the next line from the file, store the thing you read in the line. It turns
-
21:40 - 21:42out I don't actually care about that information, but there's no way to tell
-
21:42 - 21:44getline to just throw it away anyway. Oh.
-
21:44 - 21:47So I'm using it to just kinda move through line-by-line, but it happens to
-
21:47 - 21:51be that getline requires me to store the answer somewhere, and I'm storing it.
-
21:51 - 21:54Instead of returning it, it happens to use the design where it fills it in by
-
21:54 - 21:56reference.
-
21:56 - 21:59There's actually - it turns out to be a little bit more efficient
-
21:59 - 22:02to do a pass by reference and fill something in, then to return it. And the
-
22:02 - 22:05C++ libraries in general prefer that style of
-
22:05 - 22:06
-
22:06 - 22:09getting information back out of a function as opposed to the function return,
-
22:09 - 22:13which you think of as being a little more natural design. There's a slight
-
22:13 - 22:16inefficiency to that relative to the pass by reference and the libraries tend to be very
-
22:16 - 22:19hyper-conscious of that efficiency, so they tend to prefer this
-
22:19 - 22:26slightly more awkward style. Question?
-
22:26 - 22:28Why in the
-
22:28 - 22:31main [inaudible] does the
-
22:31 - 22:32error
-
22:32 - 22:36open [inaudible] file didn't open with [inaudible] like print error: file didn't open? You
-
22:36 - 22:37know
-
22:37 - 22:40it's just the way that error works. Error wants to make sure that you don't mistake what
-
22:40 - 22:44it does, and so it actually prefixes whatever you ask it to write with this big
-
22:44 - 22:47ERROR in uppercase letters, and so
-
22:47 - 22:50the purpose of error is twofold; is to report what happened and to halt
-
22:50 - 22:54processing. And so when it reports that it actually prefixes it with this big red
-
22:54 - 22:58E-R-R-O-R just to say don't miss this, and then it halts processing
-
22:58 - 23:01there. And it's just - the error [inaudible] libraries function, which is your way of handling any
-
23:01 - 23:04kind of catastrophic I can't recover from this. And it's certainly
-
23:04 - 23:07something we don't want anybody to overlook, and so we try to make it
-
23:07 - 23:11really jump out at you when
-
23:11 - 23:15it tells you that. So this is in symbio? :It is in genlib actually. Oh. So error's actually declared out of genlib. And can we use it -
-
23:15 - 23:18so it's global basically? It is global. It's a telefree function, and you will definitely have occasion
-
23:18 - 23:22to use it. Right, it's just - it's your way of saying something happened that there's just no
-
23:22 - 23:26recovery from and continuing on would not make sense. Here's a
-
23:26 - 23:28- stop and help
-
23:28 - 23:32and alert the user something's really wrong, so you don't
-
23:32 - 23:34want to keep going after this because there's no way to kind of
-
23:34 - 23:36patch things back together. In
-
23:36 - 23:38this case probably a more likely thing we'd do, is I should say
-
23:38 - 23:42give me another name, let's go back around and try again, would be a
-
23:42 - 23:44sort of better way to handle that. I can
-
23:44 - 23:46even show you how I would do that.
-
23:46 - 23:49I could say, well while true,
-
23:49 - 23:51enter the name,
-
23:51 - 23:53and maybe I could change this to be well
-
23:53 - 23:55if it didn't fail
-
23:55 - 23:59then go ahead and break out of the loop. Otherwise, just report that the file
-
23:59 - 24:02didn't open,
-
24:02 - 24:04and say try again.
-
24:04 - 24:06And then the last thing I will need to do
-
24:06 - 24:09is clear that state.
-
24:09 - 24:11So now it's prompting,
-
24:11 - 24:12trying to open it.
-
24:12 - 24:15If it didn't fail it will break and then it will move forward to counting the lines.
-
24:15 - 24:16
-
24:16 - 24:19If it did fail it'll continue on through here reporting this message, and then
-
24:19 - 24:22that clear, very important, because that clear kind of gets us
-
24:22 - 24:26back in the state where we can try again. If we don't clear the error and we try to do
-
24:26 - 24:29another in .open, once the string is in a fail state it stays in a fail
-
24:29 - 24:33state until you clear it, and no subsequent operation will work whatsoever.
-
24:33 - 24:35It's just ignoring everything you ask it to do
-
24:35 - 24:38until you have acknowledged you have done something about the problem, which
-
24:38 - 24:42in this case was as simple as clearing and asking to open again.
-
24:42 - 24:46So if I do it this way
-
24:46 - 24:50I enter some name it'll say that didn't open, try again. And then if I say
-
24:50 - 24:51handout.txt,
-
24:51 - 24:52it'll open it and
-
24:52 - 24:57go ahead and read. All right,
-
24:57 - 25:01any questions about iostreams? We're
-
25:01 - 25:07gonna move away from this [inaudible], if there's anything about it you'd like to know I'd be happy to answer it.
-
25:07 - 25:08So let me
-
25:08 - 25:11get us back to
-
25:11 - 25:15our slides,
-
25:15 - 25:17and I'll kind
-
25:17 - 25:21of move on to the more object-oriented features of the things we're going to be
-
25:21 - 25:25depending on and using this quarter.
-
25:25 - 25:27So the libraries that we have been looking at,
-
25:27 - 25:30many of them are just provided as what we call free functions. Global functions that
-
25:30 - 25:34aren't assigned to a particular object, they are part of a class, so asking for random
-
25:34 - 25:34integer,
-
25:34 - 25:37reading a line, competing the square root,
-
25:37 - 25:40gobs of things are there that just kind of have
-
25:40 - 25:43functionality that you can use anywhere and everywhere procedurally.
-
25:43 - 25:46We've just started to see some things that are provided in terms of classes,
-
25:46 - 25:50the string of the class, that means that you have string objects that you're messaging and
-
25:50 - 25:52having them manipulate themselves.
-
25:52 - 25:55The stream object also is class, ifstream, ofstream, those are all classes
-
25:55 - 25:57that you send messages like open to
-
25:57 - 26:04and fail to, to ask about that streams state or reset its state. This idea of
-
26:04 - 26:08a class is one that's hopefully not new to you. Most of you are coming from Java
-
26:08 - 26:11have - this is pretty much the only mechanism for writing code for
-
26:11 - 26:14Java is in the context of a class. Those
-
26:14 - 26:16of you who haven't seen that as much, we're going to definitely be practicing
-
26:16 - 26:20on this in our - some simple things you need to know to kind of just get up to the
-
26:20 - 26:24vocabulary wise is class is just a way of taking a set of
-
26:24 - 26:25fields or data
-
26:25 - 26:29and attaching operations to it to where it kind of creates a kind of an
-
26:29 - 26:33entity that has both its state and its functionality kind of packaged
-
26:33 - 26:34together.
-
26:34 - 26:38So in the class interface you'll say here is a time object, and a time object has an
-
26:38 - 26:39hour and a minute
-
26:39 - 26:40and you can do things like
-
26:40 - 26:42tell me if this time's before that time or what the
-
26:42 - 26:46duration starting at this time and this end time would - there would be all these
-
26:46 - 26:50behaviors that are like [inaudible] to do. Can you print a time, sure. Can I read a time for a
-
26:50 - 26:51file, sure.
-
26:51 - 26:54As long as the interface for the time class provides those things, its kinda this fully
-
26:54 - 26:56flip - fleshed out
-
26:56 - 26:58new data type
-
26:58 - 27:02that then you use time objects of whenever you need to work with time.
-
27:02 - 27:06The idea is that the client use the object, which is the first role we're
-
27:06 - 27:08gonna be in for a couple weeks here,
-
27:08 - 27:11is you learn what the abstraction is. What does the class provide? It provides the notion of a
-
27:11 - 27:14sequence of characters, that's what stream does. And so that sequence
-
27:14 - 27:17has all these operations; like well tell me what characters are at this position, or
-
27:17 - 27:19find this sub-string,
-
27:19 - 27:21or insert these characters, remove those characters. And
-
27:21 - 27:24internally it's obviously doing some machinations to keep track of what you
-
27:24 - 27:28asked it to do and how to update its internal state. But what's neat is that from
-
27:28 - 27:30the outside as a client you just think well there's a sequence of
-
27:30 - 27:34characters there and I can ask that sequence of characters to do these
-
27:34 - 27:35operations, and
-
27:35 - 27:37it does what I ask,
-
27:37 - 27:38and that I don't need to know
-
27:38 - 27:42how it's implemented internally. What mechanisms it uses and how it responds
-
27:42 - 27:44to those things to update it state
-
27:44 - 27:46is very much
-
27:46 - 27:50kind of behind the abstraction or inside that black box, sometime we'll call it to kind
-
27:50 - 27:51of
-
27:51 - 27:52
-
27:52 - 27:54suggest to ourselves that we can't see inside of it, we don't know how it works. It's
-
27:54 - 27:57like the microwave, you go up and you punch on the microwave and you say cook for a minute. Like
-
27:57 - 28:01what does the microwave do? I don't know, I have no idea, but things get hot, that's what I
-
28:01 - 28:02know.
-
28:02 - 28:04So the nice thing about [inaudible] is you can say, yeah,
-
28:04 - 28:08if you push this button things get hot and that's what I need to know.
-
28:08 - 28:12[Inaudible] has become widely
-
28:12 - 28:13industry standard in sort
-
28:13 - 28:16of all existing languages that are out there. It seems like there's been
-
28:16 - 28:19somebody who's gone to the trouble of trying to extend it to add these
-
28:19 - 28:22object [inaudible] features and languages like Java that are fully object
-
28:22 - 28:25oriented, are very much all the rage now.
-
28:25 - 28:29And I thought it was interesting to take just a minute to talk about well why is it so
-
28:29 - 28:32successful? Why is object oriented like the next big thing in programming?
-
28:32 - 28:36And there are some really good valid reasons for why it is a very
-
28:36 - 28:39sensible approach to writing programs
-
28:39 - 28:41that is
-
28:41 - 28:43worth thinking a little bit about.
-
28:43 - 28:46Probably the largest sort of
-
28:46 - 28:49motivation for the industry has to do with this idea of taming complexity
-
28:49 - 28:51that certainly one of the
-
28:51 - 28:53weaknesses of
-
28:53 - 28:54ourself as a discipline is that
-
28:54 - 28:57the complexity kinda can quickly spiral out of control.
-
28:57 - 28:58The programs that -
-
28:58 - 29:01as they get larger and larger, their interactions get harder and harder to
-
29:01 - 29:02model and we have more
-
29:02 - 29:06and more issues where we have bugs and security flaws and viruses
-
29:06 - 29:09and whatnot that exploit holes in these things.
-
29:09 - 29:12That we need a way as engineers to kind of
-
29:12 - 29:15tighten down our discipline and really produce things that actually
-
29:15 - 29:17don't have those kind of holes in them.
-
29:17 - 29:20And that object oriented probably means one of the ways to try to manage the complexities of
-
29:20 - 29:22systems.
-
29:22 - 29:25That instead of having lots and lots of code that [inaudible] things, if you can
-
29:25 - 29:28break it down into these objects, and each
-
29:28 - 29:30class that represents that object can be
-
29:30 - 29:33designed and tested and worked on independently,
-
29:33 - 29:35there's some hope that you can have a team of programmers working together,
-
29:35 - 29:38each managing their own classes
-
29:38 - 29:41and have them be able to not interfere with each other too much to kind of
-
29:41 - 29:42accomplish -
-
29:42 - 29:45get the whole end result done by having people collaborate, but without them kind
-
29:45 - 29:48of stepping on top of each other.
-
29:48 - 29:51It has a - the advantage of modeling the real world, that we tend to talk to talk about
-
29:51 - 29:54classes that kind of have names that speak to us, what's a ballot, what's
-
29:54 - 29:59a class list, what's a database, what is a
-
29:59 - 30:01time, a string,
-
30:01 - 30:04that - a fraction? These things kind of - we have ideas about what those things are
-
30:04 - 30:06in the real world, and having the class
-
30:06 - 30:09model that abstraction makes it easier to understand what the code is doing and
-
30:09 - 30:11what that objects role is
-
30:11 - 30:13in solving the problem.
-
30:13 - 30:17It also has the advantage of [inaudible] use. That once you build a class and it's
-
30:17 - 30:19operations, the idea is that it can
-
30:19 - 30:24be pulled out of the - neatly out of the one program and used in another if the
-
30:24 - 30:25design has been done,
-
30:25 - 30:29and can be changed extended fairly easily in the future if the design was
-
30:29 - 30:30
-
30:30 - 30:32good to begin with.
-
30:32 - 30:34So let me tell you what
-
30:34 - 30:38kind of things we're going to be doing in our class library
-
30:38 - 30:40that will help you to kind of just become a big fan
-
30:40 - 30:43of having a bunch of pre-written classes around.
-
30:43 - 30:44We have,
-
30:44 - 30:49I think, seven classes - I think there's eight actually in our class library
-
30:49 - 30:51that just look at certain problems that either
-
30:51 - 30:53C++
-
30:53 - 30:56provides in a way that's not as convenient for us, or is kind of missing,
-
30:56 - 30:58or that can be improved on where we've
-
30:58 - 31:01tackled those things and given you seven classes that you just get to use from
-
31:01 - 31:02the get go
-
31:02 - 31:06that solve problems that are likely to come up for you.
-
31:06 - 31:07One of them is the scanner,
-
31:07 - 31:10which I kind of separated by itself because it's a little bit of an unusual class, and
-
31:10 - 31:14then there's a bunch of container classes on that next line, the vector
-
31:14 - 31:16grid, staque, math and set
-
31:16 - 31:19that are used for storing data, different kinds of collections,
-
31:19 - 31:21and they differ in kind of
-
31:21 - 31:24what their usage pattern is and what they're storing,
-
31:24 - 31:26how they're storing it for you.
-
31:26 - 31:29But that most programs need to do stuff like this, need to store some kind of
-
31:29 - 31:30collection of date,
-
31:30 - 31:32why not have some good tools to do it.
-
31:32 - 31:33
-
31:33 - 31:36These tools kinda let you live higher on the food chain. They're very efficient,
-
31:36 - 31:39they're debugged, they're commented, the abstraction's been thought about and
-
31:39 - 31:40kind of worked out
-
31:40 - 31:43and so they provide kinda this very useful piece of function [inaudible] kinda written to you
-
31:43 - 31:46ready to go.
-
31:46 - 31:49And then I - a little note here is that we study these - we are going to study these
-
31:49 - 31:51abstractions twice.
-
31:51 - 31:53We're gonna look at these seven classes
-
31:53 - 31:57today and Friday as a client, and then start using them all through the quarter.
-
31:57 - 32:01In about a week or so after the mid-term we're gonna come back to them
-
32:01 - 32:02and say, well how are they implemented?
-
32:02 - 32:06That after having used them and appreciated what they provided to you, it
-
32:06 - 32:08will be interesting, I think, to open up the hood
-
32:08 - 32:11and look down in there and see how they work.
-
32:11 - 32:15I think this is - there is an interesting pedagogical
-
32:15 - 32:17
-
32:17 - 32:20debate going on about this, about
-
32:20 - 32:21whether
-
32:21 - 32:22
-
32:22 - 32:24it's better to first know how to implement these things and then get to
-
32:24 - 32:27use them, or to use them and then later know how to implement them.
-
32:27 - 32:30And I liken it to a little bit if you think about some things we do
-
32:30 - 32:33very clearly one way or the other in our curriculum, and it's interesting to think about
-
32:33 - 32:34why.
-
32:34 - 32:37That when you learn, for example, arithmetic as a
-
32:37 - 32:38primary schooler,
-
32:38 - 32:41they don't give you a calculator and say, here, go do some division and multiplication,
-
32:41 - 32:43and then later try to teach you long division.
-
32:43 - 32:46You'll never do it. You'll be like, why would I ever do this, this little box
-
32:46 - 32:48does it for me, the black box.
-
32:48 - 32:52So in fact they drill you on your multiplication tables and
-
32:52 - 32:56your long division long before they let you touch a calculator,
-
32:56 - 33:00which I think is one way of doing it. And, so - and for example, it's like
-
33:00 - 33:03we could do that with you, make you do it the kind of painful way and then
-
33:03 - 33:06later say, okay, well here's these way you can avoid
-
33:06 - 33:07
-
33:07 - 33:09being bogged down by that tedium.
-
33:09 - 33:12On the other had, think about the way we teach you to drive.
-
33:12 - 33:14We do not say, here's a wheel and
-
33:14 - 33:15then they say,
-
33:15 - 33:18let me tell you a little bit about the combustion engine, you
-
33:18 - 33:21know, we give you some spark plugs and
-
33:21 - 33:25try to get you to build your car from the ground up. It's like you learn to drive
-
33:25 - 33:25
-
33:25 - 33:26and then if you
-
33:26 - 33:29are more interested in that you might learn what's under the hood, how to
-
33:29 - 33:31take care of your car, and eventually how to do
-
33:31 - 33:35more serious repairs or design of your own care.
-
33:35 - 33:38Where I think of that as being a client first model, like you learn how to use the car
-
33:38 - 33:41and drive and get places and then if it
-
33:41 - 33:45intrigues you, you can dig further to learn more about how the car works.
-
33:45 - 33:48So that's definitely - our model is more of the drive one than the arithmetic one that
-
33:48 - 33:52it's really nice to be able to drive places first. Like if I - we spent all quarter
-
33:52 - 33:54learning how to build a combustion engine and you didn't get to go
-
33:54 - 33:55anywhere,
-
33:55 - 33:56
-
33:56 - 33:59I'd feel like you wouldn't have tasted what - where you're trying to get, and why that's
-
33:59 - 34:01so fabulous. So
-
34:01 - 34:04we will see them first as a client, and you'll get to do really neat things. You'll discover this thing called
-
34:04 - 34:08the map where you can put thousands, millions of entries in and
-
34:08 - 34:11have instantaneous look-up access on that.
-
34:11 - 34:14That you can put these things in a stack or a queue and then have them maintained
-
34:14 - 34:15for you and popped back out
-
34:15 - 34:19and all the storage of that being managed and the safety of that being managed without
-
34:19 - 34:23you having to kinda take any active role in that. That they provide functionality to
-
34:23 - 34:24you, that you just get
-
34:24 - 34:25to -
-
34:25 - 34:29leverage from the get go, and hopefully it will cause you to be
-
34:29 - 34:32curious though, like how does it work, why does it work so well,
-
34:32 - 34:33and what kind
-
34:33 - 34:36of things must happen behind the scenes and under the hood
-
34:36 - 34:39so that when we get to that you're actually kind of inspired to know
-
34:39 - 34:43how it did it, what it did.
-
34:43 - 34:44So I'm gonna tell you about the scanner
-
34:44 - 34:48and maybe even tell you a little bit about the vector today, and then we'll do the remaining
-
34:48 - 34:52ones on Friday, perhaps even carrying over a little bit into the weeks
-
34:52 - 34:55to get ourselves used to what we've got.
-
34:55 - 34:58The scanner I kind of separated because the scanner's more of a task based object then it
-
34:58 - 34:59is a
-
34:59 - 35:02collection or a container for storing things. The scanner's job is to break
-
35:02 - 35:07apart input into tokens. To take a string in this case that either you read from
-
35:07 - 35:10the file or you got from the user, or you constructed some way, and just tokenize
-
35:10 - 35:12it. It's called tokenizer parsec.
-
35:12 - 35:13
-
35:13 - 35:18That this is something a little bit like - strained extraction kind of does this,
-
35:18 - 35:22but strained extraction, as I said, isn't very flexible,
-
35:22 - 35:23that it doesn't
-
35:23 - 35:25make it easy for you to kind of - you
-
35:25 - 35:28have to sort of fully anticipate what's coming up on the string. There's not
-
35:28 - 35:29anyway you can sort of
-
35:29 - 35:31take a look at it and then to decide what to do with it and
-
35:31 - 35:35decide how to change your parstring strategy. And scanner has a kind of flexibility that
-
35:35 - 35:36lets it be a little bit more
-
35:36 - 35:40configurable about what you expect coming up and how it works.
-
35:40 - 35:43So the idea is that basically it just takes your input, you know, this line contains ten
-
35:43 - 35:44tokens,
-
35:44 - 35:46and as you go into a loop saying,
-
35:46 - 35:48give me the next token, it will
-
35:48 - 35:52sub-string out and return to you this four character string followed by this single
-
35:52 - 35:54character space and then this four character line
-
35:54 - 35:58and space, and so the default behavior is to extract all the tokens to come up,
-
35:58 - 36:03to use white-space and punctuation as delimiters. So it will kind of
-
36:03 - 36:05aggregate letters and numbers together
-
36:05 - 36:09and then individual spaces and new lines and tabs will come out as single
-
36:09 - 36:14character tokens. The parenthesis and dots and number signs would all come out as single character
-
36:14 - 36:15tokens,
-
36:15 - 36:17
-
36:17 - 36:20and it just kind of divides it up for you.
-
36:20 - 36:21Okay.
-
36:21 - 36:25It has fancy options though that let you do things like discard those face
-
36:25 - 36:29tokens because you don't care about them. To do things like read
-
36:29 - 36:31the fancy number formats. So it can read
-
36:31 - 36:35integer formats and real formats, it can do the real format with exponentiation
-
36:35 - 36:38in it with leading minus', things like that,
-
36:38 - 36:40that
-
36:40 - 36:41you can control
-
36:41 - 36:44with these setters and getters, like what it is you wanted to do about those things.
-
36:44 - 36:48You can it things like when I see an opening quote, I want you to gather everything to
-
36:48 - 36:50the closing quote, and so it does kind of
-
36:50 - 36:53gather
-
36:53 - 36:56phrases out of sequence if that's what you want. And so you have control over
-
36:56 - 37:00when and where it decides to do those things that lets you kind of
-
37:00 - 37:05handle a variety of kind of parsing and dividing tasks by using the scanner
-
37:05 - 37:08to get that job done. So I listed some things you might need, if you're
-
37:08 - 37:11reading txt files, you're parsing expressions, you were processing some kind of commands, that
-
37:11 - 37:15this scanner is a very handy way to just divide that [inaudible] up.
-
37:15 - 37:17You could certainly do this kind of stuff manually,
-
37:17 - 37:18for example,
-
37:18 - 37:23like using the find on the string and finding those faces and dividing it up, but
-
37:23 - 37:25that the idea is just doing that
-
37:25 - 37:27in a more convenient way for you
-
37:27 - 37:33than you having to handle that process manually.
-
37:33 - 37:35This is what its interface looks like.
-
37:35 - 37:39So this is a C++ class definition. It looks
-
37:39 - 37:42very similar to a Java class definition, but there's a little bit of
-
37:42 - 37:46variation in some of the ways the syntax comes through in the class.
-
37:46 - 37:48The class being here is scanner,
-
37:48 - 37:52the public colon introduces a sequence of where everything from
-
37:52 - 37:55here until the next access modifier is
-
37:55 - 37:58public. So I don't actually have public repeated again and again on all the
-
37:58 - 38:00
-
38:00 - 38:01individual entries here.
-
38:01 - 38:04It tells us that the scanner has a constructor
-
38:04 - 38:08that takes no arguments; it just initializes a new empty scanner.
-
38:08 - 38:12I'm gonna skip the destructor for a second; I'll come back to it.
-
38:12 - 38:15There is a set input member function that you give it the string that you want
-
38:15 - 38:15
-
38:15 - 38:19scanned and then there's these two
-
38:19 - 38:21operations that tend to be used in a look where you keep asking are there more
-
38:21 - 38:23tokens and if so, give me the next token, so it
-
38:23 - 38:27just kind of pulls them out one by one. I picked
-
38:27 - 38:31just one of the space - of the particular advanced options to show you
-
38:31 - 38:34the format for them. There's actually about six more that deal with
-
38:34 - 38:35
-
38:35 - 38:37some other more obscure things.
-
38:37 - 38:38This one is
-
38:38 - 38:40how is it you'd like it to deal with spaces,
-
38:40 - 38:44when you see face tokens, should they be returned as ordinary tokens or should you
-
38:44 - 38:48just discard them entirely and not even bother with them?
-
38:48 - 38:51The default is what's called preserve spaces, so it really does return them, so if
-
38:51 - 38:54you ask and there's only spaces left in the file, it will say there are more tokens
-
38:54 - 38:58and as you call the next token we'll return those spaces as individual tokens.
-
38:58 - 39:01If you instead have set the space option of ignore spaces, then it will just
-
39:01 - 39:05skip over all of those, and if all that was left in the file was white space
-
39:05 - 39:07when you ask for more tokens, it will say no.
-
39:07 - 39:10And when you ask for a token and there's some spaces leading up to
-
39:10 - 39:15something it will just skip right over those and return the next non-space token.
-
39:15 - 39:19There's a variety of these other ones that exist
-
39:19 - 39:23that handle the floating point and the double quote and other kind of
-
39:23 - 39:25fancy behaviors.
-
39:25 - 39:29There's one little detail I'll show you that's a C++ ism that isn't
-
39:29 - 39:31- doesn't really have a Java analog,
-
39:31 - 39:34which is the constructor which is used as the initialization function for a
-
39:34 - 39:35class
-
39:35 - 39:36has a
-
39:36 - 39:38corresponding destructor.
-
39:38 - 39:41Every class has the option of doing this.
-
39:41 - 39:41That is
-
39:41 - 39:45the - kind of when the object is being created, the constructor is being called. When the
-
39:45 - 39:50object is being de-allocated or destroyed, going out of scope, the destructor is
-
39:50 - 39:51called.
-
39:51 - 39:54And the pairing allows sort of the constructor to do any kind of set up that needs to be
-
39:54 - 39:58done and the destructor to do any kind of tear down that needs to be done.
-
39:58 - 40:01In most cases there's not that much that needs to be there, but
-
40:01 - 40:06it is part of the mechanism that allows all classes to have an option kind of at
-
40:06 - 40:09birth and death to do what it needs to do. For example, my file
-
40:09 - 40:10stream
-
40:10 - 40:13object, when you -
-
40:13 - 40:16when it goes away, closes it file automatically. So it's a place where the
-
40:16 - 40:24destructor gets used to do cleanup as that object is no longer valid.
-
40:24 - 40:25So a little bit of
-
40:25 - 40:26scanner
-
40:26 - 40:28code
-
40:28 - 40:32showing kind of the most common access pattern, is you declare the
-
40:32 - 40:37scanner. So at this point the scanner is empty, it has no contents to scan.
-
40:37 - 40:39Before I start pulling stuff out of it,
-
40:39 - 40:42I'm typically gonna call a set input on it, passing some string. In this case the
-
40:42 - 40:46string I'm passing is the one that was entered by the user, using getline.
-
40:46 - 40:48And then the
-
40:48 - 40:51ubiquitous loop that says well while the scanner has more tokens, get the next
-
40:51 - 40:52token.
-
40:52 - 40:55And in this case I'm not even actually paying attention to what those tokens are, I'm
-
40:55 - 40:56just counting them.
-
40:56 - 40:59So this one is kind of a
-
40:59 - 41:03very simple access that just says just call the next token as many times as you can
-
41:03 - 41:04until there
-
41:04 - 41:10are no more tokens to pull out. Way in the back? [Inaudible] I
-
41:10 - 41:11mean, like
-
41:11 - 41:16in the beginning when it says scanner, scanner, do we write scanner scanner = new
-
41:16 - 41:17scanner () or [inaudible]?
-
41:17 - 41:19Yes.
-
41:19 - 41:22Not exactly. So that's a very good example of like where Java and C++ are gonna
-
41:22 - 41:25conspire to trip you up just a little bit,
-
41:25 - 41:30that in Java objects were always printed using the syntax of new. You say new
-
41:30 - 41:33this thing, and in fact that actually does an allocation
-
41:33 - 41:35out in what's called the heap
-
41:35 - 41:37of that object and then from there you use it.
-
41:37 - 41:40In C++ you actually don't have to put things in the heap, and in fact
-
41:40 - 41:43we will rarely put things in the heap, and that's what new is for.
-
41:43 - 41:47So we're gonna use the stack to allocate them. So when I say scanner scanner,
-
41:47 - 41:50that really declares a scanner object right there
-
41:50 - 41:53and in this case there are no [inaudible] my constructor, so I don't have anything in
-
41:53 - 41:56parenths. If there were some arguments I would put parenths and put the
-
41:56 - 41:57information there,
-
41:57 - 42:01but the constructor is being called even with out this new. New actually is
-
42:01 - 42:04more about where the memory comes from. The constructor is called regardless of
-
42:04 - 42:07where the memory came from. And so this is the mechanism of C++ to get
-
42:07 - 42:11yourself an object tends to be, say the class name, say the name of the variable.
-
42:11 - 42:14If you have arguments for the constructor, they will go in parenths
-
42:14 - 42:16after the variable's name.
-
42:16 - 42:18So if scanner had
-
42:18 - 42:20something, I would be putting it right here,
-
42:20 - 42:25open parenth, yada, yada.
-
42:25 - 42:27So that's a little
-
42:27 - 42:30C++/Java
-
42:30 - 42:32difference. Oh, that's good. Question over
-
42:32 - 42:35here?
-
42:35 - 42:37When do we have to use the destructor?
-
42:37 - 42:41So typically you will not ever make a call that explicitly calls the
-
42:41 - 42:43destructor. It happens for you automatically. So you're - [inaudible] you're gonna
-
42:43 - 42:47see it in the interface as part of the completeness of the class it, here's how I
-
42:47 - 42:49set up, here's how I tear down.
-
42:49 - 42:51When we start implementing classes we'll have a reason to think more seriously about
-
42:51 - 42:55what goes in the destructor. But now you will never explicitly call it. Just know that
-
42:55 - 42:57it automatically gets called for you.
-
42:57 - 43:01The constructor kinda gets automatically called; the destructor gets automatically called, so
-
43:01 - 43:04just know that they're there. One
-
43:04 - 43:08of the things that's - I just want to encourage you not to get too
-
43:08 - 43:11bogged down in is that there's a lot of syntax to C++. I'm trying to give
-
43:11 - 43:15you the important parts that are going to matter early on, and we'll see more and
-
43:15 - 43:16more as we go through.
-
43:16 - 43:18Don't let it get you too overwhelmed, the feeling of it's
-
43:18 - 43:21almost but not quite like Java and it's going to make me crazy.
-
43:21 - 43:25Realize that
-
43:25 - 43:27there's just a little bit of differences that you kinda got to absorb, and once you
-
43:27 - 43:30get your head around them actually you will find yourself very able to
-
43:30 - 43:33express yourself without getting too tripped up by it. But it's just at the beginning I'm sure
-
43:33 - 43:35it feels like you've got this big list of here's a thousand things that are a
-
43:35 - 43:37little bit different that -
-
43:37 - 43:41and it will not be long before it will feel like your native language, so
-
43:41 - 43:46hang in there with us.
-
43:46 - 43:47So
-
43:47 - 43:50I wanted to show you the vector before we get done today and then we'll
-
43:50 - 43:55have a lot more chance to talk about this on Friday. That the other six
-
43:55 - 43:57classes that come in [inaudible] class library
-
43:57 - 44:00are all container classes. So containers are these things like they're buckets
-
44:00 - 44:04or shells or bags. They hold things for you. You stick things into the
-
44:04 - 44:07container and then later you can retrieve them.
-
44:07 - 44:11This turns out to be the most common need in all programs. If you look
-
44:11 - 44:14at all the things programs do, [inaudible] manipulating information, where are
-
44:14 - 44:18they putting that information, where are they storing it?
-
44:18 - 44:22One of the sorts of obvious needs is something that is just kind of a
-
44:22 - 44:25linear collection. I need to put together the 100 student that are in
-
44:25 - 44:29this class in a list, well what do I do - what do I use to do that?
-
44:29 - 44:33There is a build in kind of raw array, or primitive array in C++. I'm not
-
44:33 - 44:35even gonna show it to you right now.
-
44:35 - 44:37The truth is
-
44:37 - 44:42it's functional, it does kinda what it sets out to do, but it's very weak.
-
44:42 - 44:44It has constraints on how big it is
-
44:44 - 44:48and how it's access to it is. For example, you can make an array that has 10 members
-
44:48 - 44:51and then you can axe the 12th member or the 1,500th member
-
44:51 - 44:55without any good error reporting from either the compiler or the runtime
-
44:55 - 44:55system.
-
44:55 - 44:58That it's designed for kind of to be a professional's tool and it's very efficient,
-
44:58 - 45:00but it's not very safe.
-
45:00 - 45:04It doesn't have any convenience attached to it whatsoever. If you have a - you
-
45:04 - 45:07create a ten number array and later you decide you need to put 12 things into
-
45:07 - 45:07it,
-
45:07 - 45:11then your only recourse is to go create a new 12 number array and copy over
-
45:11 - 45:13those ten things
-
45:13 - 45:16and get rid of your old array and make a totally new one, that you can't take the
-
45:16 - 45:18one you have and just grow it
-
45:18 - 45:19
-
45:19 - 45:20in the standard language.
-
45:20 - 45:23So we'll come back to see it because it turns out there's some reasons we're gonna need to
-
45:23 - 45:27know how it works. But for now if you say if I needed to make a list what I want
-
45:27 - 45:28to use is the vector.
-
45:28 - 45:31So we have a vector class
-
45:31 - 45:32in our class library
-
45:32 - 45:36that just solves this problem of you need to collect up this sequence of
-
45:36 - 45:39things, a bunch of scores on a test,
-
45:39 - 45:41a bunch of students who are in a class,
-
45:41 - 45:44a bunch of name
-
45:44 - 45:46that are being invited to a party.
-
45:46 - 45:50And what it does for you is the things that array does but with safety
-
45:50 - 45:52and convenience built into it.
-
45:52 - 45:55So it does bounds checking. If you created a vector and you put ten things
-
45:55 - 45:56into it,
-
45:56 - 45:59then you can ask for the zero through 9th entries, but you cannot ask
-
45:59 - 46:02for the 22nd entry, it will raise an error and
-
46:02 - 46:05it will use that error function, you will get a big red error message, you will not
-
46:05 - 46:07
-
46:07 - 46:09bludgeon on unknowingly.
-
46:09 - 46:12You can add things and insert them and then remove them. So I can go into the array and
-
46:12 - 46:15say I'd like to put something in slot zero, it will shuffle everything over and make
-
46:15 - 46:19that space. If I say delete the element that's at zero it will move everything
-
46:19 - 46:21down. So it just does all this kind of handling of
-
46:21 - 46:24keeping the integrity of the list
-
46:24 - 46:26and its ordering maintained
-
46:26 - 46:28on your behalf.
-
46:28 - 46:30It also does all the
-
46:30 - 46:33management of how much storage space is needed. So if I put ten things into
-
46:33 - 46:36the vector and I put the 11th or the 12th or the - add
-
46:36 - 46:37100 more,
-
46:37 - 46:39it knows how to make the space necessary for it.
-
46:39 - 46:42Behind the scenes it's figuring out where I can get that space and how to take
-
46:42 - 46:46care of it. It always knows what count it has and what's going on there, but
-
46:46 - 46:50its doing this on our behalf in a way that that rawray just does not, that becomes
-
46:50 - 46:55very tedious and error prone if it's our responsibility to deal with it.
-
46:55 - 46:59So what the vector is kind of running, it's an instruction. And this is a key word for us in
-
46:59 - 47:01things that we're going to be talking about this quarter
-
47:01 - 47:01is that
-
47:01 - 47:03what you really wanted was a list.
-
47:03 - 47:07I want a list of students and I want to be able to put it in sorted order or
-
47:07 - 47:08find this person or print them.
-
47:08 - 47:11The fact that where the memory came from and how it's keeping track of is really
-
47:11 - 47:15a tedious detail that I'd rather not have to deal with. And that's exactly
-
47:15 - 47:17what the vector's gonna do for you, is make it so
-
47:17 - 47:21you store things and the storage is somebody else's problem.
-
47:21 - 47:23You use a list,
-
47:23 - 47:27you get an abstraction.
-
47:27 - 47:30How that - there's one little quirk, and this is
-
47:30 - 47:33not so startling to those of you who have
-
47:33 - 47:35worked on a recent version of Java,
-
47:35 - 47:38is in order to make the vector generally useful,
-
47:38 - 47:40it cannot store just one type of thing.
-
47:40 - 47:43That you can't make a vector that stores [inaudible] and
-
47:43 - 47:46service everyone's needs, that it has to be able to hold vectors of doubles
-
47:46 - 47:49or vectors of strings or vectors of student structures
-
47:49 - 47:51equally well.
-
47:51 - 47:55And so the way the vector class is actually supplied is using a
-
47:55 - 47:58feature in the C++ language called templates where
-
47:58 - 48:02the vector describes what it's storing using a placeholder. It says, well this is a
-
48:02 - 48:04vector of something and
-
48:04 - 48:08when you put these things in they all have to be the same type of thing
-
48:08 - 48:11and when you get one out you'll get the thing you put in,
-
48:11 - 48:14but I will not commit to, and the interface saying it's always an integer,
-
48:14 - 48:16it's always a double.
-
48:16 - 48:19It's left open and then the client has to describe what they want when they're
-
48:19 - 48:20ready to use it.
-
48:20 - 48:24So this is like the Java generics. When you're using an array list you said, well what
-
48:24 - 48:27kind of things am I sticking in my array list, and then that way
-
48:27 - 48:34the compiler can keep track of it for you and help you to use it correctly.
-
48:34 - 48:36The interpart of this kinda
-
48:36 - 48:38looks as
-
48:38 - 48:40we've seen before. It's a class vector,
-
48:40 - 48:42it has a constructor and destructor
-
48:42 - 48:44and it has some operations that
-
48:44 - 48:46return things like the number of elements that you can find out whether it
-
48:46 - 48:50has zero elements, you can get the element at index, you can set the element at
-
48:50 - 48:51index,
-
48:51 - 48:54you can add, insert and remove
-
48:54 - 48:55things within there.
-
48:55 - 48:56
-
48:56 - 48:59The one thing that's a little bit unusual about it is that every time it's
-
48:59 - 49:02talking about the type of something that's going into the vector or
-
49:02 - 49:04something that's coming out of the vector,
-
49:04 - 49:06it uses this elem type
-
49:06 - 49:11which traces its origin back to this template header up there,
-
49:11 - 49:14that is the clue to you that the vector
-
49:14 - 49:15doesn't
-
49:15 - 49:19commit to I'm storing ants, I'm storing doubles, I'm storing strings, it stores some
-
49:19 - 49:22generic elem type thing,
-
49:22 - 49:26which went the client is ready to create a vector, they will have to make
-
49:26 - 49:30that commitment and say this vector is gonna hold doubles, this vector is
-
49:30 - 49:31gonna hold ants,
-
49:31 - 49:34and from that point forward that vector knows that the
-
49:34 - 49:39getat on a vector of ants returns something of n type. And then add on a vector of nts
-
49:39 - 49:41expects a perimeter of n type,
-
49:41 - 49:44which is distinct from a vector of strings or a vector
-
49:44 - 49:45of doubles. So I'll
-
49:45 - 49:49show you a little code and we'll have to just really talk about this more deeply on
-
49:49 - 49:49Friday.
-
49:49 - 49:51A
-
49:51 - 49:54little bit of this in text for how I make a vector of [inaudible] how I make a vector of
-
49:54 - 49:56strings, and
-
49:56 - 49:56then
-
49:56 - 49:58some of the things that you could try to mix up
-
49:58 - 50:01that the template will actually
-
50:01 - 50:02not let you get away with,
-
50:02 - 50:05mixing those types. So
-
50:05 - 50:07we'll see this on Friday, so don't worry,
-
50:07 - 50:08
-
50:08 - 50:10there will be time to look at it
-
50:10 - 50:17and meanwhile good luck getting your compiler set up.
- Title:
- Lecture 4 | Programming Abstractions (Stanford)
- Description:
-
Lecture 4 by Julie Zelenski for the Programming Abstractions Course (CS106B) in the Stanford Computer Science Department.
Julie continues to cover the console input/output in C++ and discusses the file I/O and changing between an old style string to a C++ string format. She also begins to go over stream operations and their basic use as well as object oriented programming, and the use of scanner function and containers.
Complete Playlist for the Course:
http://www.youtube.com/view_play_list?p=FE6E58F856038C69CS 106B Course Website:
http://cs106b.stanford.eduStanford Center for Professional Development:
http://scpd.stanford.edu/Stanford University:
http://www.stanford.edu/Stanford University Channel on YouTube:
http://www.youtube.com/stanforduniversity/ - Video Language:
- English
- Duration:
- 50:27
N. Ueda edited English subtitles for Lecture 4 | Programming Abstractions (Stanford) | ||
Eunjeong_Kim added a translation |