[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.00,0:00:08.00,Default,,0000,0000,0000,, Dialogue: 0,0:00:08.00,0:00:12.00,Default,,0000,0000,0000,,This presentation is delivered by the Stanford Center for Professional Dialogue: 0,0:00:12.00,0:00:16.00,Default,,0000,0000,0000,,Development. Dialogue: 0,0:00:22.00,0:00:26.00,Default,,0000,0000,0000,,Okay. Well welcome, welcome. We got a couple parents who have braved the Dialogue: 0,0:00:26.00,0:00:29.00,Default,,0000,0000,0000,,other activities of Parents Weekend to come here, so thank you for coming to see what Dialogue: 0,0:00:29.00,0:00:32.00,Default,,0000,0000,0000,,it is that we do here in Dialogue: 0,0:00:32.00,0:00:36.00,Default,,0000,0000,0000,,CS106B. We are still in the process of grading the midterm. In fact, most of our grading, we're planning on Dialogue: 0,0:00:36.00,0:00:40.00,Default,,0000,0000,0000,,taking care of tomorrow, no, Sunday, in some big, massive grading sessions. So with any Dialogue: 0,0:00:40.00,0:00:42.00,Default,,0000,0000,0000,,luck, if it all goes well, we'll actually have them Dialogue: 0,0:00:42.00,0:00:45.00,Default,,0000,0000,0000,,ready to go back on Monday, but if somewhere it turns into some disaster, it may take us a Dialogue: 0,0:00:45.00,0:00:48.00,Default,,0000,0000,0000,,little longer, but hopefully. That's the plan. Dialogue: 0,0:00:48.00,0:00:51.00,Default,,0000,0000,0000,,So I'm gonna finish up a couple slides from last time, Dialogue: 0,0:00:51.00,0:00:54.00,Default,,0000,0000,0000,,and then I'm gonna mostly go on to do some code with you together. Dialogue: 0,0:00:54.00,0:00:56.00,Default,,0000,0000,0000,,But I just wanted to Dialogue: 0,0:00:56.00,0:00:56.00,Default,,0000,0000,0000,, Dialogue: 0,0:00:56.00,0:01:00.00,Default,,0000,0000,0000,,try to give you some perspective on the value of abstraction and the idea of an Dialogue: 0,0:01:00.00,0:01:03.00,Default,,0000,0000,0000,,abstract data type, or an ADT, and why Dialogue: 0,0:01:03.00,0:01:07.00,Default,,0000,0000,0000,,this is such a powerful and important concept in management of complexity. Dialogue: 0,0:01:07.00,0:01:09.00,Default,,0000,0000,0000,,So we saw this, for example, Dialogue: 0,0:01:09.00,0:01:13.00,Default,,0000,0000,0000,,in those first couple assignments that the client uses classes as Dialogue: 0,0:01:13.00,0:01:14.00,Default,,0000,0000,0000,,an abstraction. Dialogue: 0,0:01:14.00,0:01:18.00,Default,,0000,0000,0000,,You have the need to manage something that has a queue-like behavior: first in, Dialogue: 0,0:01:18.00,0:01:19.00,Default,,0000,0000,0000,,first out. Dialogue: 0,0:01:19.00,0:01:23.00,Default,,0000,0000,0000,,So what you want is something that you can put things in that will enforce that: Dialogue: 0,0:01:23.00,0:01:26.00,Default,,0000,0000,0000,,put things at the end of the line, always return to the head of the line. Dialogue: 0,0:01:26.00,0:01:29.00,Default,,0000,0000,0000,,And that how it managed what it did and what went on behind the scenes Dialogue: 0,0:01:29.00,0:01:33.00,Default,,0000,0000,0000,,wasn't something we had to worry about or even concern ourselves with. And in Dialogue: 0,0:01:33.00,0:01:36.00,Default,,0000,0000,0000,,fact, even if we wanted to, we couldn't muck around in there. Like Dialogue: 0,0:01:36.00,0:01:39.00,Default,,0000,0000,0000,,it wasn't our business, it was maintained privately. Dialogue: 0,0:01:39.00,0:01:44.00,Default,,0000,0000,0000,,And that is a real advantage for both sides, right? That the client Dialogue: 0,0:01:44.00,0:01:47.00,Default,,0000,0000,0000,,doesn't have to worry about it and can't even get in the way of us, that we Dialogue: 0,0:01:47.00,0:01:50.00,Default,,0000,0000,0000,,can work independently and get our things done. Dialogue: 0,0:01:50.00,0:01:54.00,Default,,0000,0000,0000,,And so one sort of piece of terminology we often use here, we talk about this wall of Dialogue: 0,0:01:54.00,0:01:55.00,Default,,0000,0000,0000,,abstraction. Dialogue: 0,0:01:55.00,0:01:57.00,Default,,0000,0000,0000,,That there is kind of a Dialogue: 0,0:01:57.00,0:01:58.00,Default,,0000,0000,0000,,real Dialogue: 0,0:01:58.00,0:02:00.00,Default,,0000,0000,0000,,block Dialogue: 0,0:02:00.00,0:02:04.00,Default,,0000,0000,0000,,that prevents the two of us from interfering with each other's process, as Dialogue: 0,0:02:04.00,0:02:06.00,Default,,0000,0000,0000,,part of, you know, combining to build a program together. Dialogue: 0,0:02:06.00,0:02:10.00,Default,,0000,0000,0000,,And there's a little tiny chink in that wall that we call the interface. Dialogue: 0,0:02:10.00,0:02:13.00,Default,,0000,0000,0000,,And that's the way that you speak to the stack and ask it to do things on your Dialogue: 0,0:02:13.00,0:02:16.00,Default,,0000,0000,0000,,behalf, and it listens to your requests and performs them. Dialogue: 0,0:02:16.00,0:02:19.00,Default,,0000,0000,0000,,And so if you think about the lexicon class, which we used in the Boggle and Dialogue: 0,0:02:19.00,0:02:22.00,Default,,0000,0000,0000,,in the recursion assignment that managed a word list, Dialogue: 0,0:02:22.00,0:02:26.00,Default,,0000,0000,0000,,the abstraction is, yeah, you say, ''Is this word in there? Add this word to it. Load Dialogue: 0,0:02:26.00,0:02:29.00,Default,,0000,0000,0000,,the words from a file.'' How does it store them? How does it do stuff? Did anybody Dialogue: 0,0:02:29.00,0:02:33.00,Default,,0000,0000,0000,,go open up the lexicon.cpp file just to see? Dialogue: 0,0:02:33.00,0:02:34.00,Default,,0000,0000,0000,,Anybody who was curious? And Dialogue: 0,0:02:34.00,0:02:37.00,Default,,0000,0000,0000,,what did you find out? I just - Dialogue: 0,0:02:37.00,0:02:40.00,Default,,0000,0000,0000,,I Dialogue: 0,0:02:40.00,0:02:40.00,Default,,0000,0000,0000,,think Dialogue: 0,0:02:40.00,0:02:44.00,Default,,0000,0000,0000,,it ended up in there [inaudible]. You ended up in there and when you got in there, what did you decide to do? Dialogue: 0,0:02:44.00,0:02:45.00,Default,,0000,0000,0000,,Leave. Leave. Dialogue: 0,0:02:45.00,0:02:48.00,Default,,0000,0000,0000,,Yeah. Did anybody else open it up and have the same sort of reaction? Over Dialogue: 0,0:02:48.00,0:02:49.00,Default,,0000,0000,0000,,here, Dialogue: 0,0:02:49.00,0:02:54.00,Default,,0000,0000,0000,,what did you think? I didn't really understand. Yeah. And what did you see? It was a mess. It was a mess. Who wrote that code? My, gosh, she should Dialogue: 0,0:02:54.00,0:02:57.00,Default,,0000,0000,0000,,be fired. Dialogue: 0,0:02:57.00,0:03:00.00,Default,,0000,0000,0000,,It's scary. It does something kind of scary. We'll talk about it. Actually, at the end, we'll come back here because I think it's actually Dialogue: 0,0:03:00.00,0:03:02.00,Default,,0000,0000,0000,,a really neat class to study. Dialogue: 0,0:03:02.00,0:03:04.00,Default,,0000,0000,0000,,But in fact, like you open it up and you're like, Dialogue: 0,0:03:04.00,0:03:09.00,Default,,0000,0000,0000,,''I don't want to be here. I want to use a word list. Let me close this file and let me go back to word list. Add word, Dialogue: 0,0:03:09.00,0:03:11.00,Default,,0000,0000,0000,,contains word, okay.'' Dialogue: 0,0:03:11.00,0:03:14.00,Default,,0000,0000,0000,,And you're happy about that. Right? It does something very complex that turns out to Dialogue: 0,0:03:14.00,0:03:18.00,Default,,0000,0000,0000,,be very efficient and optimize for the task at hand. But back to Boggle, Dialogue: 0,0:03:18.00,0:03:20.00,Default,,0000,0000,0000,,you don't want to be worrying about that; you got other things on your Dialogue: 0,0:03:20.00,0:03:25.00,Default,,0000,0000,0000,,plate and that's a fine place to just poke your nose right back out of. So if you haven't had a Dialogue: 0,0:03:25.00,0:03:28.00,Default,,0000,0000,0000,,chance to look at it, because we will talk about it later, but the person who wrote it, Dialogue: 0,0:03:28.00,0:03:30.00,Default,,0000,0000,0000,,was a crummy commoner. I'm just Dialogue: 0,0:03:30.00,0:03:32.00,Default,,0000,0000,0000,,saying Dialogue: 0,0:03:32.00,0:03:36.00,Default,,0000,0000,0000,,that they would definitely not be getting a check-plus on style, so. Dialogue: 0,0:03:36.00,0:03:38.00,Default,,0000,0000,0000,,So I drew this picture. It's like this wall. Right? Dialogue: 0,0:03:38.00,0:03:42.00,Default,,0000,0000,0000,,So when you made a lexicon, you say, ''Oh, I want a lexicon. Add this word. Add that word. Does it Dialogue: 0,0:03:42.00,0:03:44.00,Default,,0000,0000,0000,,contain this word?'' Dialogue: 0,0:03:44.00,0:03:47.00,Default,,0000,0000,0000,,And that there is this big wall that - and you think of what's on the other side as a black box. The Dialogue: 0,0:03:47.00,0:03:50.00,Default,,0000,0000,0000,,black box is the microwave that you push buttons and food gets hot. How does Dialogue: 0,0:03:50.00,0:03:53.00,Default,,0000,0000,0000,,it really work? Ah, you know, who knows. Dialogue: 0,0:03:53.00,0:03:55.00,Default,,0000,0000,0000,,You don't open up the back. You don't dig around in it. Dialogue: 0,0:03:55.00,0:03:58.00,Default,,0000,0000,0000,,And this little chink here, that's the interface. And the Dialogue: 0,0:03:58.00,0:04:01.00,Default,,0000,0000,0000,,lexicon provides a very small interface, if you remember, adding the words, checking Dialogue: 0,0:04:01.00,0:04:04.00,Default,,0000,0000,0000,,contains word and prefix, reading Dialogue: 0,0:04:04.00,0:04:07.00,Default,,0000,0000,0000,,the words from a file, really not much else beyond that. Dialogue: 0,0:04:07.00,0:04:11.00,Default,,0000,0000,0000,,And then now what we're starting to think about is, ''Well, what's this other side look like?'' What goes over Dialogue: 0,0:04:11.00,0:04:13.00,Default,,0000,0000,0000,,here Dialogue: 0,0:04:13.00,0:04:15.00,Default,,0000,0000,0000,,is that there is this implementer Dialogue: 0,0:04:15.00,0:04:18.00,Default,,0000,0000,0000,,who does know the internal structure. Who does know what shenanigans are being Dialogue: 0,0:04:18.00,0:04:21.00,Default,,0000,0000,0000,,pulled on that inside, who does have access to that private data, Dialogue: 0,0:04:21.00,0:04:25.00,Default,,0000,0000,0000,,and who, upon request when you ask it add a word or look up a word or look up a Dialogue: 0,0:04:25.00,0:04:29.00,Default,,0000,0000,0000,,prefix, internally trolls through that private information and returns Dialogue: 0,0:04:29.00,0:04:31.00,Default,,0000,0000,0000,,information back to the client, Dialogue: 0,0:04:31.00,0:04:35.00,Default,,0000,0000,0000,,having mucked around in those internals. So when you say add a word, Dialogue: 0,0:04:35.00,0:04:38.00,Default,,0000,0000,0000,,maybe what its got is some array with a count of how many words are Dialogue: 0,0:04:38.00,0:04:41.00,Default,,0000,0000,0000,,used, and it sticks it in the next slot of the array Dialogue: 0,0:04:41.00,0:04:43.00,Default,,0000,0000,0000,,and updates its counter. Dialogue: 0,0:04:43.00,0:04:46.00,Default,,0000,0000,0000,,And we don't know what it does but it's not really our business as the client, but the Dialogue: 0,0:04:46.00,0:04:47.00,Default,,0000,0000,0000,,implementer has Dialogue: 0,0:04:47.00,0:04:48.00,Default,,0000,0000,0000,,kind of the full Dialogue: 0,0:04:48.00,0:04:50.00,Default,,0000,0000,0000,,picture of that. Dialogue: 0,0:04:50.00,0:04:53.00,Default,,0000,0000,0000,,And then over here on this side, if the client attempts to do the kind of things Dialogue: 0,0:04:53.00,0:04:57.00,Default,,0000,0000,0000,,that are really implementer specific, it tries to access directly the num words Dialogue: 0,0:04:57.00,0:04:58.00,Default,,0000,0000,0000,,and the words array Dialogue: 0,0:04:58.00,0:05:01.00,Default,,0000,0000,0000,,to go in and say, ''Yeah, I'd like to put pig in the array. How about I do this? How Dialogue: 0,0:05:01.00,0:05:04.00,Default,,0000,0000,0000,,about I change the number of words? Or how about I stick it in the slot at the beginning Dialogue: 0,0:05:04.00,0:05:06.00,Default,,0000,0000,0000,,and overwrite whatever's there.'' Dialogue: 0,0:05:06.00,0:05:09.00,Default,,0000,0000,0000,,That an attempt to do this Dialogue: 0,0:05:09.00,0:05:12.00,Default,,0000,0000,0000,,should fail. It should not compile because these fields, Dialogue: 0,0:05:12.00,0:05:14.00,Default,,0000,0000,0000,,that are part of the lexicon, Dialogue: 0,0:05:14.00,0:05:17.00,Default,,0000,0000,0000,,should have been declared private to begin with, Dialogue: 0,0:05:17.00,0:05:20.00,Default,,0000,0000,0000,,to make sure that that wall's maintained. Everything that's private is like Dialogue: 0,0:05:20.00,0:05:24.00,Default,,0000,0000,0000,,over here on this side of the wall, Dialogue: 0,0:05:24.00,0:05:27.00,Default,,0000,0000,0000,,inaccessible outside of that. And Dialogue: 0,0:05:27.00,0:05:30.00,Default,,0000,0000,0000,,so why do we think this is important? Dialogue: 0,0:05:30.00,0:05:34.00,Default,,0000,0000,0000,,Of all the ideas that come away from 106B, I think this is one of the top Dialogue: 0,0:05:34.00,0:05:38.00,Default,,0000,0000,0000,,three is this idea of abstraction. We actually even call the whole class Dialogue: 0,0:05:38.00,0:05:39.00,Default,,0000,0000,0000,,Programming Abstractions. Dialogue: 0,0:05:39.00,0:05:40.00,Default,,0000,0000,0000,,Because Dialogue: 0,0:05:40.00,0:05:45.00,Default,,0000,0000,0000,,that the advantage of using this as a strategy for solving larger and Dialogue: 0,0:05:45.00,0:05:49.00,Default,,0000,0000,0000,,more complicated problems is that you can divide up your tasks. Dialogue: 0,0:05:49.00,0:05:52.00,Default,,0000,0000,0000,,That you can say this is an abstraction, like a word list, and kind of have it be as fancy as Dialogue: 0,0:05:52.00,0:05:54.00,Default,,0000,0000,0000,,it needs to be behind the scenes Dialogue: 0,0:05:54.00,0:05:56.00,Default,,0000,0000,0000,,but very easy to use from the outside. Dialogue: 0,0:05:56.00,0:05:59.00,Default,,0000,0000,0000,,So that makes it easy for me to write some piece and give it to you Dialogue: 0,0:05:59.00,0:06:01.00,Default,,0000,0000,0000,,in a form that's easy for you to incorporate. Dialogue: 0,0:06:01.00,0:06:04.00,Default,,0000,0000,0000,,We can work on our things without stepping on each other. As you get to Dialogue: 0,0:06:04.00,0:06:06.00,Default,,0000,0000,0000,,larger and larger projects Dialogue: 0,0:06:06.00,0:06:07.00,Default,,0000,0000,0000,,beyond this class, Dialogue: 0,0:06:07.00,0:06:09.00,Default,,0000,0000,0000,,you'll need ways of making it so three people can work together without Dialogue: 0,0:06:09.00,0:06:12.00,Default,,0000,0000,0000,,stepping on each other's toes the whole time. And classes provide a really good Dialogue: 0,0:06:12.00,0:06:15.00,Default,,0000,0000,0000,,way of managing those boundaries Dialogue: 0,0:06:15.00,0:06:16.00,Default,,0000,0000,0000,,to keep each other Dialogue: 0,0:06:16.00,0:06:18.00,Default,,0000,0000,0000,,out of each other's hair. Dialogue: 0,0:06:18.00,0:06:21.00,Default,,0000,0000,0000,,And there's a lot of flexibility Dialogue: 0,0:06:21.00,0:06:24.00,Default,,0000,0000,0000,,given to us by this. And we're gonna see this actually as we go forward, that we can talk Dialogue: 0,0:06:24.00,0:06:25.00,Default,,0000,0000,0000,,about what a vector is. Dialogue: 0,0:06:25.00,0:06:29.00,Default,,0000,0000,0000,,It keeps things in index order. Or a stack is, it does LIFO. Dialogue: 0,0:06:29.00,0:06:30.00,Default,,0000,0000,0000,,But there is no guarantee there about Dialogue: 0,0:06:30.00,0:06:34.00,Default,,0000,0000,0000,,how it internally is implemented, no guarantee expressed or implied, and Dialogue: 0,0:06:34.00,0:06:37.00,Default,,0000,0000,0000,,that actually gives you a lot of flexibility as the implementer. Dialogue: 0,0:06:37.00,0:06:40.00,Default,,0000,0000,0000,,You can decide to do it one way today, and if upon later learning about some Dialogue: 0,0:06:40.00,0:06:43.00,Default,,0000,0000,0000,,other new technique or some way to save memory or time, Dialogue: 0,0:06:43.00,0:06:45.00,Default,,0000,0000,0000,,you can swap it out, Dialogue: 0,0:06:45.00,0:06:47.00,Default,,0000,0000,0000,,replace an implementation with something better, Dialogue: 0,0:06:47.00,0:06:51.00,Default,,0000,0000,0000,,and all the code that depends on it shouldn't require any changes. Dialogue: 0,0:06:51.00,0:06:54.00,Default,,0000,0000,0000,,That suddenly add word just runs twice as fast or ten times as fast, Dialogue: 0,0:06:54.00,0:06:57.00,Default,,0000,0000,0000,,would be something everybody could appreciate without having to do anything in their own Dialogue: 0,0:06:57.00,0:07:00.00,Default,,0000,0000,0000,,code to take advantage of that. Dialogue: 0,0:07:00.00,0:07:01.00,Default,,0000,0000,0000,,So these are good things to know. Dialogue: 0,0:07:01.00,0:07:05.00,Default,,0000,0000,0000,,So what I'm gonna do actually today is I'm gonna just stop doing slides, because I'm sick of doing Dialogue: 0,0:07:05.00,0:07:08.00,Default,,0000,0000,0000,,slides. We do way too many slides; I'm bored with slides. Dialogue: 0,0:07:08.00,0:07:11.00,Default,,0000,0000,0000,,And what I'm gonna do is I'm gonna actually go through the process of Dialogue: 0,0:07:11.00,0:07:13.00,Default,,0000,0000,0000,,developing vector Dialogue: 0,0:07:13.00,0:07:15.00,Default,,0000,0000,0000,,from just the ground up. Dialogue: 0,0:07:15.00,0:07:16.00,Default,,0000,0000,0000,,So my plan here is to say Dialogue: 0,0:07:16.00,0:07:17.00,Default,,0000,0000,0000,,our goal is to make Dialogue: 0,0:07:17.00,0:07:19.00,Default,,0000,0000,0000,,the vector abstraction Dialogue: 0,0:07:19.00,0:07:23.00,Default,,0000,0000,0000,,real, so to get dirty, get behind the scenes and say we know what vector is. It Dialogue: 0,0:07:23.00,0:07:24.00,Default,,0000,0000,0000,,acts like an array. It Dialogue: 0,0:07:24.00,0:07:26.00,Default,,0000,0000,0000,,has these indexed Dialogue: 0,0:07:26.00,0:07:29.00,Default,,0000,0000,0000,,slots. It's this linear collection. It lets you put things anywhere you like in Dialogue: 0,0:07:29.00,0:07:30.00,Default,,0000,0000,0000,,the vector. We're gonna Dialogue: 0,0:07:30.00,0:07:33.00,Default,,0000,0000,0000,,go through the process of making that whole thing. Dialogue: 0,0:07:33.00,0:07:35.00,Default,,0000,0000,0000,,And I'm gonna start Dialogue: 0,0:07:35.00,0:07:39.00,Default,,0000,0000,0000,,at the - with a simple form that actually is not templatized and then I'm gonna Dialogue: 0,0:07:39.00,0:07:42.00,Default,,0000,0000,0000,,change it to one that is templatized. So we're gonna see kind of the whole process from Dialogue: 0,0:07:42.00,0:07:44.00,Default,,0000,0000,0000,,start to end. So Dialogue: 0,0:07:44.00,0:07:48.00,Default,,0000,0000,0000,,all I have are empty files right now. I have myvector.h and myvector.cpp that Dialogue: 0,0:07:48.00,0:07:51.00,Default,,0000,0000,0000,,really have sort of nothing other than sort of boilerplate stuff in them. Dialogue: 0,0:07:51.00,0:07:54.00,Default,,0000,0000,0000,,So let me look at myvector.h to start with. Dialogue: 0,0:07:54.00,0:07:58.00,Default,,0000,0000,0000,,So I'm calling this class myvector so that it doesn't confuse with the existing Dialogue: 0,0:07:58.00,0:08:00.00,Default,,0000,0000,0000,,vector class, so that we have that name there. Dialogue: 0,0:08:00.00,0:08:05.00,Default,,0000,0000,0000,,And I'm gonna start by just putting in Dialogue: 0,0:08:05.00,0:08:06.00,Default,,0000,0000,0000,,the Dialogue: 0,0:08:06.00,0:08:09.00,Default,,0000,0000,0000,,simple parts of the interface, and then we'll see how many other things we Dialogue: 0,0:08:09.00,0:08:12.00,Default,,0000,0000,0000,,have time to kind of add into it. But I'm gonna get the kinda basic skeletal functions Dialogue: 0,0:08:12.00,0:08:15.00,Default,,0000,0000,0000,,down and show how they get implemented, and then we'll see what else we'll try. Dialogue: 0,0:08:15.00,0:08:19.00,Default,,0000,0000,0000,,So having the size is probably pretty important, being able to say, ''Well how many things will I Dialogue: 0,0:08:19.00,0:08:22.00,Default,,0000,0000,0000,,put in there,'' get that back out, Dialogue: 0,0:08:22.00,0:08:23.00,Default,,0000,0000,0000,,being able to add an element. Dialogue: 0,0:08:23.00,0:08:27.00,Default,,0000,0000,0000,,And I'm gonna assume that right now the vector's gonna hold just scranks. Dialogue: 0,0:08:27.00,0:08:29.00,Default,,0000,0000,0000,,Certainly that's not what we're gonna want in the long run, but I'm just gonna pick something Dialogue: 0,0:08:29.00,0:08:32.00,Default,,0000,0000,0000,,for now so that we have something to practice with. Dialogue: 0,0:08:32.00,0:08:36.00,Default,,0000,0000,0000,,And then I'm gonna have the get at, Dialogue: 0,0:08:36.00,0:08:38.00,Default,,0000,0000,0000,,which give it an index and return something. Okay, Dialogue: 0,0:08:38.00,0:08:41.00,Default,,0000,0000,0000,,so I think these are the only ones I'm gonna have in the first version. Dialogue: 0,0:08:41.00,0:08:47.00,Default,,0000,0000,0000,,And then the other ones, you remember, there's like a remove at, there's an insert at, Dialogue: 0,0:08:47.00,0:08:47.00,Default,,0000,0000,0000,,there's an Dialogue: 0,0:08:47.00,0:08:55.00,Default,,0000,0000,0000,,overloaded bracket operator, and things like that I'm not gonna show right away. Question? [Inaudible]. Oh yeah, yeah. This is Dialogue: 0,0:08:55.00,0:08:58.00,Default,,0000,0000,0000,,actually you know kind of just standard boilerplate for C or C++ Dialogue: 0,0:08:58.00,0:09:01.00,Default,,0000,0000,0000,,header files. And you'll see this again and again and again. I should really have pointed Dialogue: 0,0:09:01.00,0:09:02.00,Default,,0000,0000,0000,,this out at some point along the way Dialogue: 0,0:09:02.00,0:09:08.00,Default,,0000,0000,0000,,is that the compiler does not like to see two definitions of the same thing, ever. Dialogue: 0,0:09:08.00,0:09:11.00,Default,,0000,0000,0000,,Even if those definitions exactly match, it just gets its Dialogue: 0,0:09:11.00,0:09:15.00,Default,,0000,0000,0000,,total knickers in a twist about having seen a class myvector, another Dialogue: 0,0:09:15.00,0:09:16.00,Default,,0000,0000,0000,,class myvector. Dialogue: 0,0:09:16.00,0:09:18.00,Default,,0000,0000,0000,,And so if you Dialogue: 0,0:09:18.00,0:09:21.00,Default,,0000,0000,0000,,include the header file, myvector, one place and you include it some other place, it's gonna end up Dialogue: 0,0:09:21.00,0:09:24.00,Default,,0000,0000,0000,,thinking there's two myvectors classes out there and it's gonna have a problem. Dialogue: 0,0:09:24.00,0:09:28.00,Default,,0000,0000,0000,,So this little bit of boilerplate is to tell the compiler, ''If you haven't already Dialogue: 0,0:09:28.00,0:09:32.00,Default,,0000,0000,0000,,seen this header, now's the time to go in there.'' So this ifindex, if not Dialogue: 0,0:09:32.00,0:09:34.00,Default,,0000,0000,0000,,defined, and then the name there is something I made up, some symbol name Dialogue: 0,0:09:34.00,0:09:36.00,Default,,0000,0000,0000,,that's unique to this file. When I say, Dialogue: 0,0:09:36.00,0:09:38.00,Default,,0000,0000,0000,,''Define that symbol,'' so Dialogue: 0,0:09:38.00,0:09:41.00,Default,,0000,0000,0000,,it's like saying, ''I've seen it,'' and then down here at the very bottom, there's an end if Dialogue: 0,0:09:41.00,0:09:43.00,Default,,0000,0000,0000,,that matches it. And so, Dialogue: 0,0:09:43.00,0:09:44.00,Default,,0000,0000,0000,,in the case where we have - Dialogue: 0,0:09:44.00,0:09:47.00,Default,,0000,0000,0000,,this is the first time we've seen the file it'll say, ''If not to define the symbol it's Dialogue: 0,0:09:47.00,0:09:48.00,Default,,0000,0000,0000,,not.'' Dialogue: 0,0:09:48.00,0:09:51.00,Default,,0000,0000,0000,,It'll say, ''Define that symbol, see all this stuff, and then the end if.'' The second time Dialogue: 0,0:09:51.00,0:09:54.00,Default,,0000,0000,0000,,it gets here it'll say, ''If not define that symbol, say that symbol's already defined,'' so it'll skip Dialogue: 0,0:09:54.00,0:09:59.00,Default,,0000,0000,0000,,down to the end if. And so, every subsequent attempt to look at myvector Dialogue: 0,0:09:59.00,0:10:00.00,Default,,0000,0000,0000,,will be passed over. Dialogue: 0,0:10:00.00,0:10:03.00,Default,,0000,0000,0000,,If you don't have it, you'll get a lot of errors about, Dialogue: 0,0:10:03.00,0:10:05.00,Default,,0000,0000,0000,,''I've seen this thing before. And it looks like the one I saw before but I don't like Dialogue: 0,0:10:05.00,0:10:06.00,Default,,0000,0000,0000,,it. You Dialogue: 0,0:10:06.00,0:10:08.00,Default,,0000,0000,0000,,know smells suspicious to me.'' Dialogue: 0,0:10:08.00,0:10:13.00,Default,,0000,0000,0000,,So that is sort of standard boilerplate for any header file is to have this Dialogue: 0,0:10:13.00,0:10:16.00,Default,,0000,0000,0000,,multiple include protection on it. Anything else you Dialogue: 0,0:10:16.00,0:10:18.00,Default,,0000,0000,0000,,want to ask about the - way in the back? Why Dialogue: 0,0:10:18.00,0:10:20.00,Default,,0000,0000,0000,,would it look at it multiple times, though? Dialogue: 0,0:10:20.00,0:10:23.00,Default,,0000,0000,0000,,Well because sometimes you include it and sometimes - for example, think about genlib. Like I might Dialogue: 0,0:10:23.00,0:10:25.00,Default,,0000,0000,0000,,include genlib but then Dialogue: 0,0:10:25.00,0:10:27.00,Default,,0000,0000,0000,,I include something else that includes genlib. So there's these ways that Dialogue: 0,0:10:27.00,0:10:29.00,Default,,0000,0000,0000,,you could accidentally get in there more than once, Dialogue: 0,0:10:29.00,0:10:32.00,Default,,0000,0000,0000,,just because some other things depend on it, and the next thing you know. So Dialogue: 0,0:10:32.00,0:10:34.00,Default,,0000,0000,0000,,it's better to just have the header file never Dialogue: 0,0:10:34.00,0:10:38.00,Default,,0000,0000,0000,,complain about that, never let that happen, than to make it somebody else's responsibility Dialogue: 0,0:10:38.00,0:10:44.00,Default,,0000,0000,0000,,to make sure it never happened through the includes. Dialogue: 0,0:10:44.00,0:10:45.00,Default,,0000,0000,0000,,So I've got five Dialogue: 0,0:10:45.00,0:10:48.00,Default,,0000,0000,0000,,member functions here that I'm gonna have to implement. Dialogue: 0,0:10:48.00,0:10:52.00,Default,,0000,0000,0000,,And now I need to think about what the private data, this guy's gonna look like. So Dialogue: 0,0:10:52.00,0:10:55.00,Default,,0000,0000,0000,,now, we are the low-level implementer. Dialogue: 0,0:10:55.00,0:10:58.00,Default,,0000,0000,0000,,We're not building on anything else right now, because myvector is kind Dialogue: 0,0:10:58.00,0:11:00.00,Default,,0000,0000,0000,,of the bottommost piece of things here. Dialogue: 0,0:11:00.00,0:11:05.00,Default,,0000,0000,0000,,We've got nothing out there except for the raw array to do the job for us. So Dialogue: 0,0:11:05.00,0:11:06.00,Default,,0000,0000,0000,,typically, the most Dialogue: 0,0:11:06.00,0:11:07.00,Default,,0000,0000,0000,, Dialogue: 0,0:11:07.00,0:11:08.00,Default,,0000,0000,0000,,compatible Dialogue: 0,0:11:08.00,0:11:12.00,Default,,0000,0000,0000,,mechanism to map something like vector onto is the array. You know it has Dialogue: 0,0:11:12.00,0:11:16.00,Default,,0000,0000,0000,,contiguous memory, it allows you to do direct access to things by index, Dialogue: 0,0:11:16.00,0:11:19.00,Default,,0000,0000,0000,,and so that's where we're gonna start. And we'll come back and talk about that, whether Dialogue: 0,0:11:19.00,0:11:23.00,Default,,0000,0000,0000,,that's the only option we have here. But what I'm gonna put in here Dialogue: 0,0:11:23.00,0:11:24.00,Default,,0000,0000,0000,,is a pointer Dialogue: 0,0:11:24.00,0:11:25.00,Default,,0000,0000,0000,,to Dialogue: 0,0:11:25.00,0:11:26.00,Default,,0000,0000,0000,, Dialogue: 0,0:11:26.00,0:11:27.00,Default,,0000,0000,0000,,a string, or Dialogue: 0,0:11:27.00,0:11:31.00,Default,,0000,0000,0000,,in this case it's gonna be a pointer to a string Dialogue: 0,0:11:31.00,0:11:34.00,Default,,0000,0000,0000,,array. And so in C++ those both look the same, this says arr is a single pointer that points Dialogue: 0,0:11:34.00,0:11:35.00,Default,,0000,0000,0000,,to Dialogue: 0,0:11:35.00,0:11:38.00,Default,,0000,0000,0000,,a string in memory, which in our situation is actually gonna be a whole sequence Dialogue: 0,0:11:38.00,0:11:41.00,Default,,0000,0000,0000,,of strings in memory kind of one after another. Dialogue: 0,0:11:41.00,0:11:44.00,Default,,0000,0000,0000,,The array has no knowledge of its length Dialogue: 0,0:11:44.00,0:11:47.00,Default,,0000,0000,0000,,so we're gonna build is using new and things like that. It will not know how big it is. Dialogue: 0,0:11:47.00,0:11:50.00,Default,,0000,0000,0000,,It will be our job to manually track that. Dialogue: 0,0:11:50.00,0:11:52.00,Default,,0000,0000,0000,,So I'm gonna go ahead and have Dialogue: 0,0:11:52.00,0:11:55.00,Default,,0000,0000,0000,,two fields to go with it, and I'm gonna show you why I have two in a Dialogue: 0,0:11:55.00,0:12:00.00,Default,,0000,0000,0000,,minute. But instead of having just a length field that says the array is this long, Dialogue: 0,0:12:00.00,0:12:02.00,Default,,0000,0000,0000,,I'm actually gonna track two integers on it Dialogue: 0,0:12:02.00,0:12:05.00,Default,,0000,0000,0000,,because likely the thing I'm gonna do with the array is I'm gonna allocate it to a Dialogue: 0,0:12:05.00,0:12:07.00,Default,,0000,0000,0000,,certain size and then kind of fill it up. Dialogue: 0,0:12:07.00,0:12:09.00,Default,,0000,0000,0000,,And then when it gets Dialogue: 0,0:12:09.00,0:12:13.00,Default,,0000,0000,0000,,totally filled, then I'm gonna do a process of resizing it and enlarging it. Dialogue: 0,0:12:13.00,0:12:15.00,Default,,0000,0000,0000,,And so, at any given point, there may be Dialogue: 0,0:12:15.00,0:12:18.00,Default,,0000,0000,0000,,a little bit excess capacity, a little slack in the system. So we might start by Dialogue: 0,0:12:18.00,0:12:20.00,Default,,0000,0000,0000,,making it ten long Dialogue: 0,0:12:20.00,0:12:21.00,Default,,0000,0000,0000,,and then fill it up, Dialogue: 0,0:12:21.00,0:12:24.00,Default,,0000,0000,0000,,and then as we get to size 10, make it 20 or something like that. Dialogue: 0,0:12:24.00,0:12:27.00,Default,,0000,0000,0000,,So at any point, we'll need to know things about that array: how many of Dialogue: 0,0:12:27.00,0:12:31.00,Default,,0000,0000,0000,,the slots are actually used, so that'll be the first five slots are in use; and then Dialogue: 0,0:12:31.00,0:12:33.00,Default,,0000,0000,0000,,the number of allocated will tell me how many other - how many Dialogue: 0,0:12:33.00,0:12:37.00,Default,,0000,0000,0000,,total slots do I have, so how many slots can I use before Dialogue: 0,0:12:37.00,0:12:37.00,Default,,0000,0000,0000,,I Dialogue: 0,0:12:37.00,0:12:40.00,Default,,0000,0000,0000,,will have run out of space. Dialogue: 0,0:12:40.00,0:12:42.00,Default,,0000,0000,0000,,Both of those. Okay, Dialogue: 0,0:12:42.00,0:12:45.00,Default,,0000,0000,0000,,so there's my interface. So these things all public because anybody can use Dialogue: 0,0:12:45.00,0:12:46.00,Default,,0000,0000,0000,,them; Dialogue: 0,0:12:46.00,0:12:49.00,Default,,0000,0000,0000,,these things private because they're gonna be part of my implementation; I don't want anybody mucking with Dialogue: 0,0:12:49.00,0:12:50.00,Default,,0000,0000,0000,,those Dialogue: 0,0:12:50.00,0:12:53.00,Default,,0000,0000,0000,,or directly accessing them at all, Dialogue: 0,0:12:53.00,0:12:55.00,Default,,0000,0000,0000,,so I put them down there. All Dialogue: 0,0:12:55.00,0:12:57.00,Default,,0000,0000,0000,,right, now I go to myvector.cpp. Dialogue: 0,0:12:57.00,0:13:01.00,Default,,0000,0000,0000,,So it includes myvector.h, so that it already has seen the class interface so it knows Dialogue: 0,0:13:01.00,0:13:04.00,Default,,0000,0000,0000,,when we're talking about when we start trying to implement methods on the myvector Dialogue: 0,0:13:04.00,0:13:06.00,Default,,0000,0000,0000,,class. And I'm Dialogue: 0,0:13:06.00,0:13:07.00,Default,,0000,0000,0000,,gonna start with Dialogue: 0,0:13:07.00,0:13:10.00,Default,,0000,0000,0000,,myvector's constructor, Dialogue: 0,0:13:10.00,0:13:13.00,Default,,0000,0000,0000,,and the goal of the constructor will be to setup the instance variables of this Dialogue: 0,0:13:13.00,0:13:17.00,Default,,0000,0000,0000,,object into a state that makes sense. Dialogue: 0,0:13:17.00,0:13:18.00,Default,,0000,0000,0000,,So what I'm gonna choose to do Dialogue: 0,0:13:18.00,0:13:24.00,Default,,0000,0000,0000,,is allocate my array to some size. I'm gonna pick ten. So Dialogue: 0,0:13:24.00,0:13:28.00,Default,,0000,0000,0000,,I'm gonna say I want space for ten strings. Dialogue: 0,0:13:28.00,0:13:31.00,Default,,0000,0000,0000,,I want to record that I made space for ten strings so I know the num Dialogue: 0,0:13:31.00,0:13:33.00,Default,,0000,0000,0000,,allocated, the size of my array right now is ten, Dialogue: 0,0:13:33.00,0:13:37.00,Default,,0000,0000,0000,,and the number used is zero. Dialogue: 0,0:13:37.00,0:13:39.00,Default,,0000,0000,0000,,So that means that when someone Dialogue: 0,0:13:39.00,0:13:46.00,Default,,0000,0000,0000,,makes a call, like a declaration, that says myvector V, so if I'm back over here in my main. Say Dialogue: 0,0:13:46.00,0:13:49.00,Default,,0000,0000,0000,,myvector V, Dialogue: 0,0:13:49.00,0:13:54.00,Default,,0000,0000,0000,,but the process of constructing that vector V will cause the constructor to get Dialogue: 0,0:13:54.00,0:13:58.00,Default,,0000,0000,0000,,called, will cause a ten-member string element array to get allocated out in the Dialogue: 0,0:13:58.00,0:13:58.00,Default,,0000,0000,0000,,heap Dialogue: 0,0:13:58.00,0:14:01.00,Default,,0000,0000,0000,,that's pointed to by arr, and then it will set those two integers to know that there's ten Dialogue: 0,0:14:01.00,0:14:03.00,Default,,0000,0000,0000,,of them and zero of them are used. Dialogue: 0,0:14:03.00,0:14:07.00,Default,,0000,0000,0000,,So just to kind of all as part of the machinery of declaring, the Dialogue: 0,0:14:07.00,0:14:11.00,Default,,0000,0000,0000,,constructor is just wired into that so we get setup, ready to go, with some empty Dialogue: 0,0:14:11.00,0:14:16.00,Default,,0000,0000,0000,,space set aside. Dialogue: 0,0:14:16.00,0:14:18.00,Default,,0000,0000,0000,,So to go with that, I'm Dialogue: 0,0:14:18.00,0:14:22.00,Default,,0000,0000,0000,,gonna go ahead and add the destructor right along side of it, Dialogue: 0,0:14:22.00,0:14:26.00,Default,,0000,0000,0000,,which is I need to be in charge of cleaning up my Dialogue: 0,0:14:26.00,0:14:28.00,Default,,0000,0000,0000,,dynamic allocation. I Dialogue: 0,0:14:28.00,0:14:30.00,Default,,0000,0000,0000,,allocated that with the new bracket, Dialogue: 0,0:14:30.00,0:14:33.00,Default,,0000,0000,0000,,the new that allocates out in the heap that uses the bracket form has a Dialogue: 0,0:14:33.00,0:14:37.00,Default,,0000,0000,0000,,matching delete bracket that says delete a whole array's worth of data, so not just Dialogue: 0,0:14:37.00,0:14:40.00,Default,,0000,0000,0000,,one string out there but a sequence of them. Dialogue: 0,0:14:40.00,0:14:43.00,Default,,0000,0000,0000,,We don't have to tell it the size; it actually does - as much as I said, it doesn't Dialogue: 0,0:14:43.00,0:14:47.00,Default,,0000,0000,0000,,its size. Well somewhere in the internals, it does know the size but it never Dialogue: 0,0:14:47.00,0:14:49.00,Default,,0000,0000,0000,,exposes it to us. So in fact, once I delete [inaudible] array, Dialogue: 0,0:14:49.00,0:14:52.00,Default,,0000,0000,0000,,it knows how much space is there to cleanup. Yeah? Dialogue: 0,0:14:52.00,0:14:54.00,Default,,0000,0000,0000,,Are you just temporarily Dialogue: 0,0:14:54.00,0:14:58.00,Default,,0000,0000,0000,,setting it up so the vector only works on strings? Yes, we are. Okay. Yes. We're gonna Dialogue: 0,0:14:58.00,0:14:59.00,Default,,0000,0000,0000,,come back and fix that, but Dialogue: 0,0:14:59.00,0:15:02.00,Default,,0000,0000,0000,,I think it's easier maybe to see it one time on a fixed type and then say, ''Well, what happens when you Dialogue: 0,0:15:02.00,0:15:05.00,Default,,0000,0000,0000,,go to template? What things change?'' And we'll see all the places that we have to make Dialogue: 0,0:15:05.00,0:15:08.00,Default,,0000,0000,0000,,modifications. Dialogue: 0,0:15:08.00,0:15:11.00,Default,,0000,0000,0000,,So I have myvector Dialogue: 0,0:15:11.00,0:15:13.00,Default,,0000,0000,0000,,size. Dialogue: 0,0:15:13.00,0:15:17.00,Default,,0000,0000,0000,,Which variable's the one that tells us about size? Dialogue: 0,0:15:17.00,0:15:20.00,Default,,0000,0000,0000,,I got none used. I got none allocated. Which is the right one? Num Dialogue: 0,0:15:20.00,0:15:24.00,Default,,0000,0000,0000,,used. Num used, that is exactly right. So num allocated turned out to be something we only will Dialogue: 0,0:15:24.00,0:15:29.00,Default,,0000,0000,0000,,use internally. That's not gonna - no one's gonna see that or know about it, but it is - Dialogue: 0,0:15:29.00,0:15:33.00,Default,,0000,0000,0000,,the num used tracks how many elements have actually been put in there. Then Dialogue: 0,0:15:33.00,0:15:35.00,Default,,0000,0000,0000,,I write Dialogue: 0,0:15:35.00,0:15:39.00,Default,,0000,0000,0000,,myvector add. Dialogue: 0,0:15:39.00,0:15:42.00,Default,,0000,0000,0000,,So I'm gonna write one line of code, then I'm gonna come back and think about it for a Dialogue: 0,0:15:42.00,0:15:44.00,Default,,0000,0000,0000,,second. I could Dialogue: 0,0:15:44.00,0:15:45.00,Default,,0000,0000,0000,,say Dialogue: 0,0:15:45.00,0:15:49.00,Default,,0000,0000,0000,,arr num Dialogue: 0,0:15:49.00,0:15:50.00,Default,,0000,0000,0000,,used ++ = S, so tight little line that Dialogue: 0,0:15:50.00,0:15:54.00,Default,,0000,0000,0000,,has a lot of stuff wrapped up in it. Dialogue: 0,0:15:54.00,0:15:55.00,Default,,0000,0000,0000,,Using Dialogue: 0,0:15:55.00,0:15:59.00,Default,,0000,0000,0000,,the brackets on the dynamic array here are saying, ''Take the array Dialogue: 0,0:15:59.00,0:16:03.00,Default,,0000,0000,0000,,and right to the num used slot, post-incrementing it so it's a slide effect.'' So if Dialogue: 0,0:16:03.00,0:16:08.00,Default,,0000,0000,0000,,num used is zero to start with, this has the effect of saying array of, Dialogue: 0,0:16:08.00,0:16:13.00,Default,,0000,0000,0000,,and then the num used ++ returns the value before incrementing. So it evaluates to zero, Dialogue: 0,0:16:13.00,0:16:17.00,Default,,0000,0000,0000,,but as a slide effect the next use of num used will now be one. So that's exactly Dialogue: 0,0:16:17.00,0:16:22.00,Default,,0000,0000,0000,,what we want, we want to write the slot zero and then have num used be one in subsequent usage. Dialogue: 0,0:16:22.00,0:16:25.00,Default,,0000,0000,0000,,And then we assign that to be S, so it'll put it in the right spot of the array. So once num Dialogue: 0,0:16:25.00,0:16:27.00,Default,,0000,0000,0000,,used is five, so that Dialogue: 0,0:16:27.00,0:16:31.00,Default,,0000,0000,0000,,means the zero through four slots are used. It'll write to the fifth slot and then Dialogue: 0,0:16:31.00,0:16:35.00,Default,,0000,0000,0000,,up the num used to be six, so we'll know our size is now up by one. Dialogue: 0,0:16:35.00,0:16:41.00,Default,,0000,0000,0000,,What else does add need to do? Is it good? Needs to make sure that we have that [inaudible]. It'd Dialogue: 0,0:16:41.00,0:16:43.00,Default,,0000,0000,0000,,better Dialogue: 0,0:16:43.00,0:16:44.00,Default,,0000,0000,0000,,make sure we have some space. Dialogue: 0,0:16:44.00,0:16:48.00,Default,,0000,0000,0000,,So I'm gonna do this right now. I'm gonna say if num used Dialogue: 0,0:16:48.00,0:16:50.00,Default,,0000,0000,0000,,is equal to num allocated, I'm gonna raise an error. I'm Dialogue: 0,0:16:50.00,0:16:56.00,Default,,0000,0000,0000,,gonna come back to this but I'm gonna say - Dialogue: 0,0:16:56.00,0:16:57.00,Default,,0000,0000,0000,,or Dialogue: 0,0:16:57.00,0:17:00.00,Default,,0000,0000,0000,,for this first pass, we're gonna make it so it just doesn't grow. Picked some arbitrary size, it Dialogue: 0,0:17:00.00,0:17:04.00,Default,,0000,0000,0000,,got that big, and then it ran out of space. Question? Dialogue: 0,0:17:04.00,0:17:07.00,Default,,0000,0000,0000,,So when the - for the Dialogue: 0,0:17:07.00,0:17:09.00,Default,,0000,0000,0000,,vector zero, first time it gets called it's actually gonna Dialogue: 0,0:17:09.00,0:17:11.00,Default,,0000,0000,0000,,be placed in index one of the array? Dialogue: 0,0:17:11.00,0:17:16.00,Default,,0000,0000,0000,,So it's in place of index zero. So num used ++, any variable ++ returns the - it Dialogue: 0,0:17:16.00,0:17:20.00,Default,,0000,0000,0000,,evaluates to the value before it increments, and then as a side effect, kind of in the next Dialogue: 0,0:17:20.00,0:17:23.00,Default,,0000,0000,0000,,step, it will update the value. So there is a difference between Dialogue: 0,0:17:23.00,0:17:26.00,Default,,0000,0000,0000,,the ++ num used and the num ++ form. Dialogue: 0,0:17:26.00,0:17:28.00,Default,,0000,0000,0000,,Sometimes it's a little bit of an obscure detail we don't spend a lot of time on, but Dialogue: 0,0:17:28.00,0:17:30.00,Default,,0000,0000,0000,,++ num says Dialogue: 0,0:17:30.00,0:17:33.00,Default,,0000,0000,0000,,first increment it and then tell me what the newly incremented value is. Dialogue: 0,0:17:33.00,0:17:36.00,Default,,0000,0000,0000,,Num ++ says tell me what the value is right now and then, as a side effect, increment it Dialogue: 0,0:17:36.00,0:17:41.00,Default,,0000,0000,0000,,for later use. So the num used gets changed in that? It does get changed in that expression, but the Dialogue: 0,0:17:41.00,0:17:46.00,Default,,0000,0000,0000,,expression happened to evaluate to the value before it did that change. Dialogue: 0,0:17:46.00,0:17:49.00,Default,,0000,0000,0000,,Question? And Dialogue: 0,0:17:49.00,0:17:54.00,Default,,0000,0000,0000,,is it really necessary to have myvector:: or is - Oh yeah, yeah, yeah, yeah. - there any way for - So if I take this out, Dialogue: 0,0:17:54.00,0:17:55.00,Default,,0000,0000,0000,,and I could show you what happens if I do Dialogue: 0,0:17:55.00,0:17:59.00,Default,,0000,0000,0000,,this, is what the compiler's gonna think that is, is it thinks, ''Oh, there's just a function Dialogue: 0,0:17:59.00,0:18:03.00,Default,,0000,0000,0000,,called size that take the arguments and returns them in.'' And it doesn't exist in any Dialogue: 0,0:18:03.00,0:18:05.00,Default,,0000,0000,0000,,context; it's just a global function. Dialogue: 0,0:18:05.00,0:18:08.00,Default,,0000,0000,0000,,And then I try to compile it, if I go ahead and do it, Dialogue: 0,0:18:08.00,0:18:11.00,Default,,0000,0000,0000,,it's gonna say, ''Num used? What's num used? Where did num used come from?'' Dialogue: 0,0:18:11.00,0:18:14.00,Default,,0000,0000,0000,,So if I click on this it'll say, ''Yeah, num used was not declared in this scope.'' It has no idea where that Dialogue: 0,0:18:14.00,0:18:18.00,Default,,0000,0000,0000,,came from. So there is no mechanism in C++ to identify this as being a Dialogue: 0,0:18:18.00,0:18:21.00,Default,,0000,0000,0000,,member function other than scoping it with the right names. You say this is Dialogue: 0,0:18:21.00,0:18:23.00,Default,,0000,0000,0000,,the size Dialogue: 0,0:18:23.00,0:18:25.00,Default,,0000,0000,0000,,function Dialogue: 0,0:18:25.00,0:18:27.00,Default,,0000,0000,0000,,that is part of the myvector class. And now Dialogue: 0,0:18:27.00,0:18:31.00,Default,,0000,0000,0000,,it has the right context to interpret that and it knows that when you talk about num used, Dialogue: 0,0:18:31.00,0:18:34.00,Default,,0000,0000,0000,,''Oh, there's a private data member that matches that. That must be the receiver's Dialogue: 0,0:18:34.00,0:18:37.00,Default,,0000,0000,0000,,num used.'' So Dialogue: 0,0:18:37.00,0:18:38.00,Default,,0000,0000,0000,,one other Dialogue: 0,0:18:38.00,0:18:42.00,Default,,0000,0000,0000,,member function we got to go, Dialogue: 0,0:18:42.00,0:18:45.00,Default,,0000,0000,0000,,which is the one that returns, Dialogue: 0,0:18:45.00,0:18:47.00,Default,,0000,0000,0000,,of I do this, Dialogue: 0,0:18:47.00,0:18:49.00,Default,,0000,0000,0000,,it almost but not quite, Dialogue: 0,0:18:49.00,0:18:51.00,Default,,0000,0000,0000,,looks like what I wanted. So I Dialogue: 0,0:18:51.00,0:18:53.00,Default,,0000,0000,0000,,say return arrays of index. Dialogue: 0,0:18:53.00,0:18:56.00,Default,,0000,0000,0000,,They said get in the next and returned the string that was at that index. Dialogue: 0,0:18:56.00,0:19:03.00,Default,,0000,0000,0000,,Is there anything else I might want to do here? Check and see if the index is [inaudible]. Well, let me Dialogue: 0,0:19:03.00,0:19:05.00,Default,,0000,0000,0000,,check and see if that index is valid. Dialogue: 0,0:19:05.00,0:19:06.00,Default,,0000,0000,0000,,Now, this is one of those cases where it's like you Dialogue: 0,0:19:06.00,0:19:10.00,Default,,0000,0000,0000,,could just sort of take the stance that says, ''Well, it's not my job.'' If you ask me to get you Dialogue: 0,0:19:10.00,0:19:14.00,Default,,0000,0000,0000,,know something at index 25, and there are only four of them, that's your own fault and you Dialogue: 0,0:19:14.00,0:19:14.00,Default,,0000,0000,0000,,deserve Dialogue: 0,0:19:14.00,0:19:16.00,Default,,0000,0000,0000,,what you get. Dialogue: 0,0:19:16.00,0:19:18.00,Default,,0000,0000,0000,,And that is certainly Dialogue: 0,0:19:18.00,0:19:21.00,Default,,0000,0000,0000,,the way a lot of professional libraries work because this, if I added a step here Dialogue: 0,0:19:21.00,0:19:24.00,Default,,0000,0000,0000,,that does a check, it means that every check costs a little bit more. Dialogue: 0,0:19:24.00,0:19:26.00,Default,,0000,0000,0000,,When you go in to get something out of the vector, it's gonna have to look and Dialogue: 0,0:19:26.00,0:19:28.00,Default,,0000,0000,0000,,make sure you were doing it correctly. Dialogue: 0,0:19:28.00,0:19:31.00,Default,,0000,0000,0000,,And there are people who believe that if it's like riding a motorcycle without a helmet, Dialogue: 0,0:19:31.00,0:19:32.00,Default,,0000,0000,0000,,that's your own Dialogue: 0,0:19:32.00,0:19:33.00,Default,,0000,0000,0000,,choice. Dialogue: 0,0:19:33.00,0:19:35.00,Default,,0000,0000,0000,,We're gonna actually bullet proof; Dialogue: 0,0:19:35.00,0:19:36.00,Default,,0000,0000,0000,,we're gonna make sure Dialogue: 0,0:19:36.00,0:19:39.00,Default,,0000,0000,0000,,that the index Dialogue: 0,0:19:39.00,0:19:41.00,Default,,0000,0000,0000,,isn't whack. Dialogue: 0,0:19:41.00,0:19:45.00,Default,,0000,0000,0000,,And I'm gonna go ahead and use my own size there. I'm gonna say if the index is less zero or it's greater Dialogue: 0,0:19:45.00,0:19:47.00,Default,,0000,0000,0000,,than or equal to, and I'm gonna use size. Dialogue: 0,0:19:47.00,0:19:52.00,Default,,0000,0000,0000,,I could use num used there but there are also reasons that if I use my own member functions Dialogue: 0,0:19:52.00,0:19:55.00,Default,,0000,0000,0000,,and then later somehow I change that name or I change how I calculate the Dialogue: 0,0:19:55.00,0:19:59.00,Default,,0000,0000,0000,,size, let's say I change it to where I use a linked list within an array and I'm managing the size Dialogue: 0,0:19:59.00,0:20:00.00,Default,,0000,0000,0000,,differently, Dialogue: 0,0:20:00.00,0:20:03.00,Default,,0000,0000,0000,,that if I have used size in this context, it will just change along with that Dialogue: 0,0:20:03.00,0:20:06.00,Default,,0000,0000,0000,,because it's actually depending on the Dialogue: 0,0:20:06.00,0:20:08.00,Default,,0000,0000,0000,,other part of the interface that I'll update. Dialogue: 0,0:20:08.00,0:20:09.00,Default,,0000,0000,0000,,And so I'm gonna go ahead and do that Dialogue: 0,0:20:09.00,0:20:12.00,Default,,0000,0000,0000,,rather than directly asking my variable. I'll say if that happens I say error, out of bounds. Dialogue: 0,0:20:12.00,0:20:15.00,Default,,0000,0000,0000,,And on Dialogue: 0,0:20:15.00,0:20:18.00,Default,,0000,0000,0000,,that, that error will cause the program to halt right down there, so I don't have to worry about Dialogue: 0,0:20:18.00,0:20:21.00,Default,,0000,0000,0000,,it, in that case, getting down to the return. So I feel Dialogue: 0,0:20:21.00,0:20:22.00,Default,,0000,0000,0000,,okay about this. Dialogue: 0,0:20:22.00,0:20:25.00,Default,,0000,0000,0000,,Not too much code to get kind of a little bit of something up and Dialogue: 0,0:20:25.00,0:20:26.00,Default,,0000,0000,0000,,running here. Dialogue: 0,0:20:26.00,0:20:39.00,Default,,0000,0000,0000,,Let's go over and write something to test. Add Jason, and here we go. Okay, so I Dialogue: 0,0:20:39.00,0:20:41.00,Default,,0000,0000,0000,,put some things in there and I'm Dialogue: 0,0:20:41.00,0:20:43.00,Default,,0000,0000,0000,,gonna see if Dialogue: 0,0:20:43.00,0:20:50.00,Default,,0000,0000,0000,,it'll let me get them back out. Dialogue: 0,0:20:50.00,0:20:51.00,Default,,0000,0000,0000,,And Dialogue: 0,0:20:51.00,0:20:54.00,Default,,0000,0000,0000,,before I get any further, I might as well test the code I have. Right? Dialogue: 0,0:20:54.00,0:20:58.00,Default,,0000,0000,0000,,This is - one of the things about designing a class is it's pretty hard to write Dialogue: 0,0:20:58.00,0:21:02.00,Default,,0000,0000,0000,,any one piece and test it by itself because often there's a relationship: the constructor Dialogue: 0,0:21:02.00,0:21:04.00,Default,,0000,0000,0000,,and then some adding and then some getting stuff back out. Dialogue: 0,0:21:04.00,0:21:07.00,Default,,0000,0000,0000,,So it's a little bit more code than I typically would like to write and not have Dialogue: 0,0:21:07.00,0:21:10.00,Default,,0000,0000,0000,,a chance to test, having all five of these member functions kind of at first. Dialogue: 0,0:21:10.00,0:21:11.00,Default,,0000,0000,0000,,So Dialogue: 0,0:21:11.00,0:21:14.00,Default,,0000,0000,0000,,if I run this test and it doesn't work, it's like well which part what was wrong? Was the Dialogue: 0,0:21:14.00,0:21:17.00,Default,,0000,0000,0000,,constructor wrong? Was the add wrong? Was the size wrong? You know there's a bunch of Dialogue: 0,0:21:17.00,0:21:19.00,Default,,0000,0000,0000,,places to look, but Dialogue: 0,0:21:19.00,0:21:22.00,Default,,0000,0000,0000,,unfortunately, they're kind of all interrelated in a way that makes it a little hard to have them Dialogue: 0,0:21:22.00,0:21:23.00,Default,,0000,0000,0000,,independently Dialogue: 0,0:21:23.00,0:21:26.00,Default,,0000,0000,0000,,worked out. But then subsequent thing, hopefully I can add the like the Dialogue: 0,0:21:26.00,0:21:28.00,Default,,0000,0000,0000,,insert at method without Dialogue: 0,0:21:28.00,0:21:33.00,Default,,0000,0000,0000,,having to add a lot more before I test. Dialogue: 0,0:21:33.00,0:21:37.00,Default,,0000,0000,0000,,Okay. So I run this guy and it says Jason, Illia, Nathan. Feel good, I feel good, Dialogue: 0,0:21:37.00,0:21:41.00,Default,,0000,0000,0000,,I feel smart. Dialogue: 0,0:21:41.00,0:21:42.00,Default,,0000,0000,0000,,Put them in, in that order. See Dialogue: 0,0:21:42.00,0:21:44.00,Default,,0000,0000,0000,,me get them out in that order. I might Dialogue: 0,0:21:44.00,0:21:46.00,Default,,0000,0000,0000,,try some things that I'm hoping will cause my thing Dialogue: 0,0:21:46.00,0:21:48.00,Default,,0000,0000,0000,,to blow up. Dialogue: 0,0:21:48.00,0:21:53.00,Default,,0000,0000,0000,,Why don't I get at ten? Let's see, I Dialogue: 0,0:21:53.00,0:21:55.00,Default,,0000,0000,0000,,like to be sure, Dialogue: 0,0:21:55.00,0:21:59.00,Default,,0000,0000,0000,,so I want you to tell me what's at the tenth slot in that vector. Dialogue: 0,0:21:59.00,0:22:02.00,Default,,0000,0000,0000,,And it's, ooh, out of bounds, just like I was hoping for. Dialogue: 0,0:22:02.00,0:22:05.00,Default,,0000,0000,0000,,Oh, I like to see that. Right? Dialogue: 0,0:22:05.00,0:22:07.00,Default,,0000,0000,0000,,What happens if I put Dialogue: 0,0:22:07.00,0:22:10.00,Default,,0000,0000,0000,,in Dialogue: 0,0:22:10.00,0:22:11.00,Default,,0000,0000,0000,,enough strings Dialogue: 0,0:22:11.00,0:22:12.00,Default,,0000,0000,0000,,that I Dialogue: 0,0:22:12.00,0:22:16.00,Default,,0000,0000,0000,,run out of memory? And we talked about a thing - it's set it to be ten right now. Why don't I just make it Dialogue: 0,0:22:16.00,0:22:19.00,Default,,0000,0000,0000,,smaller, that'll way it'll make it easier for me. So I say, ''Oh how Dialogue: 0,0:22:19.00,0:22:21.00,Default,,0000,0000,0000,,about this?'' I'm Dialogue: 0,0:22:21.00,0:22:23.00,Default,,0000,0000,0000,,only gonna make Dialogue: 0,0:22:23.00,0:22:26.00,Default,,0000,0000,0000,,space for two things. And it just: Dialogue: 0,0:22:26.00,0:22:30.00,Default,,0000,0000,0000,,error, out of space. Right? That happened before it got to printing anything. It tried to add the first one. It Dialogue: 0,0:22:30.00,0:22:33.00,Default,,0000,0000,0000,,added the second one. Then it went to add that third one and it said, ''Oh okay, we run out of space. We Dialogue: 0,0:22:33.00,0:22:35.00,Default,,0000,0000,0000,,only had space for two set aside, you Dialogue: 0,0:22:35.00,0:22:40.00,Default,,0000,0000,0000,,asked me to put a third in. I had no room.'' Dialogue: 0,0:22:40.00,0:22:42.00,Default,,0000,0000,0000,,So at least the kind of simple behaviors that I put in here seem to kind of Dialogue: 0,0:22:42.00,0:22:46.00,Default,,0000,0000,0000,,show evidence that we've got a little part of this up and running. Dialogue: 0,0:22:46.00,0:22:48.00,Default,,0000,0000,0000,,What I'm gonna fix first is this out of space thing. Dialogue: 0,0:22:48.00,0:22:54.00,Default,,0000,0000,0000,,So it would be pretty bogus and pretty unusual for a Dialogue: 0,0:22:54.00,0:22:56.00,Default,,0000,0000,0000,,collection class like this to have some sort of fixed limit. Dialogue: 0,0:22:56.00,0:22:59.00,Default,,0000,0000,0000,,Right? It wouldn't - you know it'd be very unusual to say well it's always gonna hold exactly 10 Dialogue: 0,0:22:59.00,0:23:02.00,Default,,0000,0000,0000,,things or 100 things or even a 1,000 things. Right? Dialogue: 0,0:23:02.00,0:23:05.00,Default,,0000,0000,0000,,You know one way you might design it is you could imagine adding an Dialogue: 0,0:23:05.00,0:23:07.00,Default,,0000,0000,0000,,argument to the constructor that said, ''Well, how many things do you plan on putting in Dialogue: 0,0:23:07.00,0:23:11.00,Default,,0000,0000,0000,,there? And then I'll allocate it to that. And then when we run out of space, you know you're hosed.'' Dialogue: 0,0:23:11.00,0:23:14.00,Default,,0000,0000,0000,,But certainly a better strategy that kind of solves more general case problems would Dialogue: 0,0:23:14.00,0:23:16.00,Default,,0000,0000,0000,,be like, ''Oh let's grow. Dialogue: 0,0:23:16.00,0:23:20.00,Default,,0000,0000,0000,,When we run out of space, let's make some more space.'' Okay, Dialogue: 0,0:23:20.00,0:23:23.00,Default,,0000,0000,0000,,let's think about what it takes to make more space in Dialogue: 0,0:23:23.00,0:23:24.00,Default,,0000,0000,0000,,using pointers. Dialogue: 0,0:23:24.00,0:23:31.00,Default,,0000,0000,0000,,So what a vector really looks like Dialogue: 0,0:23:31.00,0:23:33.00,Default,,0000,0000,0000,,is it has three fields, Dialogue: 0,0:23:33.00,0:23:34.00,Default,,0000,0000,0000,,the arr, Dialogue: 0,0:23:34.00,0:23:39.00,Default,,0000,0000,0000,,the num used, and the num allocated. When Dialogue: 0,0:23:39.00,0:23:43.00,Default,,0000,0000,0000,,I declare one, the way it's being setup right now, it's over here and it's allocating space Dialogue: 0,0:23:43.00,0:23:45.00,Default,,0000,0000,0000,,for Dialogue: 0,0:23:45.00,0:23:47.00,Default,,0000,0000,0000,,some number, let's say it's ten again, Dialogue: 0,0:23:47.00,0:23:48.00,Default,,0000,0000,0000,,and then marking it as zero. Dialogue: 0,0:23:48.00,0:23:53.00,Default,,0000,0000,0000,,Then as it fills these things up, it puts strings in each of these slots and it Dialogue: 0,0:23:53.00,0:23:58.00,Default,,0000,0000,0000,,starts updating this number, eventually it gets to where all of them are filled. Dialogue: 0,0:23:58.00,0:24:02.00,Default,,0000,0000,0000,,That when num used equals num allocated, it means that however much space I set Dialogue: 0,0:24:02.00,0:24:05.00,Default,,0000,0000,0000,,aside, every one of those slots is now in use. Dialogue: 0,0:24:05.00,0:24:08.00,Default,,0000,0000,0000,,So when that happens, it's gonna be time to make a bigger array. Dialogue: 0,0:24:08.00,0:24:11.00,Default,,0000,0000,0000,,There is not a mechanism in C++ that says take my array and just make Dialogue: 0,0:24:11.00,0:24:13.00,Default,,0000,0000,0000,,it bigger where it is. Dialogue: 0,0:24:13.00,0:24:16.00,Default,,0000,0000,0000,,That the way we'll have to do this is, we'll have to make a bigger array, copy over Dialogue: 0,0:24:16.00,0:24:17.00,Default,,0000,0000,0000,,what we have, Dialogue: 0,0:24:17.00,0:24:18.00,Default,,0000,0000,0000,,and then, Dialogue: 0,0:24:18.00,0:24:22.00,Default,,0000,0000,0000,,you know, have it add it on by making a bigger array full of space. So what we'll do is we'll make Dialogue: 0,0:24:22.00,0:24:23.00,Default,,0000,0000,0000,,something that's like Dialogue: 0,0:24:23.00,0:24:27.00,Default,,0000,0000,0000,,twice as big, I'm just gonna draw it this way since I'm running out of board space, Dialogue: 0,0:24:27.00,0:24:31.00,Default,,0000,0000,0000,,and it's got ten slots and then ten more. And Dialogue: 0,0:24:31.00,0:24:36.00,Default,,0000,0000,0000,,then I will copy over all these guys that I have Dialogue: 0,0:24:36.00,0:24:38.00,Default,,0000,0000,0000,,up to the end, Dialogue: 0,0:24:38.00,0:24:40.00,Default,,0000,0000,0000,,and then I have these new spaces at the end. Dialogue: 0,0:24:40.00,0:24:41.00,Default,,0000,0000,0000,,And so I will have Dialogue: 0,0:24:41.00,0:24:44.00,Default,,0000,0000,0000,,to reset my pointer to point to there, Dialogue: 0,0:24:44.00,0:24:49.00,Default,,0000,0000,0000,,update my num allocated, let's say to be twice as big or something, Dialogue: 0,0:24:49.00,0:24:52.00,Default,,0000,0000,0000,,and then delete this old region that Dialogue: 0,0:24:52.00,0:24:54.00,Default,,0000,0000,0000,,I'm no longer using. Dialogue: 0,0:24:54.00,0:24:57.00,Default,,0000,0000,0000,,So we're gonna see an allocate, a copy, a delete, Dialogue: 0,0:24:57.00,0:25:00.00,Default,,0000,0000,0000,,and then kind of resetting our fields Dialogue: 0,0:25:00.00,0:25:01.00,Default,,0000,0000,0000,,to know what we just did. Dialogue: 0,0:25:01.00,0:25:05.00,Default,,0000,0000,0000,,So I'm gonna make a private helper to do all this, and Dialogue: 0,0:25:05.00,0:25:12.00,Default,,0000,0000,0000,,I'll just call that enlarge capacity here. Question? Is this like [inaudible]? Well Dialogue: 0,0:25:12.00,0:25:14.00,Default,,0000,0000,0000,,it is - it can be, is the truth. Dialogue: 0,0:25:14.00,0:25:16.00,Default,,0000,0000,0000,,So you're getting a little Dialogue: 0,0:25:16.00,0:25:18.00,Default,,0000,0000,0000,,bit ahead of us. But in the sense that like, Dialogue: 0,0:25:18.00,0:25:20.00,Default,,0000,0000,0000,,you know, if the array has a thousand elements Dialogue: 0,0:25:20.00,0:25:23.00,Default,,0000,0000,0000,,and now we got to put that thousand-first thing in, it's gonna take Dialogue: 0,0:25:23.00,0:25:26.00,Default,,0000,0000,0000,,all thousand and copy them over and enlarge the space. Dialogue: 0,0:25:26.00,0:25:30.00,Default,,0000,0000,0000,,So in effect what is typically an O of one operation, just tacking something on Dialogue: 0,0:25:30.00,0:25:34.00,Default,,0000,0000,0000,,the end, every now and then is gonna take a whole hit of an end Dialogue: 0,0:25:34.00,0:25:37.00,Default,,0000,0000,0000,,operation when it does the copy. But Dialogue: 0,0:25:37.00,0:25:39.00,Default,,0000,0000,0000,,one way to think about that is that is Dialogue: 0,0:25:39.00,0:25:42.00,Default,,0000,0000,0000,,every now it's really expensive but kind of if you think of it across the whole Dialogue: 0,0:25:42.00,0:25:45.00,Default,,0000,0000,0000,,space, that you got a - let's say you started at 1,000 and then you Dialogue: 0,0:25:45.00,0:25:46.00,Default,,0000,0000,0000,,doubled to 2,000, that Dialogue: 0,0:25:46.00,0:25:48.00,Default,,0000,0000,0000,,the first 1,000 never paid that cost. Dialogue: 0,0:25:48.00,0:25:51.00,Default,,0000,0000,0000,,And then all of a sudden one of them paid it but then now you don't pay it again for Dialogue: 0,0:25:51.00,0:25:55.00,Default,,0000,0000,0000,,another 1,000. But if you kind of divided that cost, sort of amortized it across all Dialogue: 0,0:25:55.00,0:25:56.00,Default,,0000,0000,0000,,those adds, Dialogue: 0,0:25:56.00,0:25:57.00,Default,,0000,0000,0000,,that it was a - Dialogue: 0,0:25:57.00,0:25:59.00,Default,,0000,0000,0000,,it didn't change the overall constant running time. Dialogue: 0,0:25:59.00,0:26:02.00,Default,,0000,0000,0000,,So you have to - you kind of think of it maybe in the picture. It does mean every now and Dialogue: 0,0:26:02.00,0:26:05.00,Default,,0000,0000,0000,,then though one of them is gonna surprise you with how slow it is, but Dialogue: 0,0:26:05.00,0:26:06.00,Default,,0000,0000,0000,, Dialogue: 0,0:26:06.00,0:26:07.00,Default,,0000,0000,0000,,hopefully that's Dialogue: 0,0:26:07.00,0:26:12.00,Default,,0000,0000,0000,,few and far between, if we've chosen our allocation strategy well. Dialogue: 0,0:26:12.00,0:26:15.00,Default,,0000,0000,0000,,So I'm gonna say - actually, I'm gonna do this. I'm gonna call this Dialogue: 0,0:26:15.00,0:26:17.00,Default,,0000,0000,0000,,thing actually double capacity. Dialogue: 0,0:26:17.00,0:26:21.00,Default,,0000,0000,0000,,So one strategy that often is used is you could go in little chunks. You could go like Dialogue: 0,0:26:21.00,0:26:23.00,Default,,0000,0000,0000,,ten at a time all the time. Dialogue: 0,0:26:23.00,0:26:24.00,Default,,0000,0000,0000,,But Dialogue: 0,0:26:24.00,0:26:27.00,Default,,0000,0000,0000,,if the array's gonna get very big, Dialogue: 0,0:26:27.00,0:26:30.00,Default,,0000,0000,0000,,going by tens will take you a lot of time. You might sort of use sometimes the indication of, ''Well Dialogue: 0,0:26:30.00,0:26:33.00,Default,,0000,0000,0000,,how big is it already?'' Then if that's about the size it seems to be, why don't we go ahead Dialogue: 0,0:26:33.00,0:26:34.00,Default,,0000,0000,0000,,and just double. Dialogue: 0,0:26:34.00,0:26:36.00,Default,,0000,0000,0000,,So if we start at 10, Dialogue: 0,0:26:36.00,0:26:39.00,Default,,0000,0000,0000,,you know, and then we go to 20, then we go to 40, then we to 80, then Dialogue: 0,0:26:39.00,0:26:42.00,Default,,0000,0000,0000,,as we get to bigger and bigger sizes, we'll make the kind of assumption that: well, Dialogue: 0,0:26:42.00,0:26:45.00,Default,,0000,0000,0000,,given how big it is already, it's not - it wouldn't surprise us if it got a lot bigger. So Dialogue: 0,0:26:45.00,0:26:48.00,Default,,0000,0000,0000,,let's just go ahead and allocate Dialogue: 0,0:26:48.00,0:26:50.00,Default,,0000,0000,0000,,twice what we have as a way of kind of Dialogue: 0,0:26:50.00,0:26:54.00,Default,,0000,0000,0000,,predicting that it's pretty big now, it might get even bigger in the future. That Dialogue: 0,0:26:54.00,0:26:55.00,Default,,0000,0000,0000,,isn't necessarily Dialogue: 0,0:26:55.00,0:26:58.00,Default,,0000,0000,0000,,the only strategy we could use, but it's one that actually often makes Dialogue: 0,0:26:58.00,0:27:01.00,Default,,0000,0000,0000,,pretty decent sense. And then at any given point, you'll know that the vector will Dialogue: 0,0:27:01.00,0:27:03.00,Default,,0000,0000,0000,,be somewhere between Dialogue: 0,0:27:03.00,0:27:05.00,Default,,0000,0000,0000,,full and half capacity, Dialogue: 0,0:27:05.00,0:27:07.00,Default,,0000,0000,0000,,is what you're setting up for. Dialogue: 0,0:27:07.00,0:27:10.00,Default,,0000,0000,0000,,So let's do this. Dialogue: 0,0:27:10.00,0:27:12.00,Default,,0000,0000,0000,,Oops, it's not ints; Dialogue: 0,0:27:12.00,0:27:14.00,Default,,0000,0000,0000,,string bigger equals, Dialogue: 0,0:27:14.00,0:27:18.00,Default,,0000,0000,0000,,and I make a new string array that is num allocated times two, Dialogue: 0,0:27:18.00,0:27:22.00,Default,,0000,0000,0000,,so twice as big as the one we have. Dialogue: 0,0:27:22.00,0:27:30.00,Default,,0000,0000,0000,,And now I go through and I copy. I'm Dialogue: 0,0:27:30.00,0:27:34.00,Default,,0000,0000,0000,,copying from my old array to my bigger one, Dialogue: 0,0:27:34.00,0:27:37.00,Default,,0000,0000,0000,,all of num used. In this case, num used and num allocated are exactly the same thing, so. And Dialogue: 0,0:27:37.00,0:27:38.00,Default,,0000,0000,0000,,then I'm Dialogue: 0,0:27:38.00,0:27:43.00,Default,,0000,0000,0000,,gonna reset my fields as needed. I'm gonna delete my old array because I'm Dialogue: 0,0:27:43.00,0:27:45.00,Default,,0000,0000,0000,,now done with it. I'm gonna Dialogue: 0,0:27:45.00,0:27:49.00,Default,,0000,0000,0000,,set my pointer to this, so that's doing pointer assignment only. Right? So I deleted Dialogue: 0,0:27:49.00,0:27:52.00,Default,,0000,0000,0000,,the old space, I've copied everything I needed out of it, I'm Dialogue: 0,0:27:52.00,0:27:54.00,Default,,0000,0000,0000,,now resetting my pointer to the bigger one, Dialogue: 0,0:27:54.00,0:27:56.00,Default,,0000,0000,0000,,and then I'm setting my num allocated Dialogue: 0,0:27:56.00,0:28:00.00,Default,,0000,0000,0000,,to be Dialogue: 0,0:28:00.00,0:28:01.00,Default,,0000,0000,0000,,twice as big, Dialogue: 0,0:28:01.00,0:28:05.00,Default,,0000,0000,0000,,num used doesn't change. Okay, Dialogue: 0,0:28:05.00,0:28:06.00,Default,,0000,0000,0000,,so Dialogue: 0,0:28:06.00,0:28:09.00,Default,,0000,0000,0000,,went through the process of building all these things. And as noted, that is gonna Dialogue: 0,0:28:09.00,0:28:11.00,Default,,0000,0000,0000,,cost us something Dialogue: 0,0:28:11.00,0:28:15.00,Default,,0000,0000,0000,,linear in the number of elements that we currently have, so. Dialogue: 0,0:28:15.00,0:28:19.00,Default,,0000,0000,0000,,So this guy is intended to be a private method. Dialogue: 0,0:28:19.00,0:28:22.00,Default,,0000,0000,0000,,I don't want people outside of the vector being able to call this, so I'm actually Dialogue: 0,0:28:22.00,0:28:24.00,Default,,0000,0000,0000,,gonna list this Dialogue: 0,0:28:24.00,0:28:27.00,Default,,0000,0000,0000,,in the private section. It's not as Dialogue: 0,0:28:27.00,0:28:29.00,Default,,0000,0000,0000,,common that you'll see member functions listed down here. But in this case, it's Dialogue: 0,0:28:29.00,0:28:32.00,Default,,0000,0000,0000,,appropriate for something that you plan to use internally as a helper but you don't want Dialogue: 0,0:28:32.00,0:28:39.00,Default,,0000,0000,0000,,anybody outside just to be calling double capacity when they feel like it. Dialogue: 0,0:28:39.00,0:28:41.00,Default,,0000,0000,0000,,Question? So normally [inaudible] an array, you couldn't actually Dialogue: 0,0:28:41.00,0:28:45.00,Default,,0000,0000,0000,,declare a size like that, though, right, with a variable? Dialogue: 0,0:28:45.00,0:28:48.00,Default,,0000,0000,0000,,You know I don't understand the question. Try that again. You Dialogue: 0,0:28:48.00,0:28:52.00,Default,,0000,0000,0000,,know in this case to enlarge it, you use a Dialogue: 0,0:28:52.00,0:28:53.00,Default,,0000,0000,0000,,variable as the - for the Dialogue: 0,0:28:53.00,0:28:57.00,Default,,0000,0000,0000,,size of the array. When you normally declare an array, you couldn't do that, right? Well if Dialogue: 0,0:28:57.00,0:29:03.00,Default,,0000,0000,0000,,you did it - It has to be a constant. So like if you used this form of an array, you know, Dialogue: 0,0:29:03.00,0:29:05.00,Default,,0000,0000,0000,,where you declared it like this. Did you see what I just did? Dialogue: 0,0:29:05.00,0:29:05.00,Default,,0000,0000,0000,,Yeah. Yeah. Dialogue: 0,0:29:05.00,0:29:06.00,Default,,0000,0000,0000,,That way won't work, Dialogue: 0,0:29:06.00,0:29:10.00,Default,,0000,0000,0000,,right? That way is fixed size, nothing you can do about it. So I'm usually totally the Dialogue: 0,0:29:10.00,0:29:14.00,Default,,0000,0000,0000,,dynamic array at all times, so that everything - So you do it when you're declaring it on a heap? Dialogue: 0,0:29:14.00,0:29:15.00,Default,,0000,0000,0000,,Yes. Okay. Dialogue: 0,0:29:15.00,0:29:19.00,Default,,0000,0000,0000,,Yes, exactly. All I have in the - stored in the vector itself is a pointer to an Dialogue: 0,0:29:19.00,0:29:22.00,Default,,0000,0000,0000,,array elsewhere. And that array in the heap gives me the flexibility to make it as big as Dialogue: 0,0:29:22.00,0:29:26.00,Default,,0000,0000,0000,,I want, as small as I want, to change its size, to change where it Dialogue: 0,0:29:26.00,0:29:29.00,Default,,0000,0000,0000,,points to, you know all the - the dynamic arrays typically just give you a lot more flexibility Dialogue: 0,0:29:29.00,0:29:32.00,Default,,0000,0000,0000,,than a static array. That's really stack heap. It is. Array is a Dialogue: 0,0:29:32.00,0:29:35.00,Default,,0000,0000,0000,,stack heap thing. When you put on the stack, you had to say how big it is at compile time Dialogue: 0,0:29:35.00,0:29:38.00,Default,,0000,0000,0000,,and you can't change it. The heap let's you say, ''I need it bigger, I need it smaller, I need to Dialogue: 0,0:29:38.00,0:29:40.00,Default,,0000,0000,0000,,move it,'' Dialogue: 0,0:29:40.00,0:29:44.00,Default,,0000,0000,0000,,in a way that the stack just doesn't give you that at all. Dialogue: 0,0:29:44.00,0:29:47.00,Default,,0000,0000,0000,,So when I go back to myvector.cpp, the place I want to put in my Dialogue: 0,0:29:47.00,0:29:54.00,Default,,0000,0000,0000,,double capacity here is when num used is equal to num allocated, Dialogue: 0,0:29:54.00,0:29:58.00,Default,,0000,0000,0000,,but what I want to do is call double capacity there. Dialogue: 0,0:29:58.00,0:30:02.00,Default,,0000,0000,0000,,After I've done that, num allocated should've gone up by a factor of two. Dialogue: 0,0:30:02.00,0:30:04.00,Default,,0000,0000,0000,,Space will be there at that point. I know that num used is a Dialogue: 0,0:30:04.00,0:30:08.00,Default,,0000,0000,0000,,fine slot to now use to assign that next thing. So whenever we're out of space, we'll Dialogue: 0,0:30:08.00,0:30:10.00,Default,,0000,0000,0000,,make some more space. Dialogue: 0,0:30:10.00,0:30:13.00,Default,,0000,0000,0000,,And so I'm gonna - right now, I think my allocated is still set at two. That's a fine place. Dialogue: 0,0:30:13.00,0:30:16.00,Default,,0000,0000,0000,,I'd like it to be kind of small because I'd like to test kind of Dialogue: 0,0:30:16.00,0:30:18.00,Default,,0000,0000,0000,,the - Dialogue: 0,0:30:18.00,0:30:25.00,Default,,0000,0000,0000,,some of the initial allocations. So I'll go ahead and add a Dialogue: 0,0:30:25.00,0:30:28.00,Default,,0000,0000,0000,,couple more people so I can see that I know that - at that point, if I've gotten to Dialogue: 0,0:30:28.00,0:30:32.00,Default,,0000,0000,0000,,five, I'm gonna have to double once and then again to get there. Dialogue: 0,0:30:32.00,0:30:37.00,Default,,0000,0000,0000,,And let's, I'll take out my error case here, see that I've managed Dialogue: 0,0:30:37.00,0:30:38.00,Default,,0000,0000,0000,,to Dialogue: 0,0:30:38.00,0:30:40.00,Default,,0000,0000,0000,,allocate and move stuff around. Dialogue: 0,0:30:40.00,0:30:42.00,Default,,0000,0000,0000,,Got my five names back out Dialogue: 0,0:30:42.00,0:30:46.00,Default,,0000,0000,0000,,without running into anything crazy, so that makes me feel good about what I got Dialogue: 0,0:30:46.00,0:30:49.00,Default,,0000,0000,0000,,there. So Dialogue: 0,0:30:49.00,0:30:53.00,Default,,0000,0000,0000,,I could go on to show you what insert and remove Dialogue: 0,0:30:53.00,0:30:53.00,Default,,0000,0000,0000,,do; I think Dialogue: 0,0:30:53.00,0:30:57.00,Default,,0000,0000,0000,,I'm probably gonna skip that because I'd rather talk about the template thing. But I could Dialogue: 0,0:30:57.00,0:31:01.00,Default,,0000,0000,0000,,just tell you - I could sketch the [inaudible]: what does insert at do, what does remove at do? Dialogue: 0,0:31:01.00,0:31:04.00,Default,,0000,0000,0000,,Basically, that they're doing the string - the array shuffling for you. If you say Dialogue: 0,0:31:04.00,0:31:08.00,Default,,0000,0000,0000,,insert at some position, it has to move everything down by one and then Dialogue: 0,0:31:08.00,0:31:11.00,Default,,0000,0000,0000,,put in there. Whereas at is actually just tacking it onto the end of the existing Dialogue: 0,0:31:11.00,0:31:12.00,Default,,0000,0000,0000,,ones. Dialogue: 0,0:31:12.00,0:31:16.00,Default,,0000,0000,0000,,The insert and remove have to do the shuffle to either close up the space or open up a space. Dialogue: 0,0:31:16.00,0:31:21.00,Default,,0000,0000,0000,,They'll probably both need to look at the capacity as well. That the - if you're Dialogue: 0,0:31:21.00,0:31:25.00,Default,,0000,0000,0000,,inserting and you're already at capacity, you better double before you start. And then the Dialogue: 0,0:31:25.00,0:31:28.00,Default,,0000,0000,0000,,remove at, also may actually want to have a shrink capacity. Where when we realize Dialogue: 0,0:31:28.00,0:31:31.00,Default,,0000,0000,0000,,we previously were allocated much larger and we've gotten a lot smaller, should we Dialogue: 0,0:31:31.00,0:31:35.00,Default,,0000,0000,0000,,take away some of that space. A lot of times the implementations don't bother Dialogue: 0,0:31:35.00,0:31:38.00,Default,,0000,0000,0000,,with that case. They figure, ''Ah, it's already allocated, just keep it around. You Dialogue: 0,0:31:38.00,0:31:41.00,Default,,0000,0000,0000,,might need it again later.'' So it may be that actually we just leave it over-allocated, Dialogue: 0,0:31:41.00,0:31:44.00,Default,,0000,0000,0000,,even when we've deleted a lot of elements, but Dialogue: 0,0:31:44.00,0:31:47.00,Default,,0000,0000,0000,,if we were being tidy we could take an effort there. Dialogue: 0,0:31:47.00,0:31:50.00,Default,,0000,0000,0000,,What I want to do is switch it to a template. So if you have questions Dialogue: 0,0:31:50.00,0:31:53.00,Default,,0000,0000,0000,,about the code I have right here, now would be Dialogue: 0,0:31:53.00,0:31:55.00,Default,,0000,0000,0000,,a really good time to ask before I start Dialogue: 0,0:31:55.00,0:31:57.00,Default,,0000,0000,0000,,mucking it up. Way in the back? [Inaudible]. Dialogue: 0,0:31:57.00,0:32:00.00,Default,,0000,0000,0000,,I will. You know that's a really good idea because if I - at this point, I'll Dialogue: 0,0:32:00.00,0:32:03.00,Default,,0000,0000,0000,,start to change it and then it's gonna Dialogue: 0,0:32:03.00,0:32:06.00,Default,,0000,0000,0000,,be something else before we're all done. So let me Dialogue: 0,0:32:06.00,0:32:09.00,Default,,0000,0000,0000,,take a snapshot of what we have here Dialogue: 0,0:32:09.00,0:32:12.00,Default,,0000,0000,0000,,so that I - before I destroy it. Dialogue: 0,0:32:12.00,0:32:14.00,Default,,0000,0000,0000,,Question? When does the deconstructor Dialogue: 0,0:32:14.00,0:32:16.00,Default,,0000,0000,0000,,get Dialogue: 0,0:32:16.00,0:32:19.00,Default,,0000,0000,0000,,called? Okay, so the destructor gets called Dialogue: 0,0:32:19.00,0:32:23.00,Default,,0000,0000,0000,,in the most - there are two cases it gets called. One is when the Dialogue: 0,0:32:23.00,0:32:25.00,Default,,0000,0000,0000,,- so the constructor gets called when you declare Dialogue: 0,0:32:25.00,0:32:29.00,Default,,0000,0000,0000,,it, and then destructor gets called when it goes out of scope. So at the opening brace Dialogue: 0,0:32:29.00,0:32:32.00,Default,,0000,0000,0000,,of the block where you declared it in, is when the constructor's happening, Dialogue: 0,0:32:32.00,0:32:35.00,Default,,0000,0000,0000,,and then when you leave that. So in the case of this program, it's at the end of Dialogue: 0,0:32:35.00,0:32:36.00,Default,,0000,0000,0000,,May, Dialogue: 0,0:32:36.00,0:32:38.00,Default,,0000,0000,0000,,but and if it were in some called function or in the body of a for loop or Dialogue: 0,0:32:38.00,0:32:42.00,Default,,0000,0000,0000,,something, it would get called when you enter the loop and then called as - Dialogue: 0,0:32:42.00,0:32:43.00,Default,,0000,0000,0000,,destroyed as it left. Dialogue: 0,0:32:43.00,0:32:47.00,Default,,0000,0000,0000,,For things that were allocated out of the heap, so if I had a myvector new myvector, it Dialogue: 0,0:32:47.00,0:32:50.00,Default,,0000,0000,0000,,would explicitly when I called delete that thing when I was done with it. Dialogue: 0,0:32:50.00,0:32:55.00,Default,,0000,0000,0000,,So especially when the variable that holds it is going away, Dialogue: 0,0:32:55.00,0:32:58.00,Default,,0000,0000,0000,,right? Either because you're deleting it out of the heap or because it's going out of scope on the Dialogue: 0,0:32:58.00,0:33:00.00,Default,,0000,0000,0000,,stack. Here? Dialogue: 0,0:33:00.00,0:33:04.00,Default,,0000,0000,0000,,When you have string star array, arr, it's Dialogue: 0,0:33:04.00,0:33:07.00,Default,,0000,0000,0000,,a pointer to a single string and then later you can use a new statement Dialogue: 0,0:33:07.00,0:33:08.00,Default,,0000,0000,0000,,to Dialogue: 0,0:33:08.00,0:33:09.00,Default,,0000,0000,0000,,link it to - Yeah. - Dialogue: 0,0:33:09.00,0:33:13.00,Default,,0000,0000,0000,,an array. So how does it know when it - How does it know? It doesn't, is the truth, is that Dialogue: 0,0:33:13.00,0:33:16.00,Default,,0000,0000,0000,,when you say something's a pointer to a string, Dialogue: 0,0:33:16.00,0:33:20.00,Default,,0000,0000,0000,,the only guarantee is really there and says it's an address of someplace where a Dialogue: 0,0:33:20.00,0:33:23.00,Default,,0000,0000,0000,,string lives in memory. Whether there's one string there or a whole sequence of strings Dialogue: 0,0:33:23.00,0:33:27.00,Default,,0000,0000,0000,,is something you decide and you maintain, and so the compiler doesn't distinguish Dialogue: 0,0:33:27.00,0:33:28.00,Default,,0000,0000,0000,,those two cases. Dialogue: 0,0:33:28.00,0:33:30.00,Default,,0000,0000,0000,,It does not know that you made Dialogue: 0,0:33:30.00,0:33:32.00,Default,,0000,0000,0000,,exactly 1 string at the other end of this or 500. Dialogue: 0,0:33:32.00,0:33:36.00,Default,,0000,0000,0000,,And so, that's why this idea of tracking the num used and num allocated becomes our Dialogue: 0,0:33:36.00,0:33:39.00,Default,,0000,0000,0000,,job; that they look exactly the same in terms of how it interprets them. It says, ''Well it's the Dialogue: 0,0:33:39.00,0:33:43.00,Default,,0000,0000,0000,,address of where a string lives in memory, or maybe it's a sequence. I don't know.'' And Dialogue: 0,0:33:43.00,0:33:47.00,Default,,0000,0000,0000,,so it lets you use the brackets and the new and stuff on a pointer without Dialogue: 0,0:33:47.00,0:33:51.00,Default,,0000,0000,0000,,distinguishing - having any mechanism in the language to say this is a single Dialogue: 0,0:33:51.00,0:33:53.00,Default,,0000,0000,0000,,pointer and this is an array pointer. They look the same. So Dialogue: 0,0:33:53.00,0:33:55.00,Default,,0000,0000,0000,,you could use the bracket notion on a single pointer? Dialogue: 0,0:33:55.00,0:34:00.00,Default,,0000,0000,0000,,You certainly can. And then - It's not a good idea but you can do it. So Dialogue: 0,0:34:00.00,0:34:03.00,Default,,0000,0000,0000,,legally in the language, it just makes no distinction between those. Dialogue: 0,0:34:03.00,0:34:06.00,Default,,0000,0000,0000,,They are really commingled in way that - and so that's one of the more surprising features of C Dialogue: 0,0:34:06.00,0:34:08.00,Default,,0000,0000,0000,,and C++ and one's that a little bit hard to get your head around is Dialogue: 0,0:34:08.00,0:34:12.00,Default,,0000,0000,0000,,it doesn't track that it's a pointer to a single thing versus a pointer to an array. Dialogue: 0,0:34:12.00,0:34:14.00,Default,,0000,0000,0000,,That they're both just Dialogue: 0,0:34:14.00,0:34:15.00,Default,,0000,0000,0000,,pointers and it's mine. Dialogue: 0,0:34:15.00,0:34:18.00,Default,,0000,0000,0000,,And that allows opportunity for errors, when you mistreat them as the other type Dialogue: 0,0:34:18.00,0:34:21.00,Default,,0000,0000,0000,,for example. Question? Can you go Dialogue: 0,0:34:21.00,0:34:25.00,Default,,0000,0000,0000,,over in your secret view file in the [inaudible] Dialogue: 0,0:34:25.00,0:34:27.00,Default,,0000,0000,0000,,of what's going on Dialogue: 0,0:34:27.00,0:34:32.00,Default,,0000,0000,0000,,with [inaudible] real quick because it's just - Yeah, yeah, yeah, so let me - it was basically the thing I drew over here, but I'll do it again just to watch Dialogue: 0,0:34:32.00,0:34:33.00,Default,,0000,0000,0000,,it, Dialogue: 0,0:34:33.00,0:34:35.00,Default,,0000,0000,0000,,is that let's start - let's imagine I have a slightly smaller Dialogue: 0,0:34:35.00,0:34:37.00,Default,,0000,0000,0000,,num allocated so it's a little Dialogue: 0,0:34:37.00,0:34:38.00,Default,,0000,0000,0000,,less Dialogue: 0,0:34:38.00,0:34:42.00,Default,,0000,0000,0000,,for me to write. So let's say that I'm gonna use a num allocated of two, so this allocates Dialogue: 0,0:34:42.00,0:34:43.00,Default,,0000,0000,0000,,two. So Dialogue: 0,0:34:43.00,0:34:45.00,Default,,0000,0000,0000,,when I construct it, it makes a Dialogue: 0,0:34:45.00,0:34:48.00,Default,,0000,0000,0000,,block that holds two things and num used is Dialogue: 0,0:34:48.00,0:34:51.00,Default,,0000,0000,0000,,zero. So I do two adds: I add A, it Dialogue: 0,0:34:51.00,0:34:53.00,Default,,0000,0000,0000,,increments num used to one; Dialogue: 0,0:34:53.00,0:34:55.00,Default,,0000,0000,0000,,I had B, it increments num used to two. Dialogue: 0,0:34:55.00,0:34:57.00,Default,,0000,0000,0000,,I try to add C. Dialogue: 0,0:34:57.00,0:34:59.00,Default,,0000,0000,0000,,It says, ''Oh, well num used equals num allocated. Dialogue: 0,0:34:59.00,0:35:01.00,Default,,0000,0000,0000,,We're gonna go to double capacity now.'' Dialogue: 0,0:35:01.00,0:35:07.00,Default,,0000,0000,0000,,So double capacity has this little local variable called bigger. Dialogue: 0,0:35:07.00,0:35:08.00,Default,,0000,0000,0000,,And it says bigger Dialogue: 0,0:35:08.00,0:35:10.00,Default,,0000,0000,0000,,is gonna be something that is Dialogue: 0,0:35:10.00,0:35:13.00,Default,,0000,0000,0000,,four strings worth in an array, so it Dialogue: 0,0:35:13.00,0:35:14.00,Default,,0000,0000,0000,,gets four out there. Dialogue: 0,0:35:14.00,0:35:15.00,Default,,0000,0000,0000,,It does a full loop Dialogue: 0,0:35:15.00,0:35:19.00,Default,,0000,0000,0000,,to copy the contents of the old array on top of the initial part of this array; so it Dialogue: 0,0:35:19.00,0:35:22.00,Default,,0000,0000,0000,,copies over the A and the B, Dialogue: 0,0:35:22.00,0:35:23.00,Default,,0000,0000,0000,,into there. Dialogue: 0,0:35:23.00,0:35:27.00,Default,,0000,0000,0000,,And then it goes, ''Okay, I'm done with this old part. So let me go ahead Dialogue: 0,0:35:27.00,0:35:30.00,Default,,0000,0000,0000,,and delete that.'' And then it resets the arr to Dialogue: 0,0:35:30.00,0:35:34.00,Default,,0000,0000,0000,,point to this new one down here, where bigger was. So now, we got to aliases of the Dialogue: 0,0:35:34.00,0:35:35.00,Default,,0000,0000,0000,,same location. Dialogue: 0,0:35:35.00,0:35:38.00,Default,,0000,0000,0000,,And then it sets my num allocated to say and now Dialogue: 0,0:35:38.00,0:35:39.00,Default,,0000,0000,0000,,what you've got there Dialogue: 0,0:35:39.00,0:35:41.00,Default,,0000,0000,0000,,is something that holds four slots. Dialogue: 0,0:35:41.00,0:35:45.00,Default,,0000,0000,0000,,And then that used call here says, ''Okay and now writer the C into Dialogue: 0,0:35:45.00,0:35:48.00,Default,,0000,0000,0000,,the slot at three.'' Dialogue: 0,0:35:48.00,0:35:51.00,Default,,0000,0000,0000,,So the process here is the only way to enlarge an array in C++ is to make Dialogue: 0,0:35:51.00,0:35:54.00,Default,,0000,0000,0000,,a bigger one, copy what you had, Dialogue: 0,0:35:54.00,0:35:57.00,Default,,0000,0000,0000,,and then by virtue of you having made a bigger array to start with, you have some more slack that Dialogue: 0,0:35:57.00,0:35:59.00,Default,,0000,0000,0000,,you didn't have before. Dialogue: 0,0:35:59.00,0:36:02.00,Default,,0000,0000,0000,,Daniel? How does it delete arr with a star? Dialogue: 0,0:36:02.00,0:36:05.00,Default,,0000,0000,0000,,You know it has to do with just delete takes a pointer. Dialogue: 0,0:36:05.00,0:36:09.00,Default,,0000,0000,0000,,It does it - so a star arr is a string, arr is a pointer to a string. So Dialogue: 0,0:36:09.00,0:36:13.00,Default,,0000,0000,0000,,both forms of delete, delete and delete bracket - So conceptually - - a pointer. - there is a start Dialogue: 0,0:36:13.00,0:36:16.00,Default,,0000,0000,0000,,there because it's delete - Well effectively, yeah. It's delete the thing at the other end of the pointer, really. Dialogue: 0,0:36:16.00,0:36:19.00,Default,,0000,0000,0000,,But it's funny. Delete says Dialogue: 0,0:36:19.00,0:36:21.00,Default,,0000,0000,0000,,take this address and reclaim its contents. Dialogue: 0,0:36:21.00,0:36:25.00,Default,,0000,0000,0000,,And so it doesn't really operate on a string, per se, it operates on the storage Dialogue: 0,0:36:25.00,0:36:29.00,Default,,0000,0000,0000,,where that string is. And so I don't know whether you want to call that is there an implicit star there or Dialogue: 0,0:36:29.00,0:36:33.00,Default,,0000,0000,0000,,not, it really is about the pointer though rather than the contents. Dialogue: 0,0:36:33.00,0:36:38.00,Default,,0000,0000,0000,,So saying that address has some memory associated with it, reclaim that memory. If I could raise - Dialogue: 0,0:36:38.00,0:36:39.00,Default,,0000,0000,0000,,Uh-huh. Dialogue: 0,0:36:39.00,0:36:41.00,Default,,0000,0000,0000,,So when you're Dialogue: 0,0:36:41.00,0:36:45.00,Default,,0000,0000,0000,,first declaring or when you're making a pointer like string bigger, string star bigger, Dialogue: 0,0:36:45.00,0:36:47.00,Default,,0000,0000,0000,,you have to declare it with Dialogue: 0,0:36:47.00,0:36:50.00,Default,,0000,0000,0000,,the star notion. But then later on, you don't ever have to use Dialogue: 0,0:36:50.00,0:36:53.00,Default,,0000,0000,0000,,that again? You pretty much won't see that star used again. Dialogue: 0,0:36:53.00,0:36:56.00,Default,,0000,0000,0000,,Right? It's interesting that things like bigger sub I and erase sub I Dialogue: 0,0:36:56.00,0:37:00.00,Default,,0000,0000,0000,,implicitly have a D reference in them. And that can be misleading. You Dialogue: 0,0:37:00.00,0:37:02.00,Default,,0000,0000,0000,,think, ''Well how come I'm never actually using that star again on that thing to Dialogue: 0,0:37:02.00,0:37:04.00,Default,,0000,0000,0000,,get back to the strings that are out there?'' Dialogue: 0,0:37:04.00,0:37:08.00,Default,,0000,0000,0000,,And it has to do with the fact that the bracket notation kind of implicitly D Dialogue: 0,0:37:08.00,0:37:09.00,Default,,0000,0000,0000,,references in it. Dialogue: 0,0:37:09.00,0:37:13.00,Default,,0000,0000,0000,,If I did a star bigger, it would actually have the effect of giving me bigger Dialogue: 0,0:37:13.00,0:37:16.00,Default,,0000,0000,0000,,sub zero, it turns out. You can use that notation but it's Dialogue: 0,0:37:16.00,0:37:18.00,Default,,0000,0000,0000,,not that common to need to. And Dialogue: 0,0:37:18.00,0:37:19.00,Default,,0000,0000,0000,,so down on Dialogue: 0,0:37:19.00,0:37:20.00,Default,,0000,0000,0000,,the last, Dialogue: 0,0:37:20.00,0:37:23.00,Default,,0000,0000,0000,,one line up Dialogue: 0,0:37:23.00,0:37:24.00,Default,,0000,0000,0000,,from the bottom, it says array equals bigger. You don't have to - Yeah, Dialogue: 0,0:37:24.00,0:37:27.00,Default,,0000,0000,0000,,if you did that, if I did say - If you Dialogue: 0,0:37:27.00,0:37:33.00,Default,,0000,0000,0000,,said array - Star arr equals star bigger, Dialogue: 0,0:37:33.00,0:37:35.00,Default,,0000,0000,0000,,I would not be getting what I want. Dialogue: 0,0:37:35.00,0:37:37.00,Default,,0000,0000,0000,,Right? What it would be doing is it would say Dialogue: 0,0:37:37.00,0:37:40.00,Default,,0000,0000,0000,,follow bigger and see what's at the other end, so that would follow bigger and Dialogue: 0,0:37:40.00,0:37:44.00,Default,,0000,0000,0000,,get that string A. And then it would say follow ARR and overwrite it Dialogue: 0,0:37:44.00,0:37:45.00,Default,,0000,0000,0000,,with that A, Dialogue: 0,0:37:45.00,0:37:49.00,Default,,0000,0000,0000,,so it would actually have the effect of only copying the first string Dialogue: 0,0:37:49.00,0:37:51.00,Default,,0000,0000,0000,,from bigger on top of the first string of array. But array would still point to Dialogue: 0,0:37:51.00,0:37:54.00,Default,,0000,0000,0000,,where it was, bigger would still point to where it was, and they would - we Dialogue: 0,0:37:54.00,0:37:56.00,Default,,0000,0000,0000,,would've not have updated our, Dialogue: 0,0:37:56.00,0:38:00.00,Default,,0000,0000,0000,,the pointer we really wanted to point to the new array. So Dialogue: 0,0:38:00.00,0:38:02.00,Default,,0000,0000,0000,,there is a difference. Without the star, Dialogue: 0,0:38:02.00,0:38:05.00,Default,,0000,0000,0000,,we're talking about the changing the pointers; with the star, we're Dialogue: 0,0:38:05.00,0:38:08.00,Default,,0000,0000,0000,,talking about the strings at the other end. And so we're - this is a string Dialogue: 0,0:38:08.00,0:38:10.00,Default,,0000,0000,0000,,assignment. It says assign one string to another. Dialogue: 0,0:38:10.00,0:38:12.00,Default,,0000,0000,0000,,Without Dialogue: 0,0:38:12.00,0:38:15.00,Default,,0000,0000,0000,,the star on it, it's like assign one pointer to another; Dialogue: 0,0:38:15.00,0:38:18.00,Default,,0000,0000,0000,,make two pointers point to the same place. When you're done with this, Dialogue: 0,0:38:18.00,0:38:23.00,Default,,0000,0000,0000,,bigger and arr will be aliases for the same location. Dialogue: 0,0:38:23.00,0:38:25.00,Default,,0000,0000,0000,,That's a very important question though to get kind of Dialogue: 0,0:38:25.00,0:38:30.00,Default,,0000,0000,0000,,what that star's doing for you. Here? After Dialogue: 0,0:38:30.00,0:38:34.00,Default,,0000,0000,0000,,arr is bigger, can Dialogue: 0,0:38:34.00,0:38:35.00,Default,,0000,0000,0000,,you delete bigger after that? If I deleted Dialogue: 0,0:38:35.00,0:38:39.00,Default,,0000,0000,0000,,bigger, at that point arr is pointing to the same place. And so remember that Dialogue: 0,0:38:39.00,0:38:42.00,Default,,0000,0000,0000,,having two or three or ten pointers all at the same place, if you delete one of them, Dialogue: 0,0:38:42.00,0:38:46.00,Default,,0000,0000,0000,,they actually effectively are deleted. The delete really deletes the Dialogue: 0,0:38:46.00,0:38:49.00,Default,,0000,0000,0000,,storage out here. And then if I did that, it would cause arr to then be pointing to this Dialogue: 0,0:38:49.00,0:38:51.00,Default,,0000,0000,0000,,piece of memory, Dialogue: 0,0:38:51.00,0:38:54.00,Default,,0000,0000,0000,,and not a good scene will come from that. It means that when it later Dialogue: 0,0:38:54.00,0:38:57.00,Default,,0000,0000,0000,,goes back in there and starts trying to read and write to that contents at Dialogue: 0,0:38:57.00,0:39:00.00,Default,,0000,0000,0000,,any moment it could kind of shift underneath you. You don't own it any Dialogue: 0,0:39:00.00,0:39:02.00,Default,,0000,0000,0000,,more; it's not reserved for your use. Dialogue: 0,0:39:02.00,0:39:05.00,Default,,0000,0000,0000,,So if we did that, we'd get ourselves into trouble. All Dialogue: 0,0:39:05.00,0:39:08.00,Default,,0000,0000,0000,,right? So there should basically be a one-to-one correspondence between Dialogue: 0,0:39:08.00,0:39:10.00,Default,,0000,0000,0000,,things you new and things you Dialogue: 0,0:39:10.00,0:39:11.00,Default,,0000,0000,0000,,delete. And so Dialogue: 0,0:39:11.00,0:39:14.00,Default,,0000,0000,0000,,in the myvector case, we newed something in the constructor that we're gonna delete in the Dialogue: 0,0:39:14.00,0:39:18.00,Default,,0000,0000,0000,,destructor. If at some point along the way we got rid of our old one and get Dialogue: 0,0:39:18.00,0:39:21.00,Default,,0000,0000,0000,,a new one, that's the new the one that's gonna get deleted. If we deleted it Dialogue: 0,0:39:21.00,0:39:23.00,Default,,0000,0000,0000,,midstream here, we would just be asking for Dialogue: 0,0:39:23.00,0:39:29.00,Default,,0000,0000,0000,,havoc when we start accessing that deleted memory. Way in the back? Is Dialogue: 0,0:39:29.00,0:39:31.00,Default,,0000,0000,0000,,it possible to create a pointer Dialogue: 0,0:39:31.00,0:39:33.00,Default,,0000,0000,0000,,to something - Dialogue: 0,0:39:33.00,0:39:37.00,Default,,0000,0000,0000,,a pointer to the address that's one off the end of the original Dialogue: 0,0:39:37.00,0:39:42.00,Default,,0000,0000,0000,,array and then just create an array just off the end there? Not really. So new doesn't let Dialogue: 0,0:39:42.00,0:39:43.00,Default,,0000,0000,0000,,you decide where you want something. Dialogue: 0,0:39:43.00,0:39:45.00,Default,,0000,0000,0000,,So you're point being to think, Dialogue: 0,0:39:45.00,0:39:48.00,Default,,0000,0000,0000,,''Well I can tell you what this address is, can I just make space right there Dialogue: 0,0:39:48.00,0:39:53.00,Default,,0000,0000,0000,,and then I won't have to copy.'' And it turns out new just doesn't give you that kind of control. You ask it for space, it Dialogue: 0,0:39:53.00,0:39:55.00,Default,,0000,0000,0000,,finds it wherever it has and you can't - Dialogue: 0,0:39:55.00,0:39:58.00,Default,,0000,0000,0000,,there isn't even a mechanism where you could suggest where you'd like it to be. You could say, ''Well Dialogue: 0,0:39:58.00,0:40:01.00,Default,,0000,0000,0000,,let that place right there would be really handy. Could you please give me that one?'' It Dialogue: 0,0:40:01.00,0:40:02.00,Default,,0000,0000,0000,,just doesn't give it you. Dialogue: 0,0:40:02.00,0:40:03.00,Default,,0000,0000,0000,,So Dialogue: 0,0:40:03.00,0:40:05.00,Default,,0000,0000,0000,,you're - this is the way Dialogue: 0,0:40:05.00,0:40:08.00,Default,,0000,0000,0000,,that typically you have to manage a dynamic array. And this is actually one of the big Dialogue: 0,0:40:08.00,0:40:10.00,Default,,0000,0000,0000,,drawbacks Dialogue: 0,0:40:10.00,0:40:13.00,Default,,0000,0000,0000,,to continuous memory as a reason for implementing things is that the Dialogue: 0,0:40:13.00,0:40:16.00,Default,,0000,0000,0000,,fact that it has to maintain contiguousness. Means you have to shuffle and move Dialogue: 0,0:40:16.00,0:40:18.00,Default,,0000,0000,0000,,and copy Dialogue: 0,0:40:18.00,0:40:19.00,Default,,0000,0000,0000,,this block Dialogue: 0,0:40:19.00,0:40:20.00,Default,,0000,0000,0000,, Dialogue: 0,0:40:20.00,0:40:23.00,Default,,0000,0000,0000,,without the flexibility of something like a link list where every cell is independently Dialogue: 0,0:40:23.00,0:40:27.00,Default,,0000,0000,0000,,manipulated. Dialogue: 0,0:40:27.00,0:40:29.00,Default,,0000,0000,0000,,There? Why does [inaudible] the delete Dialogue: 0,0:40:29.00,0:40:34.00,Default,,0000,0000,0000,,brackets arr as delete just arr? So the difference is that if you allocated Dialogue: 0,0:40:34.00,0:40:38.00,Default,,0000,0000,0000,,something with new string bracket, new something bracket, you need a delete bracket. Dialogue: 0,0:40:38.00,0:40:41.00,Default,,0000,0000,0000,,If you actually use delete without the brackets, it thinks there's a single Dialogue: 0,0:40:41.00,0:40:42.00,Default,,0000,0000,0000,,pointer and there's only one string at the other end. Dialogue: 0,0:40:42.00,0:40:47.00,Default,,0000,0000,0000,,Where delete brackets says delete the whole gob of strings that are there. Dialogue: 0,0:40:47.00,0:40:49.00,Default,,0000,0000,0000,,If you don't do it, it's not - the consequence is not that big; Dialogue: 0,0:40:49.00,0:40:52.00,Default,,0000,0000,0000,,it's like some memory gets orphaned, some things don't happen. But Dialogue: 0,0:40:52.00,0:40:56.00,Default,,0000,0000,0000,,to be totally correct, they go hand in hand: if you use new with Dialogue: 0,0:40:56.00,0:40:59.00,Default,,0000,0000,0000,,brackets, use delete with no brackets, if you use new with brackets, use delete with brackets. Dialogue: 0,0:40:59.00,0:41:02.00,Default,,0000,0000,0000,,So it's either both with brackets or both without. Dialogue: 0,0:41:02.00,0:41:04.00,Default,,0000,0000,0000,,So even though arr is just point Dialogue: 0,0:41:04.00,0:41:07.00,Default,,0000,0000,0000,,to the first place, the Dialogue: 0,0:41:07.00,0:41:08.00,Default,,0000,0000,0000,,brackets knows the - Dialogue: 0,0:41:08.00,0:41:12.00,Default,,0000,0000,0000,,Yeah, it does. And so that kind of makes me feel like I'm a lair because I said well the array Dialogue: 0,0:41:12.00,0:41:15.00,Default,,0000,0000,0000,,doesn't know its length. Well it does somehow. Internally it is Dialogue: 0,0:41:15.00,0:41:18.00,Default,,0000,0000,0000,,maintaining some housekeeping but it doesn't expose it to you. Dialogue: 0,0:41:18.00,0:41:21.00,Default,,0000,0000,0000,,So when you say delete bracket arr it knows, ''Oh, there's a bunch of strings and I got Dialogue: 0,0:41:21.00,0:41:25.00,Default,,0000,0000,0000,,to do a bunch of cleanup on them.'' But it doesn't ever expose that information back Dialogue: 0,0:41:25.00,0:41:29.00,Default,,0000,0000,0000,,to you. It doesn't let you depend on it, so it's up to you to maintain that information redundantly Dialogue: 0,0:41:29.00,0:41:34.00,Default,,0000,0000,0000,,with it. All Dialogue: 0,0:41:34.00,0:41:36.00,Default,,0000,0000,0000,,right, let me see if I can make it a template. Dialogue: 0,0:41:36.00,0:41:39.00,Default,,0000,0000,0000,,I probably can't do this actually fast enough to get it all done today, but we can at least get started Dialogue: 0,0:41:39.00,0:41:41.00,Default,,0000,0000,0000,,on it. Dialogue: 0,0:41:41.00,0:41:42.00,Default,,0000,0000,0000,,So then, I Dialogue: 0,0:41:42.00,0:41:46.00,Default,,0000,0000,0000,,introduce a template header and Dialogue: 0,0:41:46.00,0:41:49.00,Default,,0000,0000,0000,,I make up the name that I want here, so Dialogue: 0,0:41:49.00,0:41:50.00,Default,,0000,0000,0000,,same class header now Dialogue: 0,0:41:50.00,0:41:52.00,Default,,0000,0000,0000,,other than typing elem type. Then Dialogue: 0,0:41:52.00,0:41:55.00,Default,,0000,0000,0000,,I look through my interface and I see places where I previously had said it's Dialogue: 0,0:41:55.00,0:41:59.00,Default,,0000,0000,0000,,strings, it's strings, it's storing strings. And I say it's not actually storing strings; Dialogue: 0,0:41:59.00,0:42:00.00,Default,,0000,0000,0000,,it's gonna store Dialogue: 0,0:42:00.00,0:42:02.00,Default,,0000,0000,0000,,elem type things, Dialogue: 0,0:42:02.00,0:42:06.00,Default,,0000,0000,0000,,it's gonna return elem type things Dialogue: 0,0:42:06.00,0:42:09.00,Default,,0000,0000,0000,,and it's going to have an array of Dialogue: 0,0:42:09.00,0:42:11.00,Default,,0000,0000,0000,,elem type things. Dialogue: 0,0:42:11.00,0:42:13.00,Default,,0000,0000,0000,,So I think that's everything that happened Dialogue: 0,0:42:13.00,0:42:17.00,Default,,0000,0000,0000,,to the interface. Let me see if I see any other places that I - so the Dialogue: 0,0:42:17.00,0:42:18.00,Default,,0000,0000,0000,,interface part Dialogue: 0,0:42:18.00,0:42:20.00,Default,,0000,0000,0000,,is kind of small. Dialogue: 0,0:42:20.00,0:42:23.00,Default,,0000,0000,0000,,There's one other change I'm gonna have to make to it but I'm gonna come back to it. I'm gonna look Dialogue: 0,0:42:23.00,0:42:25.00,Default,,0000,0000,0000,,at the code at the other side for a second. And I say, ''Okay, well Dialogue: 0,0:42:25.00,0:42:28.00,Default,,0000,0000,0000,,that wasn't so bad.'' Now Dialogue: 0,0:42:28.00,0:42:31.00,Default,,0000,0000,0000,,it turns out that it gets a little bit goopier over here Dialogue: 0,0:42:31.00,0:42:33.00,Default,,0000,0000,0000,,because that template type name Dialogue: 0,0:42:33.00,0:42:38.00,Default,,0000,0000,0000,,has to go on every one of these: Dialogue: 0,0:42:38.00,0:42:40.00,Default,,0000,0000,0000,,introduce them to the template type and elem type. Dialogue: 0,0:42:40.00,0:42:44.00,Default,,0000,0000,0000,,And now there's another place where it needs to show up. Dialogue: 0,0:42:44.00,0:42:49.00,Default,,0000,0000,0000,,So the full syntax for this is now saying this is a template function, Dialogue: 0,0:42:49.00,0:42:50.00,Default,,0000,0000,0000,,depending on elem type, Dialogue: 0,0:42:50.00,0:42:52.00,Default,,0000,0000,0000,,and it's actually for the myvector Dialogue: 0,0:42:52.00,0:42:54.00,Default,,0000,0000,0000,,who is being - Dialogue: 0,0:42:54.00,0:42:58.00,Default,,0000,0000,0000,,we are writing the myvector constructor for something whose name is myvector Dialogue: 0,0:42:58.00,0:43:00.00,Default,,0000,0000,0000,,angle bracket elem type. Dialogue: 0,0:43:00.00,0:43:02.00,Default,,0000,0000,0000,,So there's gonna be a lot of this goo. Every one of these is kinda change Dialogue: 0,0:43:02.00,0:43:05.00,Default,,0000,0000,0000,,its form, from just looking like the ordinary myvector class scope doesn't Dialogue: 0,0:43:05.00,0:43:06.00,Default,,0000,0000,0000,,really exist any more. Dialogue: 0,0:43:06.00,0:43:10.00,Default,,0000,0000,0000,,Myvector is now a template for which there's a lot of different class scopes, Dialogue: 0,0:43:10.00,0:43:13.00,Default,,0000,0000,0000,,one for each kind of thing being stored. So myvector int is different than myvector Dialogue: 0,0:43:13.00,0:43:14.00,Default,,0000,0000,0000,,string. Dialogue: 0,0:43:14.00,0:43:15.00,Default,,0000,0000,0000,,So we say, ''Well, Dialogue: 0,0:43:15.00,0:43:18.00,Default,,0000,0000,0000,,if you were building the myvector constructor for myvector string, it Dialogue: 0,0:43:18.00,0:43:21.00,Default,,0000,0000,0000,,looks like this.'' Or you know Dialogue: 0,0:43:21.00,0:43:24.00,Default,,0000,0000,0000,,having filled an elem type with those strings. So Dialogue: 0,0:43:24.00,0:43:30.00,Default,,0000,0000,0000,,everywhere I was using string, I got to change to elem type in the body as well. And Dialogue: 0,0:43:30.00,0:43:32.00,Default,,0000,0000,0000,,then I kind of take this guy Dialogue: 0,0:43:32.00,0:43:38.00,Default,,0000,0000,0000,,and use it in a bunch of places. I'm gonna use it here Dialogue: 0,0:43:38.00,0:43:39.00,Default,,0000,0000,0000,,and then I'm gonna have to do it down Dialogue: 0,0:43:39.00,0:43:50.00,Default,,0000,0000,0000,,here, on that side, do it Dialogue: 0,0:43:50.00,0:43:54.00,Default,,0000,0000,0000,,here, and it's gonna return something of elem type, Dialogue: 0,0:43:54.00,0:43:56.00,Default,,0000,0000,0000,,here. It's a little bit of Dialogue: 0,0:43:56.00,0:43:59.00,Default,,0000,0000,0000,,a mess to do this, and the code definitely gets a little bit goopier as a result Dialogue: 0,0:43:59.00,0:44:00.00,Default,,0000,0000,0000,,of this. It doesn't Dialogue: 0,0:44:00.00,0:44:08.00,Default,,0000,0000,0000,,look quite as pretty as it did when it wasn't a template, Dialogue: 0,0:44:08.00,0:44:11.00,Default,,0000,0000,0000,,but it becomes a lot more useful. Okay. Dialogue: 0,0:44:11.00,0:44:14.00,Default,,0000,0000,0000,,Then I need to look for places that I used a string. Dialogue: 0,0:44:14.00,0:44:17.00,Default,,0000,0000,0000,,And every place where I was using string, assuming that's what I was storing, it Dialogue: 0,0:44:17.00,0:44:20.00,Default,,0000,0000,0000,,now actually turns into elem type. So Dialogue: 0,0:44:20.00,0:44:23.00,Default,,0000,0000,0000,,my pointers and the kind of array I'm allocating is actually now made into elem Dialogue: 0,0:44:23.00,0:44:24.00,Default,,0000,0000,0000,,type. Dialogue: 0,0:44:24.00,0:44:27.00,Default,,0000,0000,0000,,The rest of the code actually didn't say anything specific about what's its doing, just copying Dialogue: 0,0:44:27.00,0:44:30.00,Default,,0000,0000,0000,,things from one array to another. And now, depending on what the arrays are, it's Dialogue: 0,0:44:30.00,0:44:32.00,Default,,0000,0000,0000,,copying ints or strings or doubles. Dialogue: 0,0:44:32.00,0:44:36.00,Default,,0000,0000,0000,,And then other places in the interface where I'm doing add or I'm going get at, I have to be Dialogue: 0,0:44:36.00,0:44:40.00,Default,,0000,0000,0000,,describing the things that are coming in and out as elem type so that they can be Dialogue: 0,0:44:40.00,0:44:42.00,Default,,0000,0000,0000,,matched to whatever the client's using. I Dialogue: 0,0:44:42.00,0:44:45.00,Default,,0000,0000,0000,,think the rest of it looks okay. Why do you have to write Dialogue: 0,0:44:45.00,0:44:47.00,Default,,0000,0000,0000,,template type name, and elem type above Dialogue: 0,0:44:47.00,0:44:50.00,Default,,0000,0000,0000,,every - Because you just have to, because it's C++. Because the thing is, Dialogue: 0,0:44:50.00,0:44:53.00,Default,,0000,0000,0000,,that piece of code is, itself, a template, so Dialogue: 0,0:44:53.00,0:44:56.00,Default,,0000,0000,0000,,these are like little mini-templates. So that I had the interface, which said here's the Dialogue: 0,0:44:56.00,0:44:58.00,Default,,0000,0000,0000,,template pattern for the Dialogue: 0,0:44:58.00,0:45:02.00,Default,,0000,0000,0000,,interface, and each of these says when you're ready to make the size member function for a vector of int, Dialogue: 0,0:45:02.00,0:45:05.00,Default,,0000,0000,0000,,it comes off this template. So this template describes what the size member function Dialogue: 0,0:45:05.00,0:45:09.00,Default,,0000,0000,0000,,looks like for any of the myvectors you might instantiate. And it describes the Dialogue: 0,0:45:09.00,0:45:12.00,Default,,0000,0000,0000,,template because, in fact, we need to build a new size for Dialogue: 0,0:45:12.00,0:45:14.00,Default,,0000,0000,0000,,ints versus doubles versus strings. Dialogue: 0,0:45:14.00,0:45:17.00,Default,,0000,0000,0000,,It's even funny because you think of my size like, ''Well size doesn't even Dialogue: 0,0:45:17.00,0:45:21.00,Default,,0000,0000,0000,,use anything related to the elem type.'' But in fact, each of the member functions Dialogue: 0,0:45:21.00,0:45:25.00,Default,,0000,0000,0000,,is kinda specific. It's not just a myvector size; it's the myvector int size, the Dialogue: 0,0:45:25.00,0:45:26.00,Default,,0000,0000,0000,,myvector string size. Dialogue: 0,0:45:26.00,0:45:30.00,Default,,0000,0000,0000,,And that for some of the member functions it's quite obvious why you need a distinct Dialogue: 0,0:45:30.00,0:45:33.00,Default,,0000,0000,0000,,copy. Get at returns an int in some cases and a double in others; Dialogue: 0,0:45:33.00,0:45:36.00,Default,,0000,0000,0000,,but even though ones that don't appear to have any dependence on the elem type, Dialogue: 0,0:45:36.00,0:45:37.00,Default,,0000,0000,0000,,actually are Dialogue: 0,0:45:37.00,0:45:40.00,Default,,0000,0000,0000,,separated into their own Dialogue: 0,0:45:40.00,0:45:41.00,Default,,0000,0000,0000,,individual versions. Dialogue: 0,0:45:41.00,0:45:45.00,Default,,0000,0000,0000,,So I think I got all of that fixed, and then I'm gonna have to do one thing that's gonna seem Dialogue: 0,0:45:45.00,0:45:49.00,Default,,0000,0000,0000,,really quirky. And it is very quirky but it is C++. Let me show Dialogue: 0,0:45:49.00,0:45:51.00,Default,,0000,0000,0000,,you what I'm gonna do. Dialogue: 0,0:45:51.00,0:45:59.00,Default,,0000,0000,0000,,Is I'm going [inaudible] out of the project. Okay, Dialogue: 0,0:45:59.00,0:46:01.00,Default,,0000,0000,0000,,stop compiling that. Dialogue: 0,0:46:01.00,0:46:01.00,Default,,0000,0000,0000,,And I'm gonna change Dialogue: 0,0:46:01.00,0:46:12.00,Default,,0000,0000,0000,,how it is that myvector gets compiled by doing this. Okay. Take Dialogue: 0,0:46:12.00,0:46:14.00,Default,,0000,0000,0000,,a deep breath. Dialogue: 0,0:46:14.00,0:46:15.00,Default,,0000,0000,0000,,This is really Dialogue: 0,0:46:15.00,0:46:18.00,Default,,0000,0000,0000,,just an oddity of C++. Dialogue: 0,0:46:18.00,0:46:20.00,Default,,0000,0000,0000,,So the situation is this: that Dialogue: 0,0:46:20.00,0:46:23.00,Default,,0000,0000,0000,,templates aren't really compiled ahead of time, Dialogue: 0,0:46:23.00,0:46:25.00,Default,,0000,0000,0000,,templates are just patterns. Dialogue: 0,0:46:25.00,0:46:26.00,Default,,0000,0000,0000,,You know? They like describe Dialogue: 0,0:46:26.00,0:46:30.00,Default,,0000,0000,0000,,a recipe for how you would build a myvector class. Dialogue: 0,0:46:30.00,0:46:34.00,Default,,0000,0000,0000,,But you can't just compile myvector and be done with it because until the client Dialogue: 0,0:46:34.00,0:46:37.00,Default,,0000,0000,0000,,uses it, you don't know what kind of myvectors you're building. Are they myvectors of ints Dialogue: 0,0:46:37.00,0:46:39.00,Default,,0000,0000,0000,,or strings or pseudo structures? Dialogue: 0,0:46:39.00,0:46:43.00,Default,,0000,0000,0000,,So it turns out that the myvector really needs to get compiled Dialogue: 0,0:46:43.00,0:46:46.00,Default,,0000,0000,0000,,at the usage, at the instantiation. When you're ready to make a myvector of Dialogue: 0,0:46:46.00,0:46:50.00,Default,,0000,0000,0000,,students, it then needs to see all the code for myvector so it can go build you a Dialogue: 0,0:46:50.00,0:46:53.00,Default,,0000,0000,0000,,myvector for students on the fly. Dialogue: 0,0:46:53.00,0:46:56.00,Default,,0000,0000,0000,,In order to see that code, it actually has to be present in a different way than Dialogue: 0,0:46:56.00,0:46:59.00,Default,,0000,0000,0000,,most code. Most code is compiled, instead of .cpp, it just Dialogue: 0,0:46:59.00,0:47:02.00,Default,,0000,0000,0000,,gets compiled once and once for all. The random library, random integer doesn't Dialogue: 0,0:47:02.00,0:47:06.00,Default,,0000,0000,0000,,change for anybody usage, there's a random.cpp. It compiled the function. Dialogue: 0,0:47:06.00,0:47:07.00,Default,,0000,0000,0000,,You're done. Dialogue: 0,0:47:07.00,0:47:11.00,Default,,0000,0000,0000,,So the template code does not get compiled ahead of time. It doesn't get listed in the Dialogue: 0,0:47:11.00,0:47:13.00,Default,,0000,0000,0000,,project. What happens is the .h Dialogue: 0,0:47:13.00,0:47:17.00,Default,,0000,0000,0000,,typically has not only the interface, but actually all the code. Dialogue: 0,0:47:17.00,0:47:21.00,Default,,0000,0000,0000,,And so the two ways to get the code in here, one way is I could've just put all the Dialogue: 0,0:47:21.00,0:47:24.00,Default,,0000,0000,0000,,code down here. And that's the way a lot of professional code gets written, it has Dialogue: 0,0:47:24.00,0:47:28.00,Default,,0000,0000,0000,,the interface followed by all the template code right after it. I Dialogue: 0,0:47:28.00,0:47:32.00,Default,,0000,0000,0000,,like to keep us thinking about the interface and the implementation being Dialogue: 0,0:47:32.00,0:47:32.00,Default,,0000,0000,0000,,separate, Dialogue: 0,0:47:32.00,0:47:35.00,Default,,0000,0000,0000,,so I'm actually taking the interface and keeping the .h, keeping this [inaudible] Dialogue: 0,0:47:35.00,0:47:36.00,Default,,0000,0000,0000,,over here Dialogue: 0,0:47:36.00,0:47:42.00,Default,,0000,0000,0000,,in a .cpp. And then I'm using the #include mechanism in a very unusual way. That almost Dialogue: 0,0:47:42.00,0:47:45.00,Default,,0000,0000,0000,,never would you want to, in a regular usage, to #include another Dialogue: 0,0:47:45.00,0:47:49.00,Default,,0000,0000,0000,,.cpp file. For templates, we're making an exception. And we're saying, ''Well Dialogue: 0,0:47:49.00,0:47:53.00,Default,,0000,0000,0000,,in this case, because I really need that code there,'' the #include mechanism is Dialogue: 0,0:47:53.00,0:47:57.00,Default,,0000,0000,0000,,basically saying go take the contents of this thing and just dump it in here. Dialogue: 0,0:47:57.00,0:48:01.00,Default,,0000,0000,0000,,It really is an include mechanism. It says, ''Go get this file and take its text Dialogue: 0,0:48:01.00,0:48:03.00,Default,,0000,0000,0000,,contents and dump it right into this file.'' Dialogue: 0,0:48:03.00,0:48:06.00,Default,,0000,0000,0000,,So that when somebody's trying to import the myvector.h, Dialogue: 0,0:48:06.00,0:48:09.00,Default,,0000,0000,0000,,they're getting both the interface plus all the code that we'll generate a Dialogue: 0,0:48:09.00,0:48:11.00,Default,,0000,0000,0000,,pattern from. Dialogue: 0,0:48:11.00,0:48:13.00,Default,,0000,0000,0000,,So this is definitely just a quirk. Dialogue: 0,0:48:13.00,0:48:14.00,Default,,0000,0000,0000,,There's no Dialogue: 0,0:48:14.00,0:48:16.00,Default,,0000,0000,0000,, Dialogue: 0,0:48:16.00,0:48:18.00,Default,,0000,0000,0000,,consistency between how other languages that do stuff like this expect Dialogue: 0,0:48:18.00,0:48:19.00,Default,,0000,0000,0000,,this. Dialogue: 0,0:48:19.00,0:48:22.00,Default,,0000,0000,0000,,This is just unique to C++ and its compilation mechanisms that require Dialogue: 0,0:48:22.00,0:48:24.00,Default,,0000,0000,0000,,this kind of sort of slight Dialogue: 0,0:48:24.00,0:48:26.00,Default,,0000,0000,0000,,variation in handling. Dialogue: 0,0:48:26.00,0:48:29.00,Default,,0000,0000,0000,,So we'll see this for all the templates we'll use is that Dialogue: 0,0:48:29.00,0:48:33.00,Default,,0000,0000,0000,,they will not be included as normal cpp files, they will get included in the .h. Dialogue: 0,0:48:33.00,0:48:35.00,Default,,0000,0000,0000,,And there is this exact pattern, which is Dialogue: 0,0:48:35.00,0:48:39.00,Default,,0000,0000,0000,,reproduced for every one of the ones in the text. You'll see it on stack and queue and Dialogue: 0,0:48:39.00,0:48:44.00,Default,,0000,0000,0000,,integer. That it becomes the kind of boilerplate we'll use when making a template. So Dialogue: 0,0:48:44.00,0:48:45.00,Default,,0000,0000,0000,,in general, I'd say Dialogue: 0,0:48:45.00,0:48:49.00,Default,,0000,0000,0000,,be very wary of anything that looks like this. This is not a normal thing to do Dialogue: 0,0:48:49.00,0:48:52.00,Default,,0000,0000,0000,,and we're doing it just specifically to kind of keep up the illusion that the Dialogue: 0,0:48:52.00,0:48:55.00,Default,,0000,0000,0000,,interface and the implementation are kept separate because there's actually Dialogue: 0,0:48:55.00,0:48:57.00,Default,,0000,0000,0000,,some good thinking that comes from that. Dialogue: 0,0:48:57.00,0:49:00.00,Default,,0000,0000,0000,,But the way the compiler sees it, it doesn't want them to be separate, and so Dialogue: 0,0:49:00.00,0:49:06.00,Default,,0000,0000,0000,,we have to accommodate it with this little hack, let's say, here. Dialogue: 0,0:49:06.00,0:49:08.00,Default,,0000,0000,0000,,So once I've done that, Dialogue: 0,0:49:08.00,0:49:10.00,Default,,0000,0000,0000,,I go back to lecture. Dialogue: 0,0:49:10.00,0:49:14.00,Default,,0000,0000,0000,,If I change this to be myvector string, Dialogue: 0,0:49:14.00,0:49:16.00,Default,,0000,0000,0000,,I'm hoping Dialogue: 0,0:49:16.00,0:49:19.00,Default,,0000,0000,0000,,that everything will still work. Dialogue: 0,0:49:19.00,0:49:20.00,Default,,0000,0000,0000,,Which it did, kind Dialogue: 0,0:49:20.00,0:49:21.00,Default,,0000,0000,0000,,of amazingly. Daniel? Dialogue: 0,0:49:21.00,0:49:23.00,Default,,0000,0000,0000,,So Dialogue: 0,0:49:23.00,0:49:27.00,Default,,0000,0000,0000,,where is the myvector.cpp file at? So it's actually just living in the same directory with this, Dialogue: 0,0:49:27.00,0:49:30.00,Default,,0000,0000,0000,,the way myvector.h is. So typically, like your .h files are just sitting in the directory - .cpp is sitting in the Dialogue: 0,0:49:30.00,0:49:34.00,Default,,0000,0000,0000,,same directory. That's where it's gonna look for it when it Dialogue: 0,0:49:34.00,0:49:38.00,Default,,0000,0000,0000,,goes #including is in the kind of local contents. But like Dialogue: 0,0:49:38.00,0:49:39.00,Default,,0000,0000,0000,,where is that? Like is it in Dialogue: 0,0:49:39.00,0:49:42.00,Default,,0000,0000,0000,,resources? No, it's just - if you look - you know this is the directory I have, this Dialogue: 0,0:49:42.00,0:49:43.00,Default,,0000,0000,0000,,is the Dialogue: 0,0:49:43.00,0:49:47.00,Default,,0000,0000,0000,,contents of my, you know all my files, my project files, where the thing gets dumped. Oh, okay. It's just sitting there with all Dialogue: 0,0:49:47.00,0:49:50.00,Default,,0000,0000,0000,,the code. Dialogue: 0,0:49:50.00,0:49:53.00,Default,,0000,0000,0000,,And I should be able to Dialogue: 0,0:49:53.00,0:49:57.00,Default,,0000,0000,0000,,change this now to put some numbers in Dialogue: 0,0:49:57.00,0:50:03.00,Default,,0000,0000,0000,,and have it do both. I just did it Dialogue: 0,0:50:03.00,0:50:05.00,Default,,0000,0000,0000,,with strings and now I'm gonna do it with ints. Dialogue: 0,0:50:05.00,0:50:06.00,Default,,0000,0000,0000,,And Dialogue: 0,0:50:06.00,0:50:10.00,Default,,0000,0000,0000,,voila, we now have something that holds ints. So a certain Dialogue: 0,0:50:10.00,0:50:12.00,Default,,0000,0000,0000,,amount of goo that went from Dialogue: 0,0:50:12.00,0:50:14.00,Default,,0000,0000,0000,,the simple form to the template form, Dialogue: 0,0:50:14.00,0:50:18.00,Default,,0000,0000,0000,,but a lot of power gained. Suddenly we took this thing that was one purpose, Dialogue: 0,0:50:18.00,0:50:21.00,Default,,0000,0000,0000,,that held strings only, and you just made it to where it can hold anything you Dialogue: 0,0:50:21.00,0:50:23.00,Default,,0000,0000,0000,,can think of to stick in there Dialogue: 0,0:50:23.00,0:50:24.00,Default,,0000,0000,0000,, Dialogue: 0,0:50:24.00,0:50:26.00,Default,,0000,0000,0000,,by making Dialogue: 0,0:50:26.00,0:50:28.00,Default,,0000,0000,0000,,little machinations in the Dialogue: 0,0:50:28.00,0:50:29.00,Default,,0000,0000,0000,,syntax there. Dialogue: 0,0:50:29.00,0:50:34.00,Default,,0000,0000,0000,,So we'll see a lot more of this. It's not the first, nor the last, syntax that - for templates that we're gonna be playing Dialogue: 0,0:50:34.00,0:50:36.00,Default,,0000,0000,0000,,with, but that will be next week. Come to Cafe with me, if you Dialogue: 0,0:50:36.00,0:50:45.00,Default,,0000,0000,0000,,got some time. Otherwise, I will see you on Monday.