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