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