< Return to Video

Lecture 2 | Programming Abstractions (Stanford)

  • 0:00 - 0:10
  • 0:10 - 0:14
    This presentation is delivered by Stanford Center for Professional
  • 0:14 - 0:23
    Development.
  • 0:23 - 0:25
    C++ and Java. This is
  • 0:25 - 0:28
    Java. It's like a death
  • 0:28 - 0:31
    match. What's the same? The first thing I want to talk about is that there are a lot of things
  • 0:31 - 0:33
    that are the same,
  • 0:33 - 0:34
    and so
  • 0:34 - 0:37
    you are building on a background that is going to serve you well
  • 0:37 - 0:39
    without having to make a lot of
  • 0:39 - 0:42
    adjustments. They're almost so similar that at times, it can be a little
  • 0:42 - 0:45
    bit unhelpful in the sense that it's easy to get tripped up because they
  • 0:45 - 0:48
    almost but not quite are exactly the same. There was one unfortunate quarter
  • 0:48 - 0:52
    where I was teaching two classes ? one that was being taught in C++ and one that was in
  • 0:52 - 0:53
    Java, and I was a mess the whole
  • 0:53 - 0:57
    quarter.
  • 0:57 - 1:00
    Luckily, I'm not doing that now, so hopefully, I'll have my C++ hat on
  • 1:00 - 1:01
    straight the whole quarter.
  • 1:01 - 1:05
    The general syntax ? I'm going to mention these things, and then I'm going to show
  • 1:05 - 1:07
    you a sample program.
  • 1:07 - 1:11
    Things like the comment sequence, the slash star or the slash slash
  • 1:11 - 1:11
    ?
  • 1:11 - 1:15
    the use of almost all the punctuation characters
  • 1:15 - 1:16
  • 1:16 - 1:20
    is exactly the same. The statement terminator that the
  • 1:20 - 1:22
    semicolon is used for, the commas to separate arguments going into a function
  • 1:22 - 1:23
    call,
  • 1:23 - 1:26
    the way you make variable and parameter declarations ?
  • 1:26 - 1:28
    exactly the same.
  • 1:28 - 1:30
  • 1:30 - 1:34
    All of the primitive variable types ? char, int, double, plus the kind of more esoteric
  • 1:34 - 1:36
    log and float and short and things like that
  • 1:36 - 1:39
    that are present in Java are present in C++ and have
  • 1:39 - 1:41
    basically the same semantics.
  • 1:41 - 1:42
    There is one
  • 1:42 - 1:47
    minor adjustment there, which is Java's Boolean type, the true/false variable type is actually
  • 1:47 - 1:50
    called Bool in C++, so you
  • 1:50 - 1:52
    have to type a few less characters
  • 1:52 - 1:55
    on that end.
  • 1:55 - 1:59
    All of the operators that manipulate the basic types, the assignment operator [inaudible] the
  • 1:59 - 2:01
    equal sign, the
  • 2:01 - 2:04
    equals equals to compare, not equals to see if they're not equal, the arithmetic
  • 2:04 - 2:10
    plus, the plus equal, multiply equal shorthands, the plus plus that
  • 2:10 - 2:12
    comes in both pre and post increment,
  • 2:12 - 2:15
    the relational less than, less than or equal to,
  • 2:15 - 2:18
    the logical operators and/or have the same behaviors. They have the same short
  • 2:18 - 2:23
    circuiting, the same precedence table, so all of that is
  • 2:23 - 2:25
    without change.
  • 2:25 - 2:29
    The control structure ? so for redirecting control flow and making choices about what
  • 2:29 - 2:30
    code to execute when,
  • 2:30 - 2:35
    the floor and [inaudible] mechanisms, the if with the optional else, the switch
  • 2:35 - 2:36
    statement, which you may or may not have
  • 2:36 - 2:39
    seen, but you can definitely look up in the text if you haven't ? that
  • 2:39 - 2:42
    kind of has a
  • 2:42 - 2:42
  • 2:42 - 2:45
    variant of the if else, so it lets you decide based on the value of something which case
  • 2:45 - 2:46
    to move into,
  • 2:46 - 2:49
    the return that's used for the function ? also
  • 2:49 - 2:54
    things like the break and continue, a little less known keywords there or the
  • 2:54 - 2:57
    do while ? they work the same way
  • 2:57 - 2:59
  • 2:59 - 3:00
  • 3:00 - 3:01
  • 3:01 - 3:04
    in Java and C++.
  • 3:04 - 3:04
    Let's
  • 3:04 - 3:06
    look at a complete C++ program.
  • 3:06 - 3:09
    This one is printed in handout four, if you want to follow along there and
  • 3:09 - 3:12
    make notes on it as we talk you through.
  • 3:12 - 3:14
    What I'm going to do here is I'm actually planning on just
  • 3:14 - 3:16
    walking through this line by line
  • 3:16 - 3:19
    to kind of point out some of the things that are different. Then I'm going to move to the
  • 3:19 - 3:22
    compiler, and we'll do a little bit of experimentation with developing and
  • 3:22 - 3:25
    writing code and manipulating something that looks like this to
  • 3:25 - 3:26
    get some
  • 3:26 - 3:28
    of our bearings about what
  • 3:28 - 3:30
    things are going on.
  • 3:30 - 3:32
    First off,
  • 3:32 - 3:34
    just some general structure.
  • 3:34 - 3:35
    A C++ program
  • 3:35 - 3:39
    is traditionally written in a text file with a suffix some name.CPP. There's actually
  • 3:39 - 3:40
  • 3:40 - 3:45
    some other names that get used like .CC or .C
  • 3:45 - 3:47
    that are also accepted by a lot of compilers.
  • 3:47 - 3:51
    Some text extension that identifies that this file has
  • 3:51 - 3:54
    C++ code ? we'll use CPP.
  • 3:54 - 3:58
    The program is traditionally in a simple form just
  • 3:58 - 4:03
    one file with all of the code that's necessary to be in there. Let's take a
  • 4:03 - 4:04
    look at it.
  • 4:04 - 4:07
    This one starts with a comment ? always a good idea.
  • 4:07 - 4:10
    This
  • 4:10 - 4:14
    is average.CPP, and it adds scores and prints their average.
  • 4:14 - 4:18
    It's just like all the good style habits that you have
  • 4:18 - 4:20
    in Java.
  • 4:20 - 4:24
  • 4:24 - 4:27
    The next three lines are #include statements.
  • 4:27 - 4:30
    #include is analogous to the Java import.
  • 4:30 - 4:32
    The C++ compiler
  • 4:32 - 4:34
    is pretty rigid about
  • 4:34 - 4:37
    wanting to see things in the proper order.
  • 4:37 - 4:40
    Before you start using some facility, it wants to know about it.
  • 4:40 - 4:43
    That's part of the safety of making sure you're using it correctly. If you
  • 4:43 - 4:46
    plan on drawing something in the screen and doing some graphics,
  • 4:46 - 4:49
    it wants to be sure you're making calls to the graphics function correctly with
  • 4:49 - 4:51
    the right arguments, the right names, the right
  • 4:51 - 4:53
    arrangements,
  • 4:53 - 4:56
    and so in order to know that, it has to ahead of time have seen
  • 4:56 - 5:00
    that bit of structure. The way this is done in C++ is
  • 5:00 - 5:03
    through these interface or include files.
  • 5:03 - 5:06
    The #include mechanism is fairly primitive.
  • 5:06 - 5:10
    It basically says look for a file with the name genlib.h and take
  • 5:10 - 5:12
    its contents and dump them right here.
  • 5:12 - 5:16
    The contents of genlib.h and simpio.h
  • 5:16 - 5:18
    are actually to give you
  • 5:18 - 5:21
    a little insight. We're going to see them more later.
  • 5:21 - 5:24
    It lists the operations and facilities that are available in this
  • 5:24 - 5:28
    particular header that we're trying to include. In this case, the genlib is a 106
  • 5:28 - 5:29
    specific header
  • 5:29 - 5:33
    that all our programs will include. It gets us set up with some basic
  • 5:33 - 5:34
    foundations. The simpio
  • 5:34 - 5:37
    is our simplified IO header
  • 5:37 - 5:41
    that adds some features for interacting with the user at the console.
  • 5:41 - 5:44
    The next one down there is include . is a C++
  • 5:44 - 5:46
    standard header,
  • 5:46 - 5:47
    and that
  • 5:47 - 5:50
    helps to point out the difference between why in one place I was using
  • 5:50 - 5:54
    double quotes to name the header file I'm looking for and in one place I'm using
  • 5:54 - 5:55
    angle brackets.
  • 5:55 - 5:56
    This is a
  • 5:56 - 6:01
    way that's distinguished for what header files are, what are called system standard, and
  • 6:01 - 6:04
    it looks for them in a particular place where the standard header files are. Those are
  • 6:04 - 6:06
    always enclosed in the angle brackets.
  • 6:06 - 6:10
    In double quotes are local headers that are specific to this
  • 6:10 - 6:13
    project or this environment that are not C++ standard.
  • 6:13 - 6:16
    Whenever you see these double quotes, you'll think it's my own local headers or headers
  • 6:16 - 6:18
    from the CS106 library.
  • 6:18 - 6:23
    Anything in angle braces is a system standard header you'd find everywhere.
  • 6:23 - 6:26
    It matches what you do in import. You say I'm going to be using these
  • 6:26 - 6:29
    features in this class. Let me import its features into here
  • 6:29 - 6:34
    so that the compiler knows about them.
  • 6:34 - 6:36
    The next line I've got here
  • 6:36 - 6:39
    is a declaration of a constant.
  • 6:39 - 6:40
    This is the const
  • 6:40 - 6:42
    whose name is NumScores,
  • 6:42 - 6:44
    and so it looks a little bit like a variable declaration.
  • 6:44 - 6:48
    From this point over, you see I have int NumScores = 4;.
  • 6:48 - 6:50
    It looks a lot like a variable operation.
  • 6:50 - 6:52
    The const in the front of it
  • 6:52 - 6:55
    is our way of telling the complier that this particular value
  • 6:55 - 6:58
    once assigned will not change.
  • 6:58 - 6:59
    It is
  • 6:59 - 7:03
    equivalent to kind of the use of final in the Java language
  • 7:03 - 7:04
    in many ways.
  • 7:04 - 7:05
  • 7:05 - 7:07
    This is being declared
  • 7:07 - 7:11
    outside of context. One point to make here is that
  • 7:11 - 7:13
    C++
  • 7:13 - 7:15
    drives on more than one
  • 7:15 - 7:19
    way of expressing code and what we call its paradigm. Java is the completely
  • 7:19 - 7:21
    object oriented language.
  • 7:21 - 7:24
    Every line of code you wrote went inside a class. It's all about classes. You wrote this
  • 7:24 - 7:27
    class that manipulated the thermometer. You wrote this class that
  • 7:27 - 7:29
    drew with the
  • 7:29 - 7:33
    turtle. There was no code that ever existed outside of a class.
  • 7:33 - 7:36
    You started off with public class something and you started writing methods
  • 7:36 - 7:39
    that operated on that class. Maybe they were static or maybe not.
  • 7:39 - 7:43
    The way you started code was always to say start from this class'
  • 7:43 - 7:46
    main to get work done.
  • 7:46 - 7:48
    C++ is a hybrid language.
  • 7:48 - 7:51
    It has object oriented features. It has the ability to find and use classes, and we'll
  • 7:51 - 7:52
    see that
  • 7:52 - 7:54
    quite extensively,
  • 7:54 - 7:55
    but it also
  • 7:55 - 8:00
    inherits the legacy of C, which is pre this whole object oriented
  • 8:00 - 8:00
    development
  • 8:00 - 8:04
    where it operates more procedurally, they call
  • 8:04 - 8:05
    it,
  • 8:05 - 8:09
    where there are functions that exist at the global level
  • 8:09 - 8:09
    that
  • 8:09 - 8:13
    don't have a context of a class that they operate in. There's not some [inaudible]
  • 8:13 - 8:16
    that's being received. They're just functions that
  • 8:16 - 8:17
    exist outside. It's a little
  • 8:17 - 8:19
    set of things to do
  • 8:19 - 8:21
    that don't live inside a class.
  • 8:21 - 8:25
    For much of the term, we're going to be doing stuff in a very procedural way,
  • 8:25 - 8:26
    at least from the code we write,
  • 8:26 - 8:29
    and we will use a lot of objects. We'll actually be
  • 8:29 - 8:30
  • 8:30 - 8:33
    mixing and matching a little bit of those paradigms together, so using the
  • 8:33 - 8:37
    procedural paradigm to write a lot of code that happens to manipulate objects
  • 8:37 - 8:39
    using an object oriented
  • 8:39 - 8:41
    set of features.
  • 8:41 - 8:42
    In this case,
  • 8:42 - 8:45
    the program here ? this is actually up at the top level, and it's saying this is a
  • 8:45 - 8:49
    global constant that's available anywhere in the file. After this declaration, I can
  • 8:49 - 8:51
    use the NumScores,
  • 8:51 - 8:52
    which will refer back to this
  • 8:52 - 8:54
    without
  • 8:54 - 8:57
    any further adornment.
  • 8:57 - 8:59
    The next thing underneath that
  • 8:59 - 9:02
    is a prototype.
  • 9:02 - 9:04
    This is actually a little bit of a novelty
  • 9:04 - 9:05
  • 9:05 - 9:07
    that in Java
  • 9:07 - 9:09
    you are allowed to declare
  • 9:09 - 9:12
    if you have three methods, A, B and C, and maybe A calls B and B calls C, you could declare A, B
  • 9:12 - 9:15
    and C in any order that's convenient for you. You want to put them
  • 9:15 - 9:18
    alphabetically? You want to put them in the order they're called? You want to put them
  • 9:18 - 9:19
    in the order
  • 9:19 - 9:22
    top to bottom down or bottom up?
  • 9:22 - 9:25
    It doesn't matter. C++ tends
  • 9:25 - 9:28
    to want to look
  • 9:28 - 9:31
    at a file once top to
  • 9:31 - 9:34
    bottom and not have any
  • 9:34 - 9:35
    reason to go back and revisit things.
  • 9:35 - 9:39
    It happens that in the main function, which is the one where code starts from,
  • 9:39 - 9:43
    I want to make a call to this GetScoresAndAverage function.
  • 9:43 - 9:46
    If I had left this prototype off and I had this code here,
  • 9:46 - 9:49
    when it gets to this line where I'm making that call, the compiler hasn't yet seen
  • 9:49 - 9:53
    anything about GetScoresAndAverage. It's on the next page.
  • 9:53 - 9:56
    It will complain. It will say I don't know what the function is. You haven't told me about it.
  • 9:56 - 9:58
    It came out of nowhere.
  • 9:58 - 10:02
    This prototype is our way of informing the compiler about something
  • 10:02 - 10:05
    that's coming up later, and so the prototype tells about the return type,
  • 10:05 - 10:07
    the name of the function,
  • 10:07 - 10:10
    the type and names of the arguments and the order of them and then ends
  • 10:10 - 10:12
    with a semicolon.
  • 10:12 - 10:15
    It matches exactly the function header that we'll see on the next page,
  • 10:15 - 10:18
    and it just tells the compiler in advance here's something that's coming
  • 10:18 - 10:23
    up later in the file. It's a function with this name and this signature.
  • 10:23 - 10:26
    It's something you didn't have to do in Java but is a part of
  • 10:26 - 10:28
    the
  • 10:28 - 10:29
    C++ world.
  • 10:29 - 10:31
    There's actually an alternate way I could have done this,
  • 10:31 - 10:34
    which is when I show you the code for GetScoresAndAverage, if I actually
  • 10:34 - 10:35
    defined them in the other order,
  • 10:35 - 10:38
    I could avoid that separate prototype.
  • 10:38 - 10:43
    It's a little bit of a matter of personal taste which way
  • 10:43 - 10:45
    you feel comfortable doing it.
  • 10:45 - 10:49
    Let's look at this guy.
  • 10:49 - 10:50
    Main is special.
  • 10:50 - 10:54
    Lowercase
  • 10:54 - 10:58
    m-a-i-n
  • 10:58 - 11:00
  • 11:00 - 11:03
    has no arguments and returns an integer type.
  • 11:03 - 11:06
    This is the function in the C++ program where execution
  • 11:06 - 11:09
    starts. There can be exactly one main function
  • 11:09 - 11:11
    with this name and this signature,
  • 11:11 - 11:13
    and when the
  • 11:13 - 11:16
    program gets up and running, the first thing it does is start executing the
  • 11:16 - 11:20
    lines of code that come in this function. No objects are created or any kind of
  • 11:20 - 11:23
    fancy outer structure the way it might be in Java. It immediately goes here
  • 11:23 - 11:25
    and starts doing what you told it to do.
  • 11:25 - 11:28
    Main has a special role to play in
  • 11:28 - 11:31
    any program.
  • 11:31 - 11:32
    What this program
  • 11:32 - 11:33
    does
  • 11:33 - 11:35
    is first prints
  • 11:35 - 11:39
    something. This is the C++ syntax for printing things out. Even if
  • 11:39 - 11:41
    you haven't ever seen it,
  • 11:41 - 11:44
    you probably can get a little bit of an idea of what it's trying to do if you just
  • 11:44 - 11:46
    step back from it and squint a little. It looks a little like system.out.print
  • 11:46 - 11:48
  • 11:48 - 11:49
    lin.
  • 11:49 - 11:54
    This is what's called the consult out, or the standard out that's here on the left-hand
  • 11:54 - 11:55
    side.
  • 11:55 - 12:00
    The use of the double less than here is what's called the stream
  • 12:00 - 12:04
    insertion operator. The standard output operation is saying insert into that
  • 12:04 - 12:05
    stream
  • 12:05 - 12:07
    and then in the double quotes is a string.
  • 12:07 - 12:09
    This program averages.
  • 12:09 - 12:14
    It says insert next to that the NumScores, which is this constant. It
  • 12:14 - 12:16
    got the value from above. And then another string,
  • 12:16 - 12:19
    and at the very end, there's this little endl,
  • 12:19 - 12:22
    which is the end line
  • 12:22 - 12:26
    insertion of what's called a stream manipulator. It says at the end of this,
  • 12:26 - 12:28
    put a new line and start any
  • 12:28 - 12:30
    subsequent text on the next line. In
  • 12:30 - 12:32
    Java, that would look like a system.out.print lin of this
  • 12:32 - 12:37
    program averages plus the NumScores plus that using the string
  • 12:37 - 12:40
    [inaudible] and conversion stuff in Java. It looks a little bit different in
  • 12:40 - 12:42
    C++, but in the end result,
  • 12:42 - 12:45
    it's a matter of printing out a bunch of numbers and strings
  • 12:45 - 12:47
    intermingled with new lines.
  • 12:47 - 12:53
    Nothing too fancy there. The
  • 12:53 - 12:57
    next line is making that call to that function that we earlier told it about
  • 12:57 - 12:59
    but we haven't yet seen the code for
  • 12:59 - 13:02
    that makes a call to get scores and averages. It passes the constant NumScores
  • 13:02 - 13:05
    and says that's how many scores that parameter has used to decide
  • 13:05 - 13:08
    how many iterations to run the loop requesting the values. Then it
  • 13:08 - 13:10
    returns
  • 13:10 - 13:11
    the average of the values entered
  • 13:11 - 13:14
    and then we take that number. This just shows
  • 13:14 - 13:17
    that in C++, you can declare
  • 13:17 - 13:18
    variables anywhere,
  • 13:18 - 13:21
    either at the start of a block, in the middle of a block or wherever they are at.
  • 13:21 - 13:27
    We took that value, stored it in this
  • 13:27 - 13:30
    variable and then used it in the next print statement.
  • 13:30 - 13:33
    The last thing we have is a return zero.
  • 13:33 - 13:34
    The
  • 13:34 - 13:37
    signature for main in C++ always returns an integer.
  • 13:37 - 13:39
    That integer is
  • 13:39 - 13:42
    not all that useful in most situations. It tended to be a kind of
  • 13:42 - 13:45
    return that tells the
  • 13:45 - 13:48
    operating system who invoked the program whether everything completed
  • 13:48 - 13:49
    successfully or not.
  • 13:49 - 13:52
    By default, the value that's used to show successful completion is zero,
  • 13:52 - 13:56
    so unless you have some other reason, it is return zero that you'll always see at the end
  • 13:56 - 13:57
    there.
  • 13:57 - 14:00
    In reality, if you were to return something else,
  • 14:00 - 14:03
    you won't see a lot of other differences. If you return -1
  • 14:03 - 14:05
    or 642,
  • 14:05 - 14:08
    in most contexts, that number's almost ignored.
  • 14:08 - 14:13
    It is there just to make tidy.
  • 14:13 - 14:16
    I'm about to flip to the next part,
  • 14:16 - 14:18
    and this is the
  • 14:18 - 14:21
    decomposed function that main made a call out to that does the
  • 14:21 - 14:23
  • 14:23 - 14:26
    averaging of the scores. We've got a comment on the top that tells a
  • 14:26 - 14:27
    little bit about how it works.
  • 14:27 - 14:28
    It's
  • 14:28 - 14:32
    always a good idea to get some documentation in there that's
  • 14:32 - 14:36
    helping the reader get oriented. That same matching of the prototype that was given
  • 14:36 - 14:37
    earlier ?
  • 14:37 - 14:40
    same name, same argument, same things, but instead of ending with that semicolon,
  • 14:40 - 14:41
    it goes into the body.
  • 14:41 - 14:45
    Sometimes we'll call these very similar terms ? this one we will call the
  • 14:45 - 14:49
    definition or the implementation of a function, and that earlier
  • 14:49 - 14:52
    just introduction of it is called its declaration or its prototype.
  • 14:52 - 14:55
    They should match, and they're just
  • 14:55 - 15:01
    there to give warning of something coming up later in the file. It
  • 15:01 - 15:02
    initializes a sum
  • 15:02 - 15:06
    to zero. It runs a standard four loop here. Like
  • 15:06 - 15:10
    Java, we tend to count from zero as computer scientists
  • 15:10 - 15:14
    for a long legacy reason. Instead of
  • 15:14 - 15:17
    going from one to NumScores, we actually go from zero to
  • 15:17 - 15:19
    NumScores minus one.
  • 15:19 - 15:19
  • 15:19 - 15:24
    Each time through the loop, we are prompting the user to
  • 15:24 - 15:25
    give us a score. We
  • 15:25 - 15:29
    are reading their score with a GetInteger call,
  • 15:29 - 15:32
    and we'll see what GetInteger is. GetInteger is a CS106 function
  • 15:32 - 15:35
    that retrieves a number that the user types in the console.
  • 15:35 - 15:41
    We add that into the sum and keep going, and after we've done the whole thing,
  • 15:41 - 15:47
    at the very end, we're doing a division of the sum by the number of scores to
  • 15:47 - 15:51
    compute the average score that was [inaudible] and return that. Did
  • 15:51 - 15:54
    you say why we didn't
  • 15:54 - 15:56
    have end
  • 15:56 - 15:59
    line? In this case, I didn't have an end line just because I think it makes it
  • 15:59 - 16:02
    nicer for the user. If you say next score and end line, then you're typing on the line
  • 16:02 - 16:05
    underneath it, and so if you don't put the end line there, it's like you type
  • 16:05 - 16:07
    right next to the question mark,
  • 16:07 - 16:09
    and so it actually it an aesthetic
  • 16:09 - 16:11
    thing.
  • 16:11 - 16:12
    If
  • 16:12 - 16:15
    I put it there, it would say next score and then I would be typing underneath it, and
  • 16:15 - 16:22
    instead, I'm typing right next to it. I hit the return and then the next
  • 16:22 - 16:26
    one comes on the
  • 16:26 - 16:30
    next line. So [inaudible] ? and I will talk about that a little bit later today, but it
  • 16:30 - 16:32
    is ?
  • 16:32 - 16:34
    that's a good question but a little bit advanced, so
  • 16:34 - 16:36
    just take my answer and then we'll get to that in maybe 20 minutes. So for every main function, you always have to end
  • 16:36 - 16:40
  • 16:40 - 16:42
    with
  • 16:42 - 16:46
    return zero? Pretty much. As I said, you could put return anything,
  • 16:46 - 16:48
    but as good form,
  • 16:48 - 16:54
    return zero is a way of saying I successfully completed my job
  • 16:54 - 16:56
    and I'm done. Do you
  • 16:56 - 16:59
    use a void return type for
  • 16:59 - 17:01
    the main? In C++, no. In C,
  • 17:01 - 17:04
    it's a little more lenient about that, and it will allow
  • 17:04 - 17:08
    that. In C++,
  • 17:08 - 17:12
    [inaudible]. I have to decide if I can get this
  • 17:12 - 17:13
  • 17:13 - 17:15
    to you without
  • 17:15 - 17:16
    hurting you. Let me
  • 17:16 - 17:20
    do a little coding with you guys
  • 17:20 - 17:22
    just to play
  • 17:22 - 17:25
    around with this
  • 17:25 - 17:29
    and show you the complier. I'd like to do a little bit of
  • 17:29 - 17:35
    this. I think it's really easy for me. I talk really fast. You might have noticed. When
  • 17:35 - 17:39
    I get going on something, it's easier for me to put up a lot of code on a slide and say
  • 17:39 - 17:42
    here it is. It's all fully formed. The reality is when you're
  • 17:42 - 17:44
    running your programs yourself,
  • 17:44 - 17:46
    it's not going to come to you fully formed on
  • 17:46 - 17:49
    a slide all ready to go. You're going to have to figure out how do you stack through making this
  • 17:49 - 17:52
    stuff work? I'm going to do a little bit of development of a program that looks very
  • 17:52 - 17:54
    similar to the one I just wrote, but I'm going to
  • 17:54 - 17:55
    play around with it a little bit
  • 17:55 - 17:57
    to get at
  • 17:57 - 17:59
    some of the things that are different about C++ and how the tools work.
  • 17:59 - 18:00
  • 18:00 - 18:04
    This is basically what an empty project starts me off with.
  • 18:04 - 18:05
    I've got
  • 18:05 - 18:06
    the
  • 18:06 - 18:09
    include for the genlib, but I'll always have my int main and return zero and nothing
  • 18:09 - 18:10
    else doing. I'm
  • 18:10 - 18:13
    going to sit down. I'm going to start typing.
  • 18:13 - 18:16
    I'm going to say
  • 18:16 - 18:19
    welcome.
  • 18:19 - 18:20
  • 18:20 - 18:23
    I'm going to
  • 18:23 - 18:26
    move to ? let's do this. We'll play with this for a second. There
  • 18:26 - 18:27
    are some things that I can
  • 18:27 - 18:31
    do. I can say welcome a lot of times.
  • 18:31 - 18:32
  • 18:32 - 18:35
    That wasn't really so hot there. You guys
  • 18:35 - 18:36
    can help me when I
  • 18:36 - 18:37
    fail to type correctly.
  • 18:37 - 18:41
    I say welcome a lot of times. Let's go see and make sure this works.
  • 18:41 - 18:43
    Look at that. It doesn't like what I did.
  • 18:43 - 18:45
    Let's see what it has to say.
  • 18:45 - 18:48
    It says cout was not [inaudible] the scope. Endl was not declared in the scope.
  • 18:48 - 18:51
    This is an error message you're going to see a lot of times
  • 18:51 - 18:55
    in various other symbol lanes that will show up there, which is the C++ compiler's
  • 18:55 - 18:57
    way of saying to you I don't read your mind.
  • 18:57 - 18:59
    You used cout. You used endl.
  • 18:59 - 19:02
    These things come from somewhere, but they're not known to me until you
  • 19:02 - 19:05
    make them known to me. The way I make those known is that those are part of
  • 19:05 - 19:06
    the
  • 19:06 - 19:08
    standard IO stream,
  • 19:08 - 19:11
    which the input/output streams facility for the C++ language,
  • 19:11 - 19:13
    and once I tell it about those things, it's going to be
  • 19:13 - 19:15
    much happier,
  • 19:15 - 19:18
    and it's going to say welcome to me a lot of times.
  • 19:18 - 19:20
    In fact, I could say this
  • 19:20 - 19:23
    ? 106B rocks. I'll
  • 19:23 - 19:24
    do this.
  • 19:24 - 19:26
    I'll say
  • 19:26 - 19:28
    how awesome
  • 19:28 - 19:31
    equals get integer
  • 19:31 - 19:33
    and I will ? it may help if
  • 19:33 - 19:35
    I move this up a little bit so we can see
  • 19:35 - 19:37
  • 19:37 - 19:38
    how much do
  • 19:38 - 19:41
    you love 106B?
  • 19:41 - 19:43
    Here, I won't use my endl, so we'll see how the prompt comes out. I'm going to get an integer
  • 19:43 - 19:47
    from them, and I'm going to use that
  • 19:47 - 19:49
    to
  • 19:49 - 19:51
    write how awesome that many times.
  • 19:51 - 19:54
    What does it not like about that? Again, it
  • 19:54 - 19:57
    says get integer not declared in the scope. This is going to be the bane of our existence today, which
  • 19:57 - 19:58
    is remembering
  • 19:58 - 20:03
    that there are headers out there that have features that we're going to be using, and getting
  • 20:03 - 20:06
    familiar with the ones that we're gonna need ? there are probably about
  • 20:06 - 20:09
    ten total that we'll use again and again, and we have to get
  • 20:09 - 20:12
    our bearings about which ones are where. The documentation for all of our CS106
  • 20:12 - 20:17
    headers is available on the website in a nice HTML-ized version,
  • 20:17 - 20:21
    and the ones from the standard libraries are
  • 20:21 - 20:26
    detailed in the book in chapter three.
  • 20:26 - 20:28
    How much do
  • 20:28 - 20:31
    you love 106B? I could say well, I love it two times.
  • 20:31 - 20:37
    I say no, I love it 1,900 and
  • 20:37 - 20:39
    so. 10,000
  • 20:39 - 20:42
    times later, if you're not convinced now, I don't know what will convince
  • 20:42 - 20:46
    you. We're seeing some stuff.
  • 20:46 - 20:47
    Let's see
  • 20:47 - 20:51
    if we can make that averaging thing work for us. I'll go
  • 20:51 - 20:54
    back to having it say welcome.
  • 20:54 - 20:57
  • 20:57 - 21:00
    That's what the endl was doing for me there. The prompt is
  • 21:00 - 21:02
    waiting at the end of that line. If I have the endl, it will actually be
  • 21:02 - 21:05
    waiting on the
  • 21:05 - 21:10
    next line. Let
  • 21:10 - 21:12
    me
  • 21:12 - 21:16
    put a bogus message here. Then I'm going to say double average equals GetScoresAndAverage
  • 21:16 - 21:18
  • 21:18 - 21:19
  • 21:19 - 21:21
    and then let's
  • 21:21 - 21:23
    go ahead and
  • 21:23 - 21:27
    ? I'm going to blow off the constant for a little bit because
  • 21:27 - 21:30
    I'm going to be mucking with it anyway, and I'm not going to use that constant for very long,
  • 21:30 - 21:31
    so I'm
  • 21:31 - 21:33
    not going to worry about it
  • 21:33 - 21:36
    yet. I put my prototype up here
  • 21:36 - 21:39
    so it knows what I'm talking about, and then I'm going to
  • 21:39 - 21:44
    print out average is. I've got a little
  • 21:44 - 21:47
    bit of structure there. I
  • 21:47 - 21:50
    can even go in here. Copy and paste is a really good idea. Once I've written
  • 21:50 - 21:54
    the function header once, I don't really want to make a mistake. Something that's
  • 21:54 - 21:56
    interesting
  • 21:56 - 22:00
    to know is that you can always write code that
  • 22:00 - 22:03
    doesn't do what you want to start with but that lets you test
  • 22:03 - 22:06
    some of the other pieces around it. In this case, I haven't
  • 22:06 - 22:08
    finished fleshing out what GetScoresAndAverage is,
  • 22:08 - 22:11
    but I can write code around it that says well, it passes in this number and it does some
  • 22:11 - 22:12
    stuff.
  • 22:12 - 22:13
    Right now, it just returns zero.
  • 22:13 - 22:16
    It's totally bogus. It's not solving any of my problems, but it does allow
  • 22:16 - 22:19
    me to test some other part of the code
  • 22:19 - 22:20
  • 22:20 - 22:23
    to make sure that connection and other parts are working before I go on
  • 22:23 - 22:25
    to work on this part in isolation. One
  • 22:25 - 22:28
    of the things I'll try to show you whenever I'm doing code
  • 22:28 - 22:32
    is to try to give you a little of the insight to how I approach
  • 22:32 - 22:34
    thinking about the code. I do think that one of the things that really distinguishes
  • 22:34 - 22:37
    somebody who works effectively and productively
  • 22:37 - 22:40
    from someone who spends a lot more time doing it is often just strategies and
  • 22:40 - 22:44
    tactics for how you attack your problem. I'm hoping that along the way, I can
  • 22:44 - 22:45
    kind
  • 22:45 - 22:49
    of share a little bit of the insights that I use to keep myself making forward progress.
  • 22:49 - 22:52
    I am going to put in that four loop that I'm
  • 22:52 - 22:54
    looking for.
  • 22:54 - 22:56
  • 22:56 - 22:58
    I'm really not this bad of a typist in real life, but
  • 22:58 - 23:00
    it's
  • 23:00 - 23:03
    easy to
  • 23:03 - 23:06
    ? the use of the braces here is what's
  • 23:06 - 23:09
    defining the block structure. Without the
  • 23:09 - 23:12
    open curly brace close curly brace, the four loop and the if and the other
  • 23:12 - 23:16
    statements only grab that next line as being part of the indented
  • 23:16 - 23:18
    body of the four loop.
  • 23:18 - 23:21
    I'm going to go ahead with my
  • 23:21 - 23:21
    next
  • 23:21 - 23:24
    idea. I'm going to put that in the sum,
  • 23:24 - 23:29
    and then I'm going to do some things that
  • 23:29 - 23:33
    seem bogus. We're going to get there. I'm going to make some mistakes.
  • 23:33 - 23:36
    Some of them I'll make on purpose. Some of them I'll actually be
  • 23:36 - 23:40
    making accidentally. We'll see which ones I'm better at finding. I
  • 23:40 - 23:43
    have my GetScoresAndAverage. It asks them for the scores. It adds it into the
  • 23:43 - 23:46
    integer and then doesn't return anything. First, let's
  • 23:46 - 23:48
    see how the compiler feels about that.
  • 23:48 - 23:50
    The first thing you'll notice is that
  • 23:50 - 23:53
    compiled.
  • 23:53 - 23:57
    That might alarm you just a little bit, because if you go back and look at this code,
  • 23:57 - 23:59
    it certainly seems wrong.
  • 23:59 - 24:02
    Anybody want to help me with one of the things that's wrong with it?
  • 24:02 - 24:05
    It doesn't return anything. That seems
  • 24:05 - 24:09
    a little shocking. It doesn't return anything at all. What is it then
  • 24:09 - 24:14
    actually going to produce? What is it doing that is a little bit bogus?
  • 24:14 - 24:15
    Sum is
  • 24:15 - 24:16
    not initialized. These are two
  • 24:16 - 24:20
    examples of errors that Java doesn't let you get away with.
  • 24:20 - 24:21
    You may very well remember from having
  • 24:21 - 24:24
    typed in some code like this in Java
  • 24:24 - 24:27
    that it will complain. If you have a variable and you declare it and you don't initialize it and you
  • 24:27 - 24:30
    start using it, the compiler says not happening. You need to go back there
  • 24:30 - 24:33
    and give that guy an initial value before you start reading it.
  • 24:33 - 24:36
    Similarly, you fall off the end of a function. It said it was going to return a
  • 24:36 - 24:36
    double ?
  • 24:36 - 24:40
    you better return a double. Every code path needs to return a double. You can't
  • 24:40 - 24:44
    bail out early in some cases and leave it fall off the end. It
  • 24:44 - 24:46
    considers both of those
  • 24:46 - 24:48
    big enough errors that it won't let you get away with them.
  • 24:48 - 24:52
    C++ compilers are a little bit more lax. It's crack mom, I told you.
  • 24:52 - 24:54
    It's like well, if you don't want to initialize that,
  • 24:54 - 25:00
    go ahead. What value does it have
  • 25:00 - 25:02
    then?
  • 25:02 - 25:06
    It doesn't make it zero. It
  • 25:06 - 25:09
    doesn't make it empty. It doesn't do anything nice or clever or
  • 25:09 - 25:14
    helpful. It's like whatever kind of contents were lying around in that particular location,
  • 25:14 - 25:16
    now it's [inaudible] initial
  • 25:16 - 25:19
    value. The result will be that we could get some pretty strange results. If I run this
  • 25:19 - 25:22
    thing right now ?
  • 25:22 - 25:24
    let's see. I type in some numbers. I type in
  • 25:24 - 25:27
    somebody got a nine, somebody got a 67, somebody got an 87.
  • 25:27 - 25:31
    I type in a
  • 25:31 - 25:35
    bunch of numbers, and the average is zero.
  • 25:35 - 25:36
  • 25:36 - 25:39
    That was interesting. Zero seems like it had some rhyme or reason to
  • 25:39 - 25:43
    it, but there's no guarantee. I could run it again and I
  • 25:43 - 25:50
    might get very different numbers. I got zero the
  • 25:50 - 25:52
    second
  • 25:52 - 25:53
    time.
  • 25:53 - 25:56
    Good luck for me.
  • 25:56 - 25:57
    Let's go in and start fixing some of this stuff.
  • 25:57 - 26:01
    The thing to note is the compiler isn't nearly as aggressive about being
  • 26:01 - 26:05
    on your case about things, so it does require a little bit more self-monitoring
  • 26:05 - 26:07
    on some of these things. Seeing this
  • 26:07 - 26:11
    result and going back and figuring out how I got into this situation
  • 26:11 - 26:13
    ? let me
  • 26:13 - 26:14
    get that guy an initial
  • 26:14 - 26:22
    value, and I'll go ahead and put a return down here.
  • 26:22 - 26:25
    We'll see if I like how this works any
  • 26:25 - 26:27
    better. I put in five,
  • 26:27 - 26:28
    six,
  • 26:28 - 26:31
    seven and six, and the average is six.
  • 26:31 - 26:32
    It kind of looks good, right? It
  • 26:32 - 26:36
    makes sense to me that the five and seven cancelled each other out.
  • 26:36 - 26:39
    But if I do a little bit more deep testing where I put in numbers like five,
  • 26:39 - 26:42
    six, five and six, my
  • 26:42 - 26:43
    average is five. In
  • 26:43 - 26:44
    this
  • 26:44 - 26:46
    case, I would think that I would be
  • 26:46 - 26:49
    getting something like five and a half. I go back
  • 26:49 - 26:52
    here and take a look at what I did. I took sum and divided it by NumScores.
  • 26:52 - 26:56
    What have I failed to do?
  • 26:56 - 27:00
    Cast that thing to get it out of there. The default ? this is the same in Java as it is
  • 27:00 - 27:01
    in C++
  • 27:01 - 27:06
    is that if you are combining two operands in an arithmetic expression,
  • 27:06 - 27:10
    if they are of mixed type ? one is double and one is int ? it promotes to the
  • 27:10 - 27:12
    richer type. If one of those was a double,
  • 27:12 - 27:16
    it would convert the other one to a double and do the calculation in double
  • 27:16 - 27:20
    space. As it is, sum is an integer and NumScores is an integer. It's doing integer division.
  • 27:20 - 27:24
    Integer division has [inaudible] built into it, so if I divide ten by three, I get three
  • 27:24 - 27:26
    and that remaining one third is just thrown away.
  • 27:26 - 27:29
    Even though the result is double,
  • 27:29 - 27:33
    that happens after the fact. It does the calculation
  • 27:33 - 27:34
    integer space ? the
  • 27:34 - 27:36
    ten divided by
  • 27:36 - 27:36
    three.
  • 27:36 - 27:40
    It gets the three-integer result and then it says oh, I have an integer here. I need
  • 27:40 - 27:43
    to convert it to a double on my way out. It takes that and turns it into
  • 27:43 - 27:44
  • 27:44 - 27:48
    3.0. It doesn't recover that truncated part that got thrown away. If I really want to get
  • 27:48 - 27:50
    a real double result, I've got to make one of those a double
  • 27:50 - 27:54
    to force the calculation into the double space.
  • 27:54 - 27:58
    The mechanism of C++ is a little bit different than it is in Java. In fact,
  • 27:58 - 27:59
    the Java mechanism
  • 27:59 - 28:03
    does work, but the preferred form in C++ looks a little bit like this where you take the
  • 28:03 - 28:06
    name of the type you're trying to convert it to. It looks a little bit like you're making a function
  • 28:06 - 28:07
    call-passing sum
  • 28:07 - 28:10
    to a function that will change it into a double.
  • 28:10 - 28:12
    By doing this,
  • 28:12 - 28:15
    I now have one of them being double. Int on the other side gets promoted.
  • 28:15 - 28:17
    If I do my five, six, five, six,
  • 28:17 - 28:19
    that truncated part is now
  • 28:19 - 28:27
    part of that calculation and preserved and pulled out and printed.
  • 28:27 - 28:30
    Let
  • 28:30 - 28:32
    me change this a little bit
  • 28:32 - 28:33
    to something else.
  • 28:33 - 28:37
    Right now, it assumes that there's a fixed number of scores that you want to do.
  • 28:37 - 28:40
    Maybe what I have is a big stack of exam papers, and I
  • 28:40 - 28:42
    don't actually know how many are in the pile, and I don't actually want to count them ahead
  • 28:42 - 28:43
    of time.
  • 28:43 - 28:46
    I just want to keep entering scores until I get to the bottom of the pile. I'm
  • 28:46 - 28:48
    going to change this loop
  • 28:48 - 28:48
    to
  • 28:48 - 28:51
    allow me to enter numbers until I say I'm done.
  • 28:51 - 28:53
  • 28:53 - 28:55
    Let's take a look at what that's gonna look like.
  • 28:55 - 28:57
    I'm going
  • 28:57 - 28:59
    to get the value
  • 28:59 - 29:00
    here. I'm going to change my code a little bit.
  • 29:00 - 29:03
    Then if the value is the sentinel,
  • 29:03 - 29:05
  • 29:05 - 29:09
    then I don't want to include it in the sum. Otherwise, I
  • 29:09 - 29:14
    do. Maybe we'll do this. We'll say if the value doesn't equal the sentinel,
  • 29:14 - 29:15
    then sum
  • 29:15 - 29:16
    adds into there.
  • 29:16 - 29:20
    I also need to make this thing stop, and so I'm going to have to rearrange
  • 29:20 - 29:23
    my loop a little bit here, because if you
  • 29:23 - 29:26
    think about what I'm going to try to do, I'm going to prompt and get a value and then
  • 29:26 - 29:27
    either add it in the sum or quit.
  • 29:27 - 29:30
    Then I'm going to prompt and get a value and add it in the sum or quit. I have a loop where
  • 29:30 - 29:33
    I need to rearrange my
  • 29:33 - 29:35
    notion a little bit here.
  • 29:35 - 29:38
    One way I could do this is something like this. I could say this. While
  • 29:38 - 29:42
    value does not equal sentinel,
  • 29:42 - 29:43
    then
  • 29:43 - 29:46
    add it into the sum. I'm going to kind of change my code around. Watch
  • 29:46 - 29:48
    carefully as I
  • 29:48 - 29:52
    reorganize it. If the value is not equal to the sentinel, then I'm going to
  • 29:52 - 29:54
    add it into the sum
  • 29:54 - 29:56
    and then I'm going to get the next
  • 29:56 - 29:58
  • 29:58 - 30:02
    and overwrite the previous
  • 30:02 - 30:07
    value. I inverted that loop that was there a little bit.
  • 30:07 - 30:10
    It says while the value is not the sentinel,
  • 30:10 - 30:13
    add it into the sum and then get the next value and come back around.
  • 30:13 - 30:17
    The problem with this code as it stands is there's something a little bit wrong about value. It's not
  • 30:17 - 30:20
    initialized. I need to initialize it to something.
  • 30:20 - 30:23
    What
  • 30:23 - 30:25
    do I need to initialize it to? What the
  • 30:25 - 30:28
    user wants. This little piece of code down here
  • 30:28 - 30:31
    needs to come up
  • 30:31 - 30:33
    and be repeated up here.
  • 30:33 - 30:38
    I need to get a value from them and then go into if it wasn't a sentinel add it
  • 30:38 - 30:40
    and get the next one.
  • 30:40 - 30:42
    This is what's called a classic loop and a
  • 30:42 - 30:45
    half construction where there's a few things I need to do and then I need to do
  • 30:45 - 30:49
    some tests and decide whether to go on with that iteration. As it is,
  • 30:49 - 30:52
    there's half of the loop that starts up. They call it
  • 30:52 - 30:55
    priming the loop.
  • 30:55 - 30:57
    That's a little bit
  • 30:57 - 30:59
    unseemly. There's something about that that's a little bothersome. Even though it's a very
  • 30:59 - 31:03
    small piece of code ? two lines of code is not going to rock the world. I do
  • 31:03 - 31:07
    want to try to get us into the habit of thinking if we can combine that code and
  • 31:07 - 31:08
    unify it, that's better.
  • 31:08 - 31:11
    It's better than repeating it. Repeating it means that if I ever change something about
  • 31:11 - 31:13
    it, I have to change it in two places.
  • 31:13 - 31:15
    There's an opportunity for error to creep in
  • 31:15 - 31:18
    that I can avoid if I can get it to where there's really just one and only one version of
  • 31:18 - 31:19
    the truth. I'm
  • 31:19 - 31:21
    going to change this.
  • 31:21 - 31:24
    It's going to use the break statement,
  • 31:24 - 31:26
    which you may have had a little experience with. I'm going to run a
  • 31:26 - 31:30
    while true loop, which looks like it's going to run forever.
  • 31:30 - 31:32
    I'm going to prompt to get the value
  • 31:32 - 31:37
    and if the value equals the sentinel,
  • 31:37 - 31:38
    then I'm going to use break
  • 31:38 - 31:41
    and immediately exit the loop here
  • 31:41 - 31:44
    without completing the bottom of this loop iteration. It causes it to move on
  • 31:44 - 31:46
    to the statements after the loop.
  • 31:46 - 31:48
    By doing it this way, I now have
  • 31:48 - 31:49
    one prompt,
  • 31:49 - 31:50
    then a check,
  • 31:50 - 31:53
    and based on that check, I either finish this iteration and keep coming
  • 31:53 - 31:54
    around
  • 31:54 - 31:57
    or I immediately break out saying that was
  • 31:57 - 32:01
    the clue that we're done with this.
  • 32:01 - 32:02
  • 32:02 - 32:03
    I would
  • 32:03 - 32:08
    say that it's a little bit of an advanced question, but it turns out that the do while loop is
  • 32:08 - 32:10
    not really that much cleaner in the end
  • 32:10 - 32:12
  • 32:12 - 32:16
    because you still have to do the prompt and test and decide when to add it in.
  • 32:16 - 32:19
    Do well loops are just so unusual that they cause everybody to slow down a little
  • 32:19 - 32:22
    bit when you read them. If you can write it using a more [inaudible] construct,
  • 32:22 - 32:24
    there is some advantage to that.
  • 32:24 - 32:27
    I got this guy together here,
  • 32:27 - 32:27
  • 32:27 - 32:31
    and then maybe I should actually tell the user ? this might be a good thing to say.
  • 32:31 - 32:33
  • 32:33 - 32:37
    Tell them what the sentinel is.
  • 32:37 - 32:41
    I have to type better than I do.
  • 32:41 - 32:47
  • 32:47 - 32:51
    NumScores
  • 32:51 - 32:54
    equals zero,
  • 32:54 - 32:58
    and then each time we
  • 32:58 - 33:01
  • 33:01 - 33:05
    get one, we
  • 33:05 - 33:10
    increment.
  • 33:10 - 33:13
    Let's go back. We'll change our call to use a more ordinary value like -1.
  • 33:13 - 33:17
    We'll type in
  • 33:17 - 33:21
    five, six, seven, eight, nine and then -1, and the average of
  • 33:21 - 33:22
    those is
  • 33:22 - 33:27
    seven.
  • 33:27 - 33:30
    It
  • 33:30 - 33:34
    should seem very familiar in a lot of ways but there are a few details that
  • 33:34 - 33:35
    need to be different. I'm going
  • 33:35 - 33:39
    to show you one of the little C++isms while I'm here because it's something that comes up in the
  • 33:39 - 33:44
    standard libraries and it's worth knowing. There are some minor conveniences in the way
  • 33:44 - 33:48
    that C++ provides certain facilities that are not conceptually a
  • 33:48 - 33:51
    big deal, but you do need to know about them because you're going to encounter them in various interfaces.
  • 33:51 - 33:55
    One is the notion of a default argument.
  • 33:55 - 33:58
    In the prototype for a function,
  • 33:58 - 34:01
  • 34:01 - 34:03
  • 34:03 - 34:05
  • 34:05 - 34:05
  • 34:05 - 34:08
    there are arguments to a function where it's
  • 34:08 - 34:11
    some very large percentage of the time always going to
  • 34:11 - 34:14
    want to be a certain value, but you still want to leave it open for the user to specify
  • 34:14 - 34:18
    a different value in the cases where they don't want to use that standard
  • 34:18 - 34:18
    value.
  • 34:18 - 34:21
    One way of doing that in C++ is to write your function using a
  • 34:21 - 34:22
    default argument
  • 34:22 - 34:24
    where I say int sentinel
  • 34:24 - 34:26
    and I said equals
  • 34:26 - 34:29
    -1. That is providing for if somebody makes a call to GetScoresAndAverage
  • 34:29 - 34:31
    passing an argument, it's used instead.
  • 34:31 - 34:35
    If they pass nothing, so they just have open paren close paren and they don't give a
  • 34:35 - 34:36
    value for that argument,
  • 34:36 - 34:39
    that means the assumption is to use that default. That means that most people
  • 34:39 - 34:43
    who are making calls to GetScoresAndAverage can just drop this number
  • 34:43 - 34:46
    and get the behavior of using -1 as the sentinel.
  • 34:46 - 34:49
    In the case where for one reason or another -1 was one of the valid values that you
  • 34:49 - 34:53
    could potentially enter, you could pick a different one.
  • 34:53 - 34:55
  • 34:55 - 34:57
  • 34:57 - 34:58
  • 34:58 - 35:01
    We'll see that in the libraries. It's interesting.
  • 35:01 - 35:02
    The
  • 35:02 - 35:06
    mechanism of it is really quite simple. It's just a convenience to
  • 35:06 - 35:07
    provide for.
  • 35:07 - 35:10
    You can actually have more than one default argument.
  • 35:10 - 35:11
    The
  • 35:11 - 35:15
    idea is that you can only leave off when you're making the call the last most
  • 35:15 - 35:18
    argument, and then from there, other ones, because it has to figure out how to match them
  • 35:18 - 35:20
    up. It matches the arguments left and right,
  • 35:20 - 35:24
    and as soon as it runs out of arguments, it assumes everything from there has to be
  • 35:24 - 35:27
    used its default argument for it to match that call. It allows
  • 35:27 - 35:28
    for ?
  • 35:28 - 35:31
    you could have three arguments of which you could specify two or one or three if they all
  • 35:31 - 35:33
    had defaults
  • 35:33 - 35:36
    if you needed that. It's not that common. Let
  • 35:36 - 35:39
    me go
  • 35:39 - 35:41
    back
  • 35:41 - 35:44
    to my slides and tell you about a couple other
  • 35:44 - 35:46
  • 35:46 - 35:53
    mechanisms of
  • 35:53 - 35:55
    some C++ features
  • 35:55 - 35:58
    that are new to us that we want to know a little bit about.
  • 35:58 - 36:01
    There are two types of user-defined types that are very simple to get your head
  • 36:01 - 36:06
    around. You want to know what the syntax for them is and what they do. It's the
  • 36:06 - 36:08
    enumerated type or the enumeration
  • 36:08 - 36:12
    where at the top of your program ? up there where I was defining constants and doing
  • 36:12 - 36:14
    #includes, this is where we put
  • 36:14 - 36:17
    information for the compiler that is of use across the entire program. This was
  • 36:17 - 36:19
    also where we would put new user defined types.
  • 36:19 - 36:22
    In this case, I want to define a type direction T
  • 36:22 - 36:25
    which can take on one of the four values, north, south, east or west.
  • 36:25 - 36:28
    That whole package up there ? that enum direction T north south east
  • 36:28 - 36:31
    west is the way of defining the new type. You're saying direction T now
  • 36:31 - 36:32
    comes into the
  • 36:32 - 36:36
    space as a name that can be used for variables, parameters, return
  • 36:36 - 36:40
    types ? any place you could have used int you can start using direction T.
  • 36:40 - 36:42
    It has
  • 36:42 - 36:43
    the expectation that variables
  • 36:43 - 36:46
    that are declared in direction T will be holding one of those four values.
  • 36:46 - 36:50
    It's like north, south, east and west got defined as constants,
  • 36:50 - 36:53
    and they are, by default, assigned the values zero, one, two and three
  • 36:53 - 36:56
    in order unless you do anything special.
  • 36:56 - 36:57
    You can use them ?
  • 36:57 - 37:00
    you can do things on enums that are very integer like.
  • 37:00 - 37:04
    You can assign them. You can compare them. You could use less than and greater than.
  • 37:04 - 37:06
    You can
  • 37:06 - 37:11
    add them. There are things ? they are largely implemented underneath the scenes as numeric
  • 37:11 - 37:13
    types, but they're a nice convenience for making it clear that
  • 37:13 - 37:16
    this thing isn't just any old ordinary integer.
  • 37:16 - 37:20
    It's specifically ? it should be kept into this constrained range. It means
  • 37:20 - 37:23
    something a little different. It's just a nicety. It does not
  • 37:23 - 37:25
    have a lot of heavy weight
  • 37:25 - 37:29
    feature associated with it, but it's just a nice way of documenting that something is
  • 37:29 - 37:32
    this kind of set up.
  • 37:32 - 37:34
    This one ?
  • 37:34 - 37:35
    the record of the [inaudible] type ?
  • 37:35 - 37:39
    much more broadly useful. It's just an aggregate where you can take a
  • 37:39 - 37:41
    set of fields
  • 37:41 - 37:43
  • 37:43 - 37:47
    of same or different type, give them names and aggregate them together. You
  • 37:47 - 37:50
    say here is student record, and the student has a name, dorm room,
  • 37:50 - 37:52
    phone number and transcript.
  • 37:52 - 37:55
    Aggregate it together into a new structure type. In this
  • 37:55 - 37:57
    case, the point T [inaudible],
  • 37:57 - 38:00
    and so like this ? this is the kind of thing you put up at the top of your
  • 38:00 - 38:02
    code that says here's what's in
  • 38:02 - 38:05
    a point T, the field names and their types,
  • 38:05 - 38:07
    and a
  • 38:07 - 38:11
    little point of noteworthy
  • 38:11 - 38:12
    error
  • 38:12 - 38:15
    producer is that there really does have to be a semicolon at the end of this.
  • 38:15 - 38:19
    Similarly, there has to be a semicolon at the end of the enum for direction
  • 38:19 - 38:20
    T.
  • 38:20 - 38:21
    If you forget to do that,
  • 38:21 - 38:25
    there's a cascade of errors out of that that are really a little bit
  • 38:25 - 38:29
    mystical the first time you see them. You learn to identify this
  • 38:29 - 38:30
    quickly later on.
  • 38:30 - 38:33
    It ends that declaration and then allows the composite to move on to the next
  • 38:33 - 38:36
    thing, not assuming you're still doing more work with this.
  • 38:36 - 38:38
    Once you have this type declared,
  • 38:38 - 38:40
    you can make variables, return types, parameters,
  • 38:40 - 38:45
    all that stuff, and then the access to the members within the fields within
  • 38:45 - 38:46
    a
  • 38:46 - 38:49
    [inaudible] looks like access to an object did in Java where you have the variable
  • 38:49 - 38:51
    name on the left
  • 38:51 - 38:54
    and then a dot and then on the right, the name of the field you're accessing,
  • 38:54 - 38:55
    setting, and reading.
  • 38:55 - 38:58
    You can do things like P = Q which does a full assignment of one
  • 38:58 - 39:02
    [inaudible] onto another, so it copies all the fields over from one
  • 39:02 - 39:03
    to the other. It's
  • 39:03 - 39:05
    something simple that does
  • 39:05 - 39:08
    have a lot of usefulness. It makes a lot of sense when you have a
  • 39:08 - 39:11
    program and you do group things together. Here's all the information about
  • 39:11 - 39:17
    this part of my data structure ? a student, a class, a
  • 39:17 - 39:17
    dorm
  • 39:17 - 39:20
    ? all the information being aggregated together into one unit is
  • 39:20 - 39:23
    actually a nice way to keep your
  • 39:23 - 39:30
    data organized.
  • 39:30 - 39:35
  • 39:35 - 39:39
    There
  • 39:39 - 39:42
    are two point T variables.
  • 39:42 - 39:47
    One is P. One is [inaudible]. I'm saying P.X. I'm saying the X field of the P variable is to be zero.
  • 39:47 - 39:49
    At this point, the P.Y's field is nonsense. It's
  • 39:49 - 39:52
    just garbage. I said P = Q, which basically says take the nonsense
  • 39:52 - 39:56
    in Q and override it onto P and make P be as nonsensical as Q is in terms of
  • 39:56 - 39:59
    its contents.
  • 39:59 - 40:02
    Not very useful code.
  • 40:02 - 40:05
    The last thing I'm going to show you today
  • 40:05 - 40:09
    is to talk a little bit about parameter passing. This is going to come up
  • 40:09 - 40:12
    again and again, but this is just kind of a quick
  • 40:12 - 40:15
    first mention of these things. Someone had asked earlier what is the
  • 40:15 - 40:19
    parameter passing mechanism that's in play? The
  • 40:19 - 40:22
    default parameter passing mechanism is what's called pass by value. It
  • 40:22 - 40:24
    copies.
  • 40:24 - 40:27
    If I have a function here binkie int X and
  • 40:27 - 40:30
    Y, in the body of it, it tries to do something like double the value of binkie
  • 40:30 - 40:33
    and reset Y to zero.
  • 40:33 - 40:36
    When I make a call to binkie and I had A set to four and B to 20, when I made
  • 40:36 - 40:37
    the call to binkie,
  • 40:37 - 40:42
    the pass by value really means that the X and Y parameters of binkie are copies of A
  • 40:42 - 40:43
    and B.
  • 40:43 - 40:46
    They are distinct. They are new integer variables, new space, new
  • 40:46 - 40:47
    storage,
  • 40:47 - 40:50
    and they got their initial values by taking the current values of A and B and copying
  • 40:50 - 40:51
    them.
  • 40:51 - 40:56
    Changes to X and Y affect binkie's context only.
  • 40:56 - 41:00
    That four that came in here and got doubled to eight, that Y that got set to zero
  • 41:00 - 41:04
    are live here, but when binkie exits and we get back to main, A
  • 41:04 - 41:06
    and B still are
  • 41:06 - 41:10
    four and 20. It just did full copies of all the variables, and this is true for
  • 41:10 - 41:13
    all types of [inaudible] and enums and ints and
  • 41:13 - 41:17
    chars and all that. It's copying the data and operating on a copy.
  • 41:17 - 41:21
    In most situations, that's actually fairly appropriate. You tend to be
  • 41:21 - 41:23
    passing information in so it can do some manipulations.
  • 41:23 - 41:27
    In the situation where you really want to be passing in and having changes be permanent
  • 41:27 - 41:29
    to it,
  • 41:29 - 41:31
    there is an alternate
  • 41:31 - 41:34
    declaration where you and an &
  • 41:34 - 41:37
    to the type. Instead of being an int, it's an
  • 41:37 - 41:42
    int&, and that changes this parameter from being a pass by value to a pass by
  • 41:42 - 41:44
    reference or a reference parameter.
  • 41:44 - 41:46
    In such a case,
  • 41:46 - 41:50
    when I make a call to binkie, this first argument will not be a copy. The second
  • 41:50 - 41:54
    argument's still a copy because I haven't changed anything about it, but when I say binkie of A and B,
  • 41:54 - 41:58
    what the binkie function is actually getting in that first argument is not a
  • 41:58 - 42:02
    copy of the value four. It's getting a reference back to the original
  • 42:02 - 42:06
    A. For the Y parameter, it's getting a copy of 20. When
  • 42:06 - 42:10
    it makes this change over here of trying to take X and double its value,
  • 42:10 - 42:12
    it really did reach back out and change
  • 42:12 - 42:14
    A. After this,
  • 42:14 - 42:17
    A would be eight. B would still be
  • 42:17 - 42:21
    20. It allows for you to pass some data in and have it be manipulated and changed, so
  • 42:21 - 42:23
    updated, adjusted and whatnot
  • 42:23 - 42:27
    and then come back out and see those changes
  • 42:27 - 42:30
    that is for certain situations very useful.
  • 42:30 - 42:34
    This mechanism doesn't exist in Java. There's not a pass by reference
  • 42:34 - 42:38
    mechanism, so this is likely to seem a little bit bizarre at first.
  • 42:38 - 42:40
    We will see this a lot, so this is maybe just our first introduction to this.
  • 42:40 - 42:43
    We're going to come back to this more and more. One
  • 42:43 - 42:45
    thing I will note is that the
  • 42:45 - 42:49
    primary purpose of this is to allow you to kind of change some data.
  • 42:49 - 42:52
    It also gets used in a lot of situations just for efficiency reasons where if you have
  • 42:52 - 42:57
    a large piece of data you're passing in a big database,
  • 42:57 - 42:59
    to copy it could be expensive.
  • 42:59 - 43:03
    Copying just so you could
  • 43:03 - 43:05
    hand it off to a function to print
  • 43:05 - 43:09
    it would be unnecessary, and so by using the reference parameter in those situations, you're
  • 43:09 - 43:12
    actually just allowing it to avoid that copy overhead
  • 43:12 - 43:16
    and still get access to the data. Sometimes, you'll see it used both for changing as well as
  • 43:16 - 43:18
    efficiency. I'm
  • 43:18 - 43:21
    not going to show my last slide, but that's what we're going to be talking about on Monday. We'll talk about
  • 43:21 - 43:23
    libraries. Looking at chapter three is the place to go in the reader. I'm going
  • 43:23 - 43:26
    to be walking over to Terman
  • 43:26 - 43:28
    momentarily, and I would love to have somebody come and hang out with me. Otherwise,
  • 43:28 - 43:32
    have a good weekend. You have nothing to do for me. Enjoy it. It's the last weekend in
  • 43:32 - 43:39
    which you will not be
Title:
Lecture 2 | Programming Abstractions (Stanford)
Description:

Lecture two by Julie Zelenski for the Programming Abstractions Course (CS106B) in the Stanford Computer Science Department.

Julie describes the similarities between C++ and Java, which include general syntax, primitive variable types, operators and control structures; she proceeds to go through the code of a basic C++ program and explains each individual piece of code, headers, global constants, global data types, and calling functions. She also proceeds to write a simple program during the lecture that gets input from the user and prints a statement to the screen.

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:
43:48
N. Ueda edited English subtitles for Lecture 2 | Programming Abstractions (Stanford)
Eunjeong_Kim added a translation

English subtitles

Revisions