Lecture 27 | Programming Abstractions (Stanford)
-
0:00 - 0:10
-
0:10 - 0:11
-
0:11 - 0:15This presentation is delivered by the Stanford Center for Professional
-
0:15 - 0:25Development.
-
0:25 - 0:29I'm guessing by the fact that I can see myself that we're on.
-
0:29 - 0:33Welcome back to CS106L. My name's Keith. I'm a section
-
0:33 - 0:37leader here. I teach CS106L, which is the standard C++ program and
-
0:37 - 0:37laboratory.
-
0:37 - 0:39I recognize a few people here, which
-
0:39 - 0:40is great.
-
0:40 - 0:44Julie wanted me to come in today and talk to you about the C++
-
0:44 - 0:47programming language, what's it look like outside of CS106B?
-
0:47 - 0:50What sorts of things do you need to be aware of to just kind of give you a general
-
0:50 - 0:53picture of what this language looks like?
-
0:53 - 0:56Before I get into the actual C++ stuff, I wanted to start off by
-
0:56 - 0:59congratulating all of you for making it through this class. That's not a
-
0:59 - 1:01small accomplishment.
-
1:01 - 1:04If you think about when you started, day one, probably looking at this screen right here, probably
-
1:04 - 1:05thinking,
-
1:05 - 1:08what's that [inaudible], what's Genlib, what's C [inaudible], and why's it
-
1:08 - 1:11really, really less than something?
-
1:11 - 1:12Look where you are now.
-
1:12 - 1:16You now know how to be a client of the vector, the stack, the map, the queue
-
1:16 - 1:17and the set.
-
1:17 - 1:20You know that recursion, you know pointers, you know Linklis and binary
-
1:20 - 1:21[inaudible] and graphs.
-
1:21 - 1:25You know algorithmic analysis and graph algorithms and searching and sorting.
-
1:25 - 1:26You know how the lexicon works.
-
1:26 - 1:29You know how to implement all of these classes. You know data abstraction.
-
1:29 - 1:34That's not a small accomplishment. That's really something to be proud of.
-
1:34 - 1:37What I wanted to say is that these are real, practical tools that will follow
-
1:37 - 1:39you, no matter where you take them.
-
1:39 - 1:43Some of you might go on in CS. You might think, wow, I really like this class. I want to
-
1:43 - 1:45see where it goes. That's great.
-
1:45 - 1:48No matter where you take it, if you go and take algorithms, you take
-
1:48 - 1:52compliers or data bases or operating systems, the skills you've learned here is really
-
1:52 - 1:54going to be the foundation
-
1:54 - 1:57of all of your programing. You're always going to be using maps. You're always
-
1:57 - 2:00going to be using vectors. You're going to need recursion, no matter where you
-
2:00 - 2:01apply it.
-
2:01 - 2:04It's really wonderful that you have these skills.
-
2:04 - 2:06Of course, some of you aren't going to go on in computer science. That's totally fine. You
-
2:06 - 2:11took this class and said, it's not for me or, you know, maybe not. Well,
-
2:11 - 2:12that's fine too because
-
2:12 - 2:15whether you go into sociology or philosophy or psychology or math or
-
2:15 - 2:16physics or chemistry,
-
2:16 - 2:18there are problems to be solved,
-
2:18 - 2:20and there are problems you can solve with a computer.
-
2:20 - 2:23What you've learned in this class is the most valuable skill of them all, which
-
2:23 - 2:25is how to take a problem,
-
2:25 - 2:26model it
-
2:26 - 2:27and solve it.
-
2:27 - 2:29That really is something.
-
2:29 - 2:32The skills you've learned here transcend any specific programming language.
-
2:32 - 2:36You can do everything you've done in here, in Java, in C++, in PHP,
-
2:36 - 2:41in Python. Everything that you think of, you will be using these same tools.
-
2:41 - 2:42That said,
-
2:42 - 2:46we've taught you everything in this class using the C++ programming
-
2:46 - 2:46language.
-
2:46 - 2:48We use C++
-
2:48 - 2:52because it's very good for expressing the concepts that we were going over. It
-
2:52 - 2:56has great support for recursion, has exposed pointers, so you can do things like
-
2:56 - 2:58Linklis and binary trees quite nicely.
-
2:58 - 3:01It has objects so you can do data abstraction. It's got templates. It's a very
-
3:01 - 3:05good mix of different programing strategies and different programming styles
-
3:05 - 3:07that means we can teach you things without having to bog you down in
-
3:07 - 3:09language syntax.
-
3:09 - 3:11That said, this really hasn't been much of a class
-
3:11 - 3:13in C++.
-
3:13 - 3:16Think of it was we all put you inside the C++ bus and drove
-
3:16 - 3:19you up to the top of the mountain of CS106B knowledge.
-
3:19 - 3:21You know that the bus got you up there,
-
3:21 - 3:24but you don't really know what's going on under the hood.
-
3:24 - 3:27That's okay because what you learn in this class is more important than that.
-
3:27 - 3:30All the programing language and knowledge in the universe is not going to help you
-
3:30 - 3:33solve a problem if you don't know how to use a vector, you don't know how to make a
-
3:33 - 3:36map, and you can't make a recursive function solve things.
-
3:36 - 3:40What I want to do today is talk about what the C++ language is. What's
-
3:40 - 3:42it look like the real world?
-
3:42 - 3:43What kind of stuff do you need to know?
-
3:43 - 3:46More importantly, how do you take these skills that you've learned, which are very
-
3:46 - 3:50generic, and go and apply them in this language.
-
3:50 - 3:53I love C++. I've been using it for years. I think it's a beautiful
-
3:53 - 3:56language. It's got support for so many different programing paradigms. It's
-
3:56 - 3:59easy to use once you get a handle on it.
-
3:59 - 4:01It lets you solve problems in so many different ways.
-
4:01 - 4:04Really, I just want to show you what it looks like.
-
4:04 - 4:07Think of this as an appetizer. I'm just going to give you the - this
-
4:07 - 4:10is the C++ appetizer plate. I'll show you a little bit of this, a
-
4:10 - 4:11little bit of that,
-
4:11 - 4:15give you enough working knowledge that you know where to go to get help.
-
4:15 - 4:16You can see
-
4:16 - 4:18this is what you need to learn, get
-
4:18 - 4:21an overview of what we've been doing, what we've provided you, what we haven't
-
4:21 - 4:22provided you,
-
4:22 - 4:25so that by the time you're done, you can think,
-
4:25 - 4:27maybe I want to go on in this language.
-
4:27 - 4:30I'm not going to force you to learn C++. I can't do that. I'm
-
4:30 - 4:33not going to follow you around, did you learn C++ yet? Did you learn C++ yet?
-
4:33 - 4:35No, I'm not going to do that.
-
4:35 - 4:38You guys are all competent programmers right now.
-
4:38 - 4:41You know how to solve these problems. You know how to take on these challenges.
-
4:41 - 4:43So I just want to show you the language so you can decide whether or not you
-
4:43 - 4:44want to pursue it.
-
4:44 - 4:47Maybe you will. That'd be great. If not,
-
4:47 - 4:48that's okay, too,
-
4:48 - 4:51because you know how to tackle problems, and all you need now is a language to
-
4:51 - 4:53do it
-
4:53 - 4:56in. I want to show you basically four aspects of C++. First
-
4:56 - 5:00some philosophy and some history because every language has a personality.
-
5:00 - 5:03Every language wants to treat you as a programmer a different way, give you some
-
5:03 - 5:05different options. So I just want to show you what to expect when
-
5:05 - 5:07you're working with C++.
-
5:07 - 5:10As a language, it has a lot of criticisms. How many of you have heard
-
5:10 - 5:15bad things about C++ in any way, shape or form?
-
5:15 - 5:18Wow, every, single person in this room, basically.
-
5:18 - 5:21So there's a lot of criticisms leveled at this language, and what I want to
-
5:21 - 5:22do
-
5:22 - 5:25is show you what the mindset is so you can take a look at these
-
5:25 - 5:28criticisms and evaluate them. In all seriousness, most of the
-
5:28 - 5:30criticisms leveled at this language are
-
5:30 - 5:33criticisms with things a language doesn't try to do.
-
5:33 - 5:37At the same time, if you get this philosophy and you get this history, you'll
-
5:37 - 5:38understand where this language came from,
-
5:38 - 5:41what it's designed to do and where it's going.
-
5:41 - 5:44The second thing I want to talk about is the actual mechanics.
-
5:44 - 5:47What does the programing language look like? So I'll talk about the
-
5:47 - 5:47libraries, and
-
5:47 - 5:49I'll talk about the core language features.
-
5:49 - 5:52What happens when you take away things like [inaudible] and Synfi? What are you going to have
-
5:52 - 5:54to do now that you don't have those? In
-
5:54 - 5:58terms of it's actually pretty straightforward and you already know most of it.
-
5:58 - 6:00Then some language features that we didn't cover, things that you would expect to
-
6:00 - 6:02see in a professional setting
-
6:02 - 6:05that we didn't go over in this class because it's needlessly complicated and
-
6:05 - 6:08doesn't have much to do with this general data structure and
-
6:08 - 6:10algorithm challenges we've been giving you.
-
6:10 - 6:12Finally, just some references.
-
6:12 - 6:14Where do you go?
-
6:14 - 6:17You want to learn C++? Great. We have a list of wonderful books and resources
-
6:17 - 6:18you can go and reference,
-
6:18 - 6:20and it can get you up to speed very quickly.
-
6:20 - 6:22So it's kind of a game plan.
-
6:22 - 6:23See what's out there,
-
6:23 - 6:28have some fun with it and learn where to go. Sound good? Okay.
-
6:28 - 6:29So I want to start off
-
6:29 - 6:32with a quick history of C++.
-
6:32 - 6:34So C++ did not come into being one day when a whole bunch of programmers came
-
6:34 - 6:37into a room, made some demonic incantations over a bubbling cauldron and
-
6:37 - 6:39walked out. It didn't happen.
-
6:39 - 6:42It was not invented in one day. People did not sit down and say, our language is going to look like
-
6:42 - 6:44this.
-
6:44 - 6:45It has a history.
-
6:45 - 6:49It has evolution, and you can see different traces of we tried doing
-
6:49 - 6:51this. It didn't work.
-
6:51 - 6:54Let's go add this feature, etc.
-
6:54 - 6:56It started off
-
6:56 - 6:58with this guy.
-
6:58 - 7:02So I will attempt to pronounce his name. It's Bjarne Stroustrup.
-
7:02 - 7:05I've been told that the way to pronounce this is to say Stroustrup with a
-
7:05 - 7:10Norwegian accent while cramming a potato down your throat. I'll
-
7:10 - 7:10just
-
7:10 - 7:14get it close. Anyway, he's a Danish computer scientist, and he was getting his PhD in computer
-
7:14 - 7:15science from Cambridge,
-
7:15 - 7:19and his specialty was in distributive systems and operating systems. So his goal
-
7:19 - 7:22was, let's take some problems, spread it out over several computers, have
-
7:22 - 7:26them communicate via network protocol and get some results.
-
7:26 - 7:29He chose to write this program in a language called Simula. I
-
7:29 - 7:34confess, I don't know much about this language. I haven't heard anyone using it, but
-
7:34 - 7:37back then, it was the his object-oriented language, and he said it helped him think
-
7:37 - 7:38about the problem.
-
7:38 - 7:42It had classes, so you could build a computer object, a protocol object, a
-
7:42 - 7:43network object,
-
7:43 - 7:46and they'd send messages to each other the same way that a
-
7:46 - 7:48physical computer, a physical network
-
7:48 - 7:51and a physical protocol would communicate with each other. He
-
7:51 - 7:54got it working pretty fast and ran it,
-
7:54 - 7:57and much to his surprise, he found out that it was so abysmally slow that he could get no
-
7:57 - 7:59practical results out of it. The
-
7:59 - 8:01question is what went wrong?
-
8:01 - 8:04It wasn't his program. It was the Simula language implementation.
-
8:04 - 8:08He actually ran a profiler on his code to figure out why it was going slowly.
-
8:08 - 8:1080 percent of the time his program was running, it was going automatic
-
8:10 - 8:13garbage collection, even though he was doing [inaudible] memory management.
-
8:13 - 8:14So think about this.
-
8:14 - 8:17You spend several weeks writing a program.
-
8:17 - 8:17You run it,
-
8:17 - 8:20and for every line of code you're writing, four lines of meaningless code
-
8:20 - 8:24that someone else wrote are also executing. That's really slow. So
-
8:24 - 8:27he had to change approaches. He went and rewrote this entire program in a
-
8:27 - 8:30different language called BCPL, which is related to C and the B
-
8:30 - 8:33programing language that came before C.
-
8:33 - 8:36Of course, it's very low-level, it's very fast, but it didn't have these nice
-
8:36 - 8:37high-level features in it.
-
8:37 - 8:39He got it working, but it took a lot of time.
-
8:39 - 8:42When it was done, he decided, I'm never going to tackle a
-
8:42 - 8:46problem like this again until I have a proper tool for the job. He
-
8:46 - 8:49ended up at AT&T Bell labs, which is the same place that the C programming language came
-
8:49 - 8:53out of. In fact, he knew Kernighan and Ritchie, who invented that,
-
8:53 - 8:55and came up with this language called C with classes. It was
-
8:55 - 8:58a precursor to C++, and people loved it.
-
8:58 - 9:02It combined the best features of Simula, which is this object-oriented design,
-
9:02 - 9:04with the best features of C, which is the runtime efficiency.
-
9:04 - 9:06It was something that was fast,
-
9:06 - 9:09flexible and helped you think about problems.
-
9:09 - 9:11What he would do is work on this implementation.
-
9:11 - 9:14He had people actually using the C++, and when they needed new
-
9:14 - 9:15features, they said,
-
9:15 - 9:17Bjarne, it doesn't work. He'd go fix it.
-
9:17 - 9:20We had this cycle of real programmers solving real problems
-
9:20 - 9:23with this developer saying, okay, let's go fix it.
-
9:23 - 9:26After a while, you ended up with C++. I think this
-
9:26 - 9:28is the 25th anniversary of C++, so
-
9:28 - 9:30how exciting.
-
9:30 - 9:33I want to talk about the philosophy a little bit. What is really driving the
-
9:33 - 9:36development of this language? What does it expect out of you?
-
9:36 - 9:39So I have some quotes from this book called The Design and Evolution of C++. It's
-
9:39 - 9:40a good book. You should read it.
-
9:40 - 9:42The first one,
-
9:42 - 9:45C++'s evolution should be driven by real problems, and you don't
-
9:45 - 9:47want to get down on a quest for perfection.
-
9:47 - 9:51Basically, this language is designed to solve real problems that
-
9:51 - 9:53real programmers like you are going to run into
-
9:53 - 9:55in not necessarily the best way.
-
9:55 - 9:56It tries to do a good job.
-
9:56 - 10:01It's not intended to be perfect. There are some sloppy edges, but it works.
-
10:01 - 10:04Because it is driven by real problems and real design issues, you can solve
-
10:04 - 10:06real problems with it.
-
10:06 - 10:07If you think that's a good thing in a language,
-
10:07 - 10:11I personally do. I think it's good to have a language that can solve problems,
-
10:11 - 10:14then C++ is a good choice.
-
10:14 - 10:17This is probably the one that you've seen the most. Don't try to force people.
-
10:17 - 10:21C++ gives you so many choices that your question should not be,
-
10:21 - 10:24how do I do this in the language, but more, which of these hundreds of
-
10:24 - 10:26options is the best choice for me?
-
10:26 - 10:29It really trusts you as a language. It will give you an incredible amount of
-
10:29 - 10:33flexibility, even to the point where it will let you make the wrong decision.
-
10:33 - 10:36I've always thought C++ is a language that will let you ride a
-
10:36 - 10:37bicycle without a helmet
-
10:37 - 10:40because that one time that you need to go under a bridge that's exactly a quarter inch over your
-
10:40 - 10:43head, you wont have your helmet knock you off your bike. You
-
10:43 - 10:45do have to worry about a lot of risks,
-
10:45 - 10:48but in the end, you'll have more flexibility than you'll find in most other
-
10:48 - 10:50languages you
-
10:50 - 10:52see. Finally, I think the most important one
-
10:52 - 10:54is this.
-
10:54 - 10:56C++ makes programming more enjoyable
-
10:56 - 10:58for serious programmers.
-
10:58 - 11:01The two things here are serious programmers and more enjoyable.
-
11:01 - 11:04This is a professional language. It's used in industry everywhere.
-
11:04 - 11:05It's very fast.
-
11:05 - 11:08It's very efficient. It has a bit of a learning curve. It is kind of
-
11:08 - 11:11tricky to get into the language, but once you've got it,
-
11:11 - 11:13the second part, the more enjoyable, is so true.
-
11:13 - 11:16This language is fun to write things in.
-
11:16 - 11:19Once you've got it down, you will actually step back from your program and say, wow,
-
11:19 - 11:22I just wrote something that took every, single word out of this set that I had
-
11:22 - 11:24[inaudible] contains a specific letter
-
11:24 - 11:25in a single line of code.
-
11:25 - 11:29Or I lettered all the contents of this file into my set in one line of code. Or I
-
11:29 - 11:31just wrote a template that
-
11:31 - 11:34is a multi-dimensional ray of any dimension I want.
-
11:34 - 11:37Those are not trivial accomplishments, and you can do it with
-
11:37 - 11:38this language.
-
11:38 - 11:39It's fun.
-
11:39 - 11:41It's a great language, and if you think that this sort of philosophy is what you
-
11:41 - 11:43want, where it will trust you,
-
11:43 - 11:46it will really let you make the decisions rather than forcing you into any one
-
11:46 - 11:47paradigm,
-
11:47 - 11:48learn C++.
-
11:48 - 11:51I think you'll love it. So
-
11:51 - 11:55philosophy. There's lots of it, but I'm not here to teach a philosophy
-
11:55 - 11:58class. I'm here to teach you about C++, so let's get down to some of the details. What does this
-
11:58 - 12:00language look like?
-
12:00 - 12:02The first thing I want to do is talk about
-
12:02 - 12:04what Genlib.H has.
-
12:04 - 12:06Since day one, you probably have seen
-
12:06 - 12:07things like this. How
-
12:07 - 12:10to include Genlib. Very first line, right here.
-
12:10 - 12:14What is in this Genlib thing? Has anybody actually looked at Genlib before? Actually, pulled up
-
12:14 - 12:20the Genlib file and looked inside? No one? Okay.
-
12:20 - 12:22If you look inside Genlib,
-
12:22 - 12:24it looks mostly like this.
-
12:24 - 12:27There are basically three important lines you need to know.
-
12:27 - 12:28Time includes string.
-
12:28 - 12:31So we talked to you about the string classes that was a built-in type, like Ent or
-
12:31 - 12:35double. It's a class. It's like any other object, like a vector or a map. You do need
-
12:35 - 12:36to pound include it.
-
12:36 - 12:39We just took care of it for you because since you use it everywhere and it's easy to
-
12:39 - 12:42forget and you can get some pretty nasty compiler errors if you don't,
-
12:42 - 12:44we thought,
-
12:44 - 12:47we'll be nice. We'll do that for you.
-
12:47 - 12:48The second thing is this error function.
-
12:48 - 12:52You've seen error. Genlib just gives you a prototype for it. The end.
-
12:52 - 12:56But this last one right here using name space STD. Using the standard
-
12:56 - 12:58name space. What on
-
12:58 - 13:00earth is this line?
-
13:00 - 13:03It looks almost like an English sentence.
-
13:03 - 13:05Compiler, make my code work.
-
13:05 - 13:07What's going on here?
-
13:07 - 13:10Well, I'll show you.
-
13:10 - 13:14Suppose I have this little program back here. Can anybody see this? So I want to pound include [inaudible]
-
13:14 - 13:17and pound include string.
-
13:17 - 13:19If you notice here, I don't have Genlib, and I'm going to make
-
13:19 - 13:22a string and print it out.
-
13:22 - 13:26So the question is, when I say string, my string,
-
13:26 - 13:28what am I referring to?
-
13:28 - 13:31Well, when this happens, the compiler says, well, go look for something called
-
13:31 - 13:32string.
-
13:32 - 13:34When we pound included string,
-
13:34 - 13:38it put it inside this big bucket of something called name space standard.
-
13:38 - 13:41The idea is that the real name of string is standard string. The real name of C out
-
13:41 - 13:44is standard C out. The real name of endal is standard endal.
-
13:44 - 13:47It's just so that if you make your own versions of those things, it doesn't have
-
13:47 - 13:51the same name as the standard. It won't conflict, and your program will still compile.
-
13:51 - 13:54But the problem is that if you try to run this, what
-
13:54 - 13:55will happen is that
-
13:55 - 13:56the compiler says string,
-
13:56 - 13:59looks up here and hits this wall. It
-
13:59 - 14:03says, oops, that's inside the standard name space. I can't go in there,
-
14:03 - 14:06and you'll get this really nasty compiler error, something like, string's not defined. C out's not defined.
-
14:06 - 14:08Endal's not defined.
-
14:08 - 14:12Now, most of the time when you're programming, you want things like C out and
-
14:12 - 14:14string to actually exist.
-
14:14 - 14:17So rather than saying you have to call it standard string, standard C out, standard
-
14:17 - 14:19endal,
-
14:19 - 14:22you can introduce this little phrase right here,
-
14:22 - 14:24using name space standard. What it says
-
14:24 - 14:26is take this wall and get rid of it.
-
14:26 - 14:29It's like, Mr. Compiler, tear down this wall,
-
14:29 - 14:30and the result
-
14:30 - 14:34is that this barrier gets a little bit
-
14:34 - 14:34transparent.
-
14:34 - 14:38It's all still included inside of the standard name
-
14:38 - 14:39space, but now it's accessible.
-
14:39 - 14:42So when you go, string goes, oh, yeah, that thing, and
-
14:42 - 14:44actually compiles.
-
14:44 - 14:46Now most of today, I'm not going to be hitting you with C++ and saying,
-
14:46 - 14:49you must know this. You must know this. There's just not enough time to
-
14:49 - 14:50go over everything,
-
14:50 - 14:53but this little line might be something worth committing to memory because it's
-
14:53 - 14:56the most noticeable change you'll see when you stop using Genlib.
-
14:56 - 14:59If you don't put that line in your code, you'll get lots of compiler errors, and it will
-
14:59 - 15:00just scare you.
-
15:00 - 15:02So if you put this line in,
-
15:02 - 15:06I'd say 90 percent of your compiler problems will go away.
-
15:06 - 15:07That's that line.
-
15:07 - 15:10That's Genlib, and if you want to take a look,
-
15:10 - 15:13if you right this code, you basically replace all of
-
15:13 - 15:16Genlib. The first header file you'll have to include is this one, CSTDLIB, the C
-
15:16 - 15:18standard library.
-
15:18 - 15:22I'm thinking that when the invented the C programing language, it cost them
-
15:22 - 15:26several million dollars per consonant or vowel they put in their header files.
-
15:26 - 15:30So they put it as small as possible but still readable. The idea
-
15:30 - 15:32is that you have these two lines.
-
15:32 - 15:33Pound include string
-
15:33 - 15:35using the standard name
-
15:35 - 15:38space. Then right here in this error function, you just
-
15:38 - 15:41print everything to C error, which is like C out. It's just designed for error
-
15:41 - 15:45handling. You call exit minus one, which says, quit this program. Report some error to
-
15:45 - 15:49the operating system and we're done.
-
15:49 - 15:52This is it. This is all that is contained in Genlib, and
-
15:52 - 15:54the only two important lines are these two right here.
-
15:54 - 15:57Including the string class and using the standard name space. If you do
-
15:57 - 15:58that,
-
15:58 - 16:00and you get [inaudible] Genlib,
-
16:00 - 16:03you're going to be in perfectly good shape.
-
16:03 - 16:06Now, Genlib is probably one of the easier ones, but what about some of these other
-
16:06 - 16:08headers you've seen?
-
16:08 - 16:11I could go into some detail about all of these, but I don't have
-
16:11 - 16:14time. I really wish I could show you how these work,
-
16:14 - 16:17but I'm going to give you a quick overview today
-
16:17 - 16:19to show you what you'll need to know if you want to get off these
-
16:19 - 16:21libraries.
-
16:21 - 16:25The first one is [inaudible]. This is the one that gives you string to integer,
-
16:25 - 16:30integer to string, string to reel, convert to uppercase, convert to lowercase, etc.
-
16:30 - 16:32Behind the scenes, most of this stuff is backed by this class called a string stream. You've seen
-
16:32 - 16:33[inaudible]
-
16:33 - 16:37streams, you've seen - they're all streams. You can do
-
16:37 - 16:39less than, you can do greater than.
-
16:39 - 16:42The point of the string stream, it does the same thing, except that
-
16:42 - 16:44it writes to a string buffer.
-
16:44 - 16:48So you can take some integer, dump it into a string, that's your integer to
-
16:48 - 16:49string function right there.
-
16:49 - 16:52There's a couple other things you can do with it. We use it for doing
-
16:52 - 16:52
-
16:52 - 16:56conversion between strings in other data types like integers.
-
16:56 - 16:59So you might want to look into that if you want to play around with it.
-
16:59 - 17:01As for convert to upper or lower case,
-
17:01 - 17:04there are these functions, two upper and two lower, that take individual characters
-
17:04 - 17:07and convert them to upper or lower case. Just
-
17:07 - 17:10fore loop over your string. Call that function every time.
-
17:10 - 17:13Presto. Your string is in uppercase. That was pretty straightforward.
-
17:13 - 17:16If you're interested, in CS106L, we actually wrote some of these
-
17:16 - 17:20functions. If you want to have a working implementation of [inaudible], if you go to
-
17:20 - 17:24the CS106L web site, under the code section, there is a working implementation
-
17:24 - 17:26there. So if you want to keep using these things,
-
17:26 - 17:28just go download that file, take a look at it,
-
17:28 - 17:31play around with it, and it should work.
-
17:31 - 17:32So
-
17:32 - 17:34if you wanted to set up a
-
17:34 - 17:36coding environment on some
-
17:36 - 17:38Linux machine that
-
17:38 - 17:41doesn't have any of the standard PC or Mac startup files,
-
17:41 - 17:46we can just use that? Yeah, it uses only standard C++, so it will compile everywhere.
-
17:46 - 17:49It works pretty well. I think we only defined a few of them, but you
-
17:49 - 17:51can see how they work, and you can define it for
-
17:51 - 17:55- you can do string to integer, string to double or string to reel pretty easily just be changing a
-
17:55 - 18:00couple types around. Okay.
-
18:00 - 18:03This is probably the big one you're going to miss. Sympio.H.
-
18:03 - 18:09How many of you have ever played around with raw C++ input functions before?
-
18:09 - 18:12They're kind of nasty. I've always thought this thing called C in, which
-
18:12 - 18:15is the counterpart of C out that does input, is kind of like a rose. It's
-
18:15 - 18:19very sweet. It's very delicate, but the second you break it, it stabs you with a thorn,
-
18:19 - 18:22and it hurts a lot.
-
18:22 - 18:25Really, you have to think of this as probably one of the most powerful one of
-
18:25 - 18:29the hardest-to-use input libraries out of any programing language you'll see.
-
18:29 - 18:32We gave you this Sympio library just to take care of all these things for you so
-
18:32 - 18:34you don't have to worry about
-
18:34 - 18:36it.
-
18:36 - 18:39Everybody remember get line? You can use it to get a line out of a file?
-
18:39 - 18:43You can use get line on this C in stream to do input reading.
-
18:43 - 18:44Real line as text
-
18:44 - 18:46uses string stream to convert it to an integer,
-
18:46 - 18:49plus or minus a couple extra things. That's get integer.
-
18:49 - 18:52Again, if you go to the CS106L web site, there is a working implementation of
-
18:52 - 18:53this.
-
18:53 - 18:56If you do want to consider doing C++ beyond this, it actually is
-
18:56 - 19:00a pretty good set of input functions. You'd be surprised how many people don't
-
19:00 - 19:01actually know how to write these things.
-
19:01 - 19:04Go fire it up. Have some fun,
-
19:04 - 19:05see what they do and
-
19:05 - 19:07that way, you continue using the coding conventions that you've seen
-
19:07 - 19:12but by only using standard C++. What about
-
19:12 - 19:14random?
-
19:14 - 19:17The random functions in C++ are
-
19:17 - 19:21pretty simple. There's two functions, rand and Crand.
-
19:21 - 19:23Random and C, the randomizer. Again,
-
19:23 - 19:27consonants are expensive, vowels are expensive.
-
19:27 - 19:30Rand gives you random integers in the range zero
-
19:30 - 19:31to rand max.
-
19:31 - 19:34So if you want to get a random double, you want to get a random integer, a random Goulian,
-
19:34 - 19:39just take the number in that range, scale it down to zero one, scale it back up, translate it,
-
19:39 - 19:40etc.
-
19:40 - 19:42It turns out that this is probably the easiest one to rewrite from scratch. You just have to
-
19:42 - 19:46be a little bit careful of how you do our bounce checking, but it's not so bad.
-
19:46 - 19:49The header file for that is C standard library, but you can probably build
-
19:49 - 19:53this one up from scratch pretty simply.
-
19:53 - 19:56The one that you actually are going to be missing is this one.
-
19:56 - 19:58Graphics.H and extended graphics.
-
19:58 - 20:02Unfortunately, C++ does not have a standard graphics library. It
-
20:02 - 20:05just doesn't exist. There's a couple reasons. One, it's very hard to
-
20:05 - 20:08standardize graphics. You have to make sure that it's something that works on every,
-
20:08 - 20:10single platform, which is very hard to do.
-
20:10 - 20:13Also, if you're writing C++ code from a microprocessor where
-
20:13 - 20:16you don't have graphics, it would be difficult to support the
-
20:16 - 20:18library.
-
20:18 - 20:21So there's no standard C++ equivalent, but I bet you this program
-
20:21 - 20:24that I'm using right now is written in C++. It's got some kind of
-
20:24 - 20:25graphics.
-
20:25 - 20:27Most of this is from third-party libraries. I do
-
20:27 - 20:29some Windows programing, so I use - for the Win32 API, which does some
-
20:29 - 20:31graphics stuff.
-
20:31 - 20:35There's a bunch of cross-platform ones like open GL. You can do X Window system.
-
20:35 - 20:36I think Mac's is called Carbon. I
-
20:36 - 20:38might be wrong about that.
-
20:38 - 20:41[Crosstalk] Yeah,
-
20:41 - 20:44there's a lot, and you won't have trouble finding it. You just won't
-
20:44 - 20:47have a standard library that does it for you. So shop around with this. The
-
20:47 - 20:48
-
20:48 - 20:50ones we give you are pretty nice.
-
20:50 - 20:53I might tell you how they work except that I don't know because it's
-
20:53 - 20:55really, really hard.
-
20:55 - 21:00So take a look around, and you'll find some ones that are quite nice.
-
21:00 - 21:02Everything I've shown you up to this point are simple, like
-
21:02 - 21:03how to read input,
-
21:03 - 21:04how to convert to a string,
-
21:04 - 21:07things that, while they're nice and important, are not going to
-
21:07 - 21:08make or break your program.
-
21:08 - 21:11The thing that is going to make a difference are
-
21:11 - 21:12these ADTs.
-
21:12 - 21:14The vector, the set, the map,
-
21:14 - 21:15the stack, the queue.
-
21:15 - 21:16Those are important.
-
21:16 - 21:17If you don't have a map,
-
21:17 - 21:20the number of things you can do in a program drops exponentially.
-
21:20 - 21:24It's really impressive how much you need these data structures.
-
21:24 - 21:27The good news is that C++ not only has a very good support for these data
-
21:27 - 21:31structures, it has probably the best library in any standard programming
-
21:31 - 21:32language
-
21:32 - 21:33that does these things for you.
-
21:33 - 21:37It's called the Standard Template Library, which is the
-
21:37 - 21:41STL. For every, single container class you have seen in this class, all the ADTs,
-
21:41 - 21:42you will find them in the STL.
-
21:42 - 21:46The names might be a little bit different, the syntax is a little bit weirder, but conceptually,
-
21:46 - 21:49it's the same thing. A vector is a vector, no matter what language
-
21:49 - 21:51you write it
-
21:51 - 21:54in. The STL
-
21:54 - 21:57on top of that gives you these things called algorithms.
-
21:57 - 22:00I'll talk about this in a little bit.
-
22:00 - 22:02Basically, it's the best thing since sliced bread.
-
22:02 - 22:06I'm not kidding. It's really wonderful and amazing, and I'll talk about that in a
-
22:06 - 22:07little bit.
-
22:07 - 22:10The STL is one of the major features of the C++ standard library.
-
22:10 - 22:11It's
-
22:11 - 22:12brilliantly designed.
-
22:12 - 22:13It's not necessarily
-
22:13 - 22:17the most intuitive thing, but once you get a handle of it, it's very
-
22:17 - 22:20powerful.
-
22:20 - 22:23The way you can visualize what this looks like is as this
-
22:23 - 22:25somewhat pyramid
-
22:25 - 22:25structure.
-
22:25 - 22:27At the very bottom, you've got containers.
-
22:27 - 22:31Your data structures, this is your vector, you map, your set.
-
22:31 - 22:33You have iterators on top of that. You've all seen iterators in this class.
-
22:33 - 22:36You've seen map and set iterators.
-
22:36 - 22:37The STL is basically
-
22:37 - 22:41all about iterators. Every, single container class has
-
22:41 - 22:42iterators.
-
22:42 - 22:45You can iterate over a vector or map or set.
-
22:45 - 22:46Those worked
-
22:46 - 22:49to build these things called algorithms where they're
-
22:49 - 22:51functions that operate on ranges of data.
-
22:51 - 22:54It's really impressive what you can do with these things. I'll show you a
-
22:54 - 22:57little bit of that
-
22:57 - 23:01later. These container classes, the good news is that the names are pretty similar.
-
23:01 - 23:04Basically, take the 106 version, make it lowercase, you've got your
-
23:04 - 23:08STL. So big vector turns into little vector. Big map turns into little map,
-
23:08 - 23:13etc. One exception is grid. There is no STL grid class. It's very easy to
-
23:13 - 23:16make one. All you have to do is take a vector and do some math to wrap
-
23:16 - 23:20a two-dimensional container into a one-dimensional one. In fact, that's how our
-
23:20 - 23:22grid works, so you'll have to build that one.
-
23:22 - 23:25Otherwise, everything's taken care for you, and that's nice.
-
23:25 - 23:28Again, they have the same fundamental abstractions. You know how to work with a
-
23:28 - 23:33stack, so you know how to use the STL stack with a little extra syntax.
-
23:33 - 23:34The big thing I will say up front,
-
23:34 - 23:36the emphasis in the STL
-
23:36 - 23:37is on speed.
-
23:37 - 23:40In fact, they are designed to be really, really fast.
-
23:40 - 23:45That means their notion of safety is, good one.
-
23:45 - 23:48So what this means is you'll have to do your own bounce checking.
-
23:48 - 23:50When you're iterating, you'll have to make sure you don't walk off the edge of your
-
23:50 - 23:51container
-
23:51 - 23:52and stuff like that.
-
23:52 - 23:56One of the major reasons we didn't go over the STL in this class
-
23:56 - 23:57is that,
-
23:57 - 24:00which is if you're trying to figure out how to work with a vector, it's
-
24:00 - 24:03really bad if you also have to worry about doing your own bounce checking.
-
24:03 - 24:05If you're working with a map and you have to use the STL map,
-
24:05 - 24:07you probably wouldn't ever want to use another map
-
24:07 - 24:09for the rest of your life. It's pretty complicated.
-
24:09 - 24:12The point is this is what you'll see in the professional environment because it's a very good library, and it's very
-
24:12 - 24:16fast.
-
24:16 - 24:19I want to give you a quick example of what this might look like.
-
24:19 - 24:22This is a program I've written using the CS106 vector. I want to go
-
24:22 - 24:24change it to use the STL to show you
-
24:24 - 24:28it's the same thing with a little bit different syntax.
-
24:28 - 24:32The first thing you have to do is say, the Specter.H we gave you, so you have
-
24:32 - 24:33to change that to the standard vector,
-
24:33 - 24:36which is vector in brackets.
-
24:36 - 24:40Number of characters we have changed so far. Three characters.
-
24:40 - 24:44Next thing, the big vector becomes little vector. Four characters.
-
24:44 - 24:48The only big difference is this add function. Anyone who isn't in
-
24:48 - 24:50my class want to guess
-
24:50 - 24:54what you would call the function to append something to the end of a vector? Anyone
-
24:54 - 24:57want to take a guess? [Inaudible].
-
24:57 - 24:58Append? That would
-
24:58 - 25:02be nice. It's actually called push back.
-
25:02 - 25:05There's good reason for this. It means all these different containers
-
25:05 - 25:08have similar function names. Push back,
-
25:08 - 25:09append, potato,
-
25:09 - 25:12potato.
-
25:12 - 25:15Okay. That's probably about ten or eleven character changes. Then
-
25:15 - 25:16to loop over the thing,
-
25:16 - 25:18it's exactly what you've seen before.
-
25:18 - 25:19There is a size function.
-
25:19 - 25:23There are brackets. You can go and iterate. You can go and access things.
-
25:23 - 25:26The result, as you can see, is not that different.
-
25:26 - 25:28There are a couple subtleties that I didn't go into here about how the
-
25:28 - 25:32STL behaves different from the vector you've seen, but overall, it
-
25:32 - 25:36is the same container.
-
25:36 - 25:39The other one I want to call your attention to is the STL map.
-
25:39 - 25:42So if you work with the STL, you need to know two containers, and the rest
-
25:42 - 25:44follow from there. If you know vector and you know map,
-
25:44 - 25:46you've got the whole thing down.
-
25:46 - 25:50The STL map is like our map except that instead of going string to some value, it's
-
25:50 - 25:51any key type you want
-
25:51 - 25:53to any value type you want.
-
25:53 - 25:56So you can map strings to ints, you can map ints to strings,
-
25:56 - 26:00you can map stack of queue of int to vector of double.
-
26:00 - 26:04I don't know why you would, but you have the ability to do so.
-
26:04 - 26:07The problem is that the interface on map
-
26:07 - 26:11was probably designed by someone who didn't like people. It's really hard
-
26:11 - 26:14to use. Anyone who has used it will tell you that. Once you
-
26:14 - 26:17get the hang of it, though, it's pretty intuitive. The result is you have a
-
26:17 - 26:22very fast map container that is going to be the backbone of any of the programs that you write.
-
26:22 - 26:23It's quite a useful class.
-
26:23 - 26:26Again, I don't expect you to memorize every, single bit of syntax here,
-
26:26 - 26:27everything I'm saying.
-
26:27 - 26:29Just keep it in the back of the mind if this is what you want to look into.
-
26:29 - 26:33The map you do need to know. Go take a look at that one if you want to
-
26:33 - 26:36pursue C++. Now
-
26:36 - 26:37iterators.
-
26:37 - 26:40So the iterators you've seen in this class are very nice. They're well-behaved.
-
26:40 - 26:44Has next,
-
26:44 - 26:45next. Has next,
-
26:45 - 26:47next. I'm done. [Inaudible]
-
26:47 - 26:50iterators are designed to look like pointers.
-
26:50 - 26:53You might wonder why anyone would voluntarily make something look like a
-
26:53 - 26:54pointer. There's a very good reason.
-
26:54 - 26:58This means they're about as smart as a pointer, which means they know
-
26:58 - 26:59nothing.
-
26:59 - 27:02So if you want to iterate over a range, you have to have two different iterators. You have
-
27:02 - 27:03to say start here,
-
27:03 - 27:04stop there,
-
27:04 - 27:07keep walking forward until you get there.
-
27:07 - 27:08Admittedly,
-
27:08 - 27:10it's more work,
-
27:10 - 27:12but it's very useful because it means that
-
27:12 - 27:15if you want to iterate over a 20-element slice out of a 400
-
27:15 - 27:17million element container,
-
27:17 - 27:19you can do that. Try doing that with our libraries. You have to go iterate all the
-
27:19 - 27:21way up the start,
-
27:21 - 27:23then keep iterating more.
-
27:23 - 27:26The other thing you need to know about the iterators is that they're read/write.
-
27:26 - 27:30So you can actually crawl over a container and update the values with an iterator,
-
27:30 - 27:32something that you cannot do with our libraries.
-
27:32 - 27:35It's a bit more work. The syntax basically looks like pointer reading and
-
27:35 - 27:35writing,
-
27:35 - 27:40but the result is that they're much more powerful, and they're a lot more flexible.
-
27:40 - 27:43Here's a quick example.
-
27:43 - 27:46I just wrote some code here that uses a vector iterator. The thing to take a look at is this
-
27:46 - 27:49right here.
-
27:49 - 27:53So basically, there's a couple parts to point out. I want to show you this so that if you
-
27:53 - 27:54see this later on,
-
27:54 - 27:57you'll get it. Can everybody see this?
-
27:57 - 27:59The first thing is this begin function.
-
27:59 - 28:03We call it iterator, they call it begin. It just says, give me a start iterator.
-
28:03 - 28:07Since we need two iterators to define a range, we keep going until we hit this iterator called
-
28:07 - 28:09N, which means stop. To
-
28:09 - 28:13move the iterator forward, has next and next would be too nice, so it's ++,
-
28:13 - 28:15and then to read from an iterator or write to it, you just de-reference
-
28:15 - 28:17it.
-
28:17 - 28:20So you do star iterator, as if it's a pointer, which in many cases, it
-
28:20 - 28:22actually is.
-
28:22 - 28:24That's the gist of what this looks like. There's a few more nuances
-
28:24 - 28:25and subtleties,
-
28:25 - 28:28but if you keep in mind that if you're looking at STL stuff, just treat this
-
28:28 - 28:34like pointers, it will make a lot of sense. The
-
28:34 - 28:37last thing I want to talk about are algorithms. These things are
-
28:37 - 28:40amazing. The idea is that everything's got iterators,
-
28:40 - 28:42so if you write functions that work on iterators, they will work on any
-
28:42 - 28:43container type.
-
28:43 - 28:47So if you want a binary search or a vector, you call binary search.
-
28:47 - 28:51It's a prewritten function. You want to sort something? Great. Go call sort.
-
28:51 - 28:55You guys have seen permutations before, right? You have to go do recursion pulsing in the
-
28:55 - 28:58front, permute the rest, stick it back on, that sort of stuff?
-
28:58 - 29:00STL, you can do it in a while loop.
-
29:00 - 29:02You can while you next permutation something,
-
29:02 - 29:05come up with every, single permutation, and you don't have to write a single recursive
-
29:05 - 29:07function ever.
-
29:07 - 29:10These sorts of things, I think there's 75 different algorithms, anything
-
29:10 - 29:12from permuting to searching to sorting,
-
29:12 - 29:15to transforming to rearranging to sorting to splitting.
-
29:15 - 29:18You name it, there's probably an algorithm that does it. If
-
29:18 - 29:19you play around with the STL,
-
29:19 - 29:26you get to see all this stuff. It's a lot of fun.
-
29:26 - 29:29I've just been showing you some of the libraries. That's basically all the library changes you need to
-
29:29 - 29:29know.
-
29:29 - 29:32Basically, everything you want to do is still there.
-
29:32 - 29:35The syntax is a little different. Some of the libraries you'll have to write yourself, but it's
-
29:35 - 29:39all there. It's not like you have to relearn everything from scratch.
-
29:39 - 29:42But the libraries are only as good as the language is,
-
29:42 - 29:45and there are a couple of language features of C++ that we didn't really talk about
-
29:45 - 29:48in this class. I want to go over some of them because you will see them a lot in
-
29:48 - 29:50professional code.
-
29:50 - 29:53After all, you cannot use a library any more than you understand how the
-
29:53 - 29:55language works. So getting a rough understanding of what this will do
-
29:55 - 29:59will make your life easier in the long
-
29:59 - 30:01run. The first thing I want to talk about is
-
30:01 - 30:06const. You've seen const before in the context of const and my context equals five.
-
30:06 - 30:10Just make a global constant. Saying that const makes global constants is
-
30:10 - 30:13like saying a computer is something that adds numbers.
-
30:13 - 30:13Yes,
-
30:13 - 30:17but they can also run Windows, for example.
-
30:17 - 30:21Const shows up just about everywhere. It's the little keyword that could.
-
30:21 - 30:24You'd be amazed just how useful this little keyword can be.
-
30:24 - 30:26The biggest thing that it does
-
30:26 - 30:29is helps protect you against your own mistakes.
-
30:29 - 30:32If you're writing a program and you know something shouldn't change, tag
-
30:32 - 30:33it const
-
30:33 - 30:36because that way, later down the lie, if you do accidentally change it, it says,
-
30:36 - 30:39that's a mistake. Compiler error. You screwed up.
-
30:39 - 30:42It protects you. That should've been a double equal, not
-
30:42 - 30:44a single equal,
-
30:44 - 30:46those sorts of things.
-
30:46 - 30:49You've all probably seen that the C++ compiler doesn't treat anything as sacred. Like, oh, you don't
-
30:49 - 30:51want to return
-
30:51 - 30:56something? Or something like, oh, wow, you did single equal instead of double equal.
-
30:56 - 30:57You're the boss.
-
30:57 - 31:00The second you try to break const, it pulls out its big ruler of judgment and
-
31:00 - 31:04whacks you on the wrist with it, like shame on your for breaking this constants. It
-
31:04 - 31:06is the compiler trying to help you out.
-
31:06 - 31:08Please know about this keyword. I'll give you
-
31:08 - 31:11an example. See
-
31:11 - 31:15this function prototype? Void do something takes a vector by reference.
-
31:15 - 31:18So I've passed my vector into this function, and the question is, what's it going to
-
31:18 - 31:20hold when it comes back?
-
31:20 - 31:22Well, it could be the same. It
-
31:22 - 31:25could be completely empty. There could be 137 copies of the
-
31:25 - 31:26number 137.
-
31:26 - 31:28You don't know.
-
31:28 - 31:31In this class, we've shown you, pass by reference can be either for
-
31:31 - 31:33efficiency reasons because it's not fast enough,
-
31:33 - 31:35or it can be
-
31:35 - 31:37because you want to actually change the thing.
-
31:37 - 31:40It's difficult to see what this function does because you have no idea
-
31:40 - 31:43which of those two it is.
-
31:43 - 31:46The idea, though, and this is something that you will see in professional code everywhere,
-
31:46 - 31:48is doing something like this.
-
31:48 - 31:51If I say, void do something const
-
31:51 - 31:53vector and my vector,
-
31:53 - 31:56that says that this vector can't be modified within the function.
-
31:56 - 32:00It's like handing this thing off saying, I'm giving you this value
-
32:00 - 32:01for efficiency reasons.
-
32:01 - 32:04Donft try to modify it. In fact, you can't modify it.
-
32:04 - 32:07It makes your code self-documenting. Someone looking at your functions can go,
-
32:07 - 32:09that's const. I can't possibly change anything there,
-
32:09 - 32:13and they'll be totally fine handing off the string containing their graduate thesis
-
32:13 - 32:14to it, for example.
-
32:14 - 32:18If you had your graduate thesis stored as a string,
-
32:18 - 32:19which I don't
-
32:19 - 32:20know why you would ever do this,
-
32:20 - 32:24but if you see a function take the string and parameter, you'd probably be a little
-
32:24 - 32:27bit worried. Like, is it going to replace my graduate thesis with,
-
32:27 - 32:30hi, I don't have a graduate thesis or what?
-
32:30 - 32:34The const is useful.
-
32:34 - 32:36The question is, if it's such a useful keyword,
-
32:36 - 32:39why didn't we show you this?
-
32:39 - 32:43The biggest reason is you can't just use const every now and then. You can't say, I'm going
-
32:43 - 32:47to mark this parameter const. I'm gong to say that one's const right there. It
-
32:47 - 32:48doesn't work that way.
-
32:48 - 32:52Let's say I do mark something const. That object has to know how to behave
-
32:52 - 32:53when it can't modify itself.
-
32:53 - 32:56So you have to make the entire class written const correct.
-
32:56 - 32:59Then if it has any data members that are classes, that class has to be const correct. You get this
-
32:59 - 33:00
-
33:00 - 33:01exploding effect where
-
33:01 - 33:05you stick a singe const in, and suddenly every, single piece of your code is marked
-
33:05 - 33:06const.
-
33:06 - 33:09That's a good thing because it makes your code self-documenting,
-
33:09 - 33:13but the point is it's an unnecessary language complication. When
-
33:13 - 33:16you're trying to write the PQ class, you should be worried, how do I build a
-
33:16 - 33:17chunk list, not oh,
-
33:17 - 33:20is this function const? So in the
-
33:20 - 33:23professional world, you will see it. Here we thought, it makes things too
-
33:23 - 33:28complicated. We'll give it a pass for right
-
33:28 - 33:30now. Another big one. Object copying an assignment.
-
33:30 - 33:34You've seen disallow copy. It says, don't copy this
-
33:34 - 33:38object. It's not standard C++, but I've been told that Google actually
-
33:38 - 33:41has a macro that does something just like this, so you're programming the same way Google
-
33:41 - 33:42people do.
-
33:42 - 33:43The difference is that
-
33:43 - 33:45if you're trying to write a PQ
-
33:45 - 33:47and it can't copy itself,
-
33:47 - 33:51it's a little bit frustrating. Think about it. You make some PQ. You can make a vector
-
33:51 - 33:51copy.
-
33:51 - 33:52You can make a map copy.
-
33:52 - 33:56If you can't make a PQ copy, you'll be wondering, why not?
-
33:56 - 33:59So it turns out C++ lets you redefine the way copying
-
33:59 - 34:01works. So these two functions here
-
34:01 - 34:04called copy constructors and assignment operators.
-
34:04 - 34:07While it's very useful to know how to do this, there's a major reason we didn't tell
-
34:07 - 34:08you about it in this class.
-
34:08 - 34:12It's because it's really hard.
-
34:12 - 34:16I think I spent, in 106L, an entire week going over this.
-
34:16 - 34:18We don't have a week to tell you how to do this.
-
34:18 - 34:21Here's a list of some of the things you have to keep in mind when writing these
-
34:21 - 34:21functions.
-
34:21 - 34:25[Inaudible] to clean up your own memory, not memory you don't own. To clean
-
34:25 - 34:28up the memory, to copy an object, to copy the object correctly, to handle
-
34:28 - 34:31these [inaudible], to follow the same rules that C++ syntax dictates.
-
34:31 - 34:34There's an awful lot of stuff going on there.
-
34:34 - 34:37To expect you to get all this right when you're worrying about your PQ is just
-
34:37 - 34:37too much.
-
34:37 - 34:40It really is.
-
34:40 - 34:41Imagine it's the day before it's due.
-
34:41 - 34:43You've got the PQ working beautifully, and
-
34:43 - 34:45it doesn't copy
-
34:45 - 34:49right. What does that tell you? You've written your copy function wrong, but you totally
-
34:49 - 34:50understand and made your point,
-
34:50 - 34:52which is how to build a priority queue.
-
34:52 - 34:54This is a chunk list. This is a heap.
-
34:54 - 34:55This is an unsorted
-
34:55 - 34:58vector. That's what's actually important, not building stuff like this.
-
34:58 - 35:00When you're in the professional world,
-
35:00 - 35:03you do need to do some extra work to make your objects copy
-
35:03 - 35:07correctly. So you should look into these functions a little bit.
-
35:07 - 35:08Again, to stress,
-
35:08 - 35:11the stuff you've learned in here, the actual here's how you build these data
-
35:11 - 35:14structures, is more important than these global syntactic nuances that you've got to be
-
35:14 - 35:18aware of.
-
35:18 - 35:20One more thing. This is pretty cool.
-
35:20 - 35:22I have this
-
35:22 - 35:23code snippet right here.
-
35:23 - 35:24I make a string,
-
35:24 - 35:25and I say,
-
35:25 - 35:30my string is equal to this is, and then I tack onto it, a string.
-
35:30 - 35:32I'm going to iterate over this entire string,
-
35:32 - 35:35and every step, I'm going to print out the current character. So
-
35:35 - 35:37this is just print out the string
-
35:37 - 35:39one letter at a time. Ifm going to
-
35:39 - 35:42highlight a few things.
-
35:42 - 35:45Why is it legal to [inaudible] equals a string with something else?
-
35:45 - 35:48Why string but not priority queue? Why are we allow to do this?
-
35:48 - 35:50It's a string. It's an
-
35:50 - 35:54object, and we just code plus equals on it.
-
35:54 - 35:56Why does it let you do less than, less than?
-
35:56 - 35:59What does that mean? Why do streams let you do that but nothing else? Why can
-
35:59 - 36:03I read a string with brackets even though it's really an object?
-
36:03 - 36:07At a high level, we all know what it means to add something to a string. It
-
36:07 - 36:11means take something and stick it on. You know what this less than, less than. It means put into
-
36:11 - 36:14a stream.
-
36:14 - 36:18[Inaudible] C++, I want to define these operators for my classes.
-
36:18 - 36:21It's a technique known as operator overloading.
-
36:21 - 36:25Basically, all you do is write functions that are called operator and
-
36:25 - 36:28whatever the name of your operator is. So operator equals, operator brackets, operator
-
36:28 - 36:29less than, less than.
-
36:29 - 36:30Operator plus, plus.
-
36:30 - 36:34It's just a syntax convenience. You can write code that looks more
-
36:34 - 36:34intuitive
-
36:34 - 36:37that does something complex behind the scenes. For
-
36:37 - 36:41example, if I write my vector bracket I, it's not like, oh, it's a bracket. It's much
-
36:41 - 36:43faster. It really means, call the function called
-
36:43 - 36:45operator brackets.
-
36:45 - 36:49Again, it's a convenience and nothing else. If you treat it as anything more than a
-
36:49 - 36:51convenience, you can kind of hurt yourself with it.
-
36:51 - 36:54You can overload basically every operator in C++. You can
-
36:54 - 36:58overload things like bit wise [inaudible] with assignment. You can overload the
-
36:58 - 37:01exore operator. You can overload parenthesis, and, comma,
-
37:01 - 37:03all these operators that most people never use.
-
37:03 - 37:06The ones that you see most frequently, though, are these
-
37:06 - 37:09ones. Overloading operator less than, less than for stream insertion.
-
37:09 - 37:12You can actually make it so that you can make a vector class than
-
37:12 - 37:15can print itself out to the screen. It's kind
-
37:15 - 37:17of useful. The assignment operator, operator equals,
-
37:17 - 37:20the less than operator for comparing things and the parentheses operator.
-
37:20 - 37:23You can actually overload parentheses. It's pretty cool stuff. It's
-
37:23 - 37:27actually for something called functors, which is really awesome. If you do play around with
-
37:27 - 37:30C++, this is definitely something to keep in mind because it will make your
-
37:30 - 37:33life a lot more enjoyable.
-
37:33 - 37:36I think more than any other language features, C++, except possibly
-
37:36 - 37:40multiple inherence, this is the most widely-criticized
-
37:40 - 37:41feature of the language.
-
37:41 - 37:44The reason is that you can make things that make no sense legal.
-
37:44 - 37:47Like, defining the modular operator for two
-
37:47 - 37:50PQ. My PQ, my other PQ. Anybody think
-
37:50 - 37:54of a sensible interpretation of what it means to mod one PQ by
-
37:54 - 37:57another? If you do, please send me an email or
-
37:57 - 37:59shout it out right now. I can't think of one.
-
37:59 - 38:01But you can make it legal.
-
38:01 - 38:04I don't know why you would, but you can.
-
38:04 - 38:05The point of this is, if you think
-
38:05 - 38:07about it, call it the philosophy. Let
-
38:07 - 38:11the programmer make the choice, even if it lets them choose wrong.
-
38:11 - 38:14You can do this. You probably shouldn't do it, but
-
38:14 - 38:16by giving you the opportunity to do so,
-
38:16 - 38:19it means that when you really do need to write a class that has a module
-
38:19 - 38:21that's defined, you can do it.
-
38:21 - 38:22It's flexibility. It
-
38:22 - 38:23means that you have to
-
38:23 - 38:30make sure that what you're doing makes sense, but it's flexibility. You've
-
38:30 - 38:32all just finished Pathfinder, right?
-
38:32 - 38:37Did anybody templatize the [inaudible] PQ, by any chance? Anyone? You guys
-
38:37 - 38:39are smart.
-
38:39 - 38:44Here's the thing. Let's say I wrote something that says, A equals B.
-
38:44 - 38:47What does this mean? It says assign B
-
38:47 - 38:49A. What if I write this in a template function?
-
38:49 - 38:53I have a template of some unknown type. I'll just say A equals B.
-
38:53 - 38:54What's it mean?
-
38:54 - 38:58Well, if it's an int, it means take the int and copy it. If it's a floating point
-
38:58 - 39:01number, it says take the floating point number and copy it. If
-
39:01 - 39:04it's a string, it says make a string deep copy.
-
39:04 - 39:08The point is that every, single time you use this same syntax, A equals B,
-
39:08 - 39:11but the result is different, and it's always the correct result.
-
39:11 - 39:15This is what you can get with operator [inaudible], the ability to take code
-
39:15 - 39:19and make it look right for every, single class so that in templates, you can just
-
39:19 - 39:23call the function, and you're guaranteed it will work.
-
39:23 - 39:26That's a quick tour of some of the language features. I know this
-
39:26 - 39:30might seem like an awful lot. I've been going very quickly through it,
-
39:30 - 39:33but want to conclude by saying so what?
-
39:33 - 39:37I just harangued you with a lot of language features, a lot of syntax, a lot of
-
39:37 - 39:37libraries.
-
39:37 - 39:39What's the point?
-
39:39 - 39:41The point to take out of this is that
-
39:41 - 39:43C++ is big,
-
39:43 - 39:46but it's also a very powerful language, and it's a very expressive language. I
-
39:46 - 39:48think it's a very beautiful language.
-
39:48 - 39:51The features that you've learned in this class will go anywhere you want them to.
-
39:51 - 39:54If you want to take your coding skills
-
39:54 - 39:55and apply
-
39:55 - 39:57them to C++, you can,
-
39:57 - 39:58and you have this flexibility.
-
39:58 - 40:00You really have this gift.
-
40:00 - 40:03What you've learned in here is something most people never learn. It's how to make
-
40:03 - 40:05a computer solve a problem for you.
-
40:05 - 40:07That's a skill that's going to follow you for the rest of your life, no matter where
-
40:07 - 40:09you take it.
-
40:09 - 40:12If you're interested in C++, I have a huge number of references here, if you
-
40:12 - 40:13want to go see them.
-
40:13 - 40:16The point is that if you want to solve a problem with a computer, you need three things.
-
40:16 - 40:19First,
-
40:19 - 40:20programming skills.
-
40:20 - 40:21You all have that.
-
40:21 - 40:23You need a good idea.
-
40:23 - 40:26I don't think I have any good ideas for programming. If I did, I'd probably do them myself,
-
40:26 - 40:29but if you have a good idea, and you want to make $1 billion with it, the
-
40:29 - 40:32last thing you need is a programming language.
-
40:32 - 40:35Hopefully this quick tour of C++ has showed you. This
-
40:35 - 40:38is what this language is. It's a tool for solving real problems
-
40:38 - 40:40that trusts you
-
40:40 - 40:41and that means that overall, you'll
-
40:41 - 40:43have fun doing it.
-
40:43 - 40:45So if you want to go and run with this, if you want to say, I'm a good programmer.
-
40:45 - 40:46I can do this and
-
40:46 - 40:49learn C++. I think you will love it. I think you will enjoy it. I think it will
-
40:49 - 40:53be some of the most fun you will have sitting in front of a computer.
-
40:53 - 40:56So have fun with this stuff. That's the whole point. If you're in this class, I hope
-
40:56 - 41:00you enjoy it. I hope you said, yeah, I can make a computer solve something. I can do graph
-
41:00 - 41:01algorithms. I can do graph [inaudible].
-
41:01 - 41:04I can make data structures. I can make a computer program that plays Boggle
-
41:04 - 41:08better than I ever could or anyone I know ever can.
-
41:08 - 41:10Have fun. That's the whole point of this.
-
41:10 - 41:14If you want to do it in C++, come talk to me. I will give you some references.
-
41:14 - 41:17Enjoy. Good luck on the final, and I will probably see you around on Friday for the final.
-
41:17 - 41:18Enjoy.
- Title:
- Lecture 27 | Programming Abstractions (Stanford)
- Description:
-
Lecture 27 by Keith (for Julie Zelenski)--a section leader and the instructor of CS 106L--for the Programming Abstractions Course (CS106B) in the Stanford Computer Science Department.
In the final lecture, Keith talks about the C++ programming language. He starts of with C++ history, C++ without CS 106 and what comes next.
Complete Playlist for the Course:
http://www.youtube.com/view_play_list?p=FE6E58F856038C69CS 106B Course Website:
http://cs106b.stanford.eduStanford Center for Professional Development:
http://scpd.stanford.edu/Stanford University:
http://www.stanford.edu/Stanford University Channel on YouTube:
http://www.youtube.com/stanforduniversity/ - Video Language:
- English
- Duration:
- 41:34