< Return to Video

Lecture 27 | Programming Abstractions (Stanford)

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

CS 106B Course Website:
http://cs106b.stanford.edu

Stanford Center for Professional Development:
http://scpd.stanford.edu/

Stanford University:
http://www.stanford.edu/

Stanford University Channel on YouTube:
http://www.youtube.com/stanforduniversity/

more » « less
Video Language:
English
Duration:
41:34
N. Ueda added a translation

English subtitles

Revisions