1 00:00:00,000 --> 00:00:10,000 2 00:00:10,000 --> 00:00:14,000 This presentation is delivered by Stanford Center for Professional 3 00:00:14,000 --> 00:00:23,000 Development. 4 00:00:23,000 --> 00:00:25,000 C++ and Java. This is 5 00:00:25,000 --> 00:00:28,000 Java. It's like a death 6 00:00:28,000 --> 00:00:31,000 match. What's the same? The first thing I want to talk about is that there are a lot of things 7 00:00:31,000 --> 00:00:33,000 that are the same, 8 00:00:33,000 --> 00:00:34,000 and so 9 00:00:34,000 --> 00:00:37,000 you are building on a background that is going to serve you well 10 00:00:37,000 --> 00:00:39,000 without having to make a lot of 11 00:00:39,000 --> 00:00:42,000 adjustments. They're almost so similar that at times, it can be a little 12 00:00:42,000 --> 00:00:45,000 bit unhelpful in the sense that it's easy to get tripped up because they 13 00:00:45,000 --> 00:00:48,000 almost but not quite are exactly the same. There was one unfortunate quarter 14 00:00:48,000 --> 00:00:52,000 where I was teaching two classes ? one that was being taught in C++ and one that was in 15 00:00:52,000 --> 00:00:53,000 Java, and I was a mess the whole 16 00:00:53,000 --> 00:00:57,000 quarter. 17 00:00:57,000 --> 00:01:00,000 Luckily, I'm not doing that now, so hopefully, I'll have my C++ hat on 18 00:01:00,000 --> 00:01:01,000 straight the whole quarter. 19 00:01:01,000 --> 00:01:05,000 The general syntax ? I'm going to mention these things, and then I'm going to show 20 00:01:05,000 --> 00:01:07,000 you a sample program. 21 00:01:07,000 --> 00:01:11,000 Things like the comment sequence, the slash star or the slash slash 22 00:01:11,000 --> 00:01:11,000 ? 23 00:01:11,000 --> 00:01:15,000 the use of almost all the punctuation characters 24 00:01:15,000 --> 00:01:16,000 25 00:01:16,000 --> 00:01:20,000 is exactly the same. The statement terminator that the 26 00:01:20,000 --> 00:01:22,000 semicolon is used for, the commas to separate arguments going into a function 27 00:01:22,000 --> 00:01:23,000 call, 28 00:01:23,000 --> 00:01:26,000 the way you make variable and parameter declarations ? 29 00:01:26,000 --> 00:01:28,000 exactly the same. 30 00:01:28,000 --> 00:01:30,000 31 00:01:30,000 --> 00:01:34,000 All of the primitive variable types ? char, int, double, plus the kind of more esoteric 32 00:01:34,000 --> 00:01:36,000 log and float and short and things like that 33 00:01:36,000 --> 00:01:39,000 that are present in Java are present in C++ and have 34 00:01:39,000 --> 00:01:41,000 basically the same semantics. 35 00:01:41,000 --> 00:01:42,000 There is one 36 00:01:42,000 --> 00:01:47,000 minor adjustment there, which is Java's Boolean type, the true/false variable type is actually 37 00:01:47,000 --> 00:01:50,000 called Bool in C++, so you 38 00:01:50,000 --> 00:01:52,000 have to type a few less characters 39 00:01:52,000 --> 00:01:55,000 on that end. 40 00:01:55,000 --> 00:01:59,000 All of the operators that manipulate the basic types, the assignment operator [inaudible] the 41 00:01:59,000 --> 00:02:01,000 equal sign, the 42 00:02:01,000 --> 00:02:04,000 equals equals to compare, not equals to see if they're not equal, the arithmetic 43 00:02:04,000 --> 00:02:10,000 plus, the plus equal, multiply equal shorthands, the plus plus that 44 00:02:10,000 --> 00:02:12,000 comes in both pre and post increment, 45 00:02:12,000 --> 00:02:15,000 the relational less than, less than or equal to, 46 00:02:15,000 --> 00:02:18,000 the logical operators and/or have the same behaviors. They have the same short 47 00:02:18,000 --> 00:02:23,000 circuiting, the same precedence table, so all of that is 48 00:02:23,000 --> 00:02:25,000 without change. 49 00:02:25,000 --> 00:02:29,000 The control structure ? so for redirecting control flow and making choices about what 50 00:02:29,000 --> 00:02:30,000 code to execute when, 51 00:02:30,000 --> 00:02:35,000 the floor and [inaudible] mechanisms, the if with the optional else, the switch 52 00:02:35,000 --> 00:02:36,000 statement, which you may or may not have 53 00:02:36,000 --> 00:02:39,000 seen, but you can definitely look up in the text if you haven't ? that 54 00:02:39,000 --> 00:02:42,000 kind of has a 55 00:02:42,000 --> 00:02:42,000 56 00:02:42,000 --> 00:02:45,000 variant of the if else, so it lets you decide based on the value of something which case 57 00:02:45,000 --> 00:02:46,000 to move into, 58 00:02:46,000 --> 00:02:49,000 the return that's used for the function ? also 59 00:02:49,000 --> 00:02:54,000 things like the break and continue, a little less known keywords there or the 60 00:02:54,000 --> 00:02:57,000 do while ? they work the same way 61 00:02:57,000 --> 00:02:59,000 62 00:02:59,000 --> 00:03:00,000 63 00:03:00,000 --> 00:03:01,000 64 00:03:01,000 --> 00:03:04,000 in Java and C++. 65 00:03:04,000 --> 00:03:04,000 Let's 66 00:03:04,000 --> 00:03:06,000 look at a complete C++ program. 67 00:03:06,000 --> 00:03:09,000 This one is printed in handout four, if you want to follow along there and 68 00:03:09,000 --> 00:03:12,000 make notes on it as we talk you through. 69 00:03:12,000 --> 00:03:14,000 What I'm going to do here is I'm actually planning on just 70 00:03:14,000 --> 00:03:16,000 walking through this line by line 71 00:03:16,000 --> 00:03:19,000 to kind of point out some of the things that are different. Then I'm going to move to the 72 00:03:19,000 --> 00:03:22,000 compiler, and we'll do a little bit of experimentation with developing and 73 00:03:22,000 --> 00:03:25,000 writing code and manipulating something that looks like this to 74 00:03:25,000 --> 00:03:26,000 get some 75 00:03:26,000 --> 00:03:28,000 of our bearings about what 76 00:03:28,000 --> 00:03:30,000 things are going on. 77 00:03:30,000 --> 00:03:32,000 First off, 78 00:03:32,000 --> 00:03:34,000 just some general structure. 79 00:03:34,000 --> 00:03:35,000 A C++ program 80 00:03:35,000 --> 00:03:39,000 is traditionally written in a text file with a suffix some name.CPP. There's actually 81 00:03:39,000 --> 00:03:40,000 82 00:03:40,000 --> 00:03:45,000 some other names that get used like .CC or .C 83 00:03:45,000 --> 00:03:47,000 that are also accepted by a lot of compilers. 84 00:03:47,000 --> 00:03:51,000 Some text extension that identifies that this file has 85 00:03:51,000 --> 00:03:54,000 C++ code ? we'll use CPP. 86 00:03:54,000 --> 00:03:58,000 The program is traditionally in a simple form just 87 00:03:58,000 --> 00:04:03,000 one file with all of the code that's necessary to be in there. Let's take a 88 00:04:03,000 --> 00:04:04,000 look at it. 89 00:04:04,000 --> 00:04:07,000 This one starts with a comment ? always a good idea. 90 00:04:07,000 --> 00:04:10,000 This 91 00:04:10,000 --> 00:04:14,000 is average.CPP, and it adds scores and prints their average. 92 00:04:14,000 --> 00:04:18,000 It's just like all the good style habits that you have 93 00:04:18,000 --> 00:04:20,000 in Java. 94 00:04:20,000 --> 00:04:24,000 95 00:04:24,000 --> 00:04:27,000 The next three lines are #include statements. 96 00:04:27,000 --> 00:04:30,000 #include is analogous to the Java import. 97 00:04:30,000 --> 00:04:32,000 The C++ compiler 98 00:04:32,000 --> 00:04:34,000 is pretty rigid about 99 00:04:34,000 --> 00:04:37,000 wanting to see things in the proper order. 100 00:04:37,000 --> 00:04:40,000 Before you start using some facility, it wants to know about it. 101 00:04:40,000 --> 00:04:43,000 That's part of the safety of making sure you're using it correctly. If you 102 00:04:43,000 --> 00:04:46,000 plan on drawing something in the screen and doing some graphics, 103 00:04:46,000 --> 00:04:49,000 it wants to be sure you're making calls to the graphics function correctly with 104 00:04:49,000 --> 00:04:51,000 the right arguments, the right names, the right 105 00:04:51,000 --> 00:04:53,000 arrangements, 106 00:04:53,000 --> 00:04:56,000 and so in order to know that, it has to ahead of time have seen 107 00:04:56,000 --> 00:05:00,000 that bit of structure. The way this is done in C++ is 108 00:05:00,000 --> 00:05:03,000 through these interface or include files. 109 00:05:03,000 --> 00:05:06,000 The #include mechanism is fairly primitive. 110 00:05:06,000 --> 00:05:10,000 It basically says look for a file with the name genlib.h and take 111 00:05:10,000 --> 00:05:12,000 its contents and dump them right here. 112 00:05:12,000 --> 00:05:16,000 The contents of genlib.h and simpio.h 113 00:05:16,000 --> 00:05:18,000 are actually to give you 114 00:05:18,000 --> 00:05:21,000 a little insight. We're going to see them more later. 115 00:05:21,000 --> 00:05:24,000 It lists the operations and facilities that are available in this 116 00:05:24,000 --> 00:05:28,000 particular header that we're trying to include. In this case, the genlib is a 106 117 00:05:28,000 --> 00:05:29,000 specific header 118 00:05:29,000 --> 00:05:33,000 that all our programs will include. It gets us set up with some basic 119 00:05:33,000 --> 00:05:34,000 foundations. The simpio 120 00:05:34,000 --> 00:05:37,000 is our simplified IO header 121 00:05:37,000 --> 00:05:41,000 that adds some features for interacting with the user at the console. 122 00:05:41,000 --> 00:05:44,000 The next one down there is include . is a C++ 123 00:05:44,000 --> 00:05:46,000 standard header, 124 00:05:46,000 --> 00:05:47,000 and that 125 00:05:47,000 --> 00:05:50,000 helps to point out the difference between why in one place I was using 126 00:05:50,000 --> 00:05:54,000 double quotes to name the header file I'm looking for and in one place I'm using 127 00:05:54,000 --> 00:05:55,000 angle brackets. 128 00:05:55,000 --> 00:05:56,000 This is a 129 00:05:56,000 --> 00:06:01,000 way that's distinguished for what header files are, what are called system standard, and 130 00:06:01,000 --> 00:06:04,000 it looks for them in a particular place where the standard header files are. Those are 131 00:06:04,000 --> 00:06:06,000 always enclosed in the angle brackets. 132 00:06:06,000 --> 00:06:10,000 In double quotes are local headers that are specific to this 133 00:06:10,000 --> 00:06:13,000 project or this environment that are not C++ standard. 134 00:06:13,000 --> 00:06:16,000 Whenever you see these double quotes, you'll think it's my own local headers or headers 135 00:06:16,000 --> 00:06:18,000 from the CS106 library. 136 00:06:18,000 --> 00:06:23,000 Anything in angle braces is a system standard header you'd find everywhere. 137 00:06:23,000 --> 00:06:26,000 It matches what you do in import. You say I'm going to be using these 138 00:06:26,000 --> 00:06:29,000 features in this class. Let me import its features into here 139 00:06:29,000 --> 00:06:34,000 so that the compiler knows about them. 140 00:06:34,000 --> 00:06:36,000 The next line I've got here 141 00:06:36,000 --> 00:06:39,000 is a declaration of a constant. 142 00:06:39,000 --> 00:06:40,000 This is the const 143 00:06:40,000 --> 00:06:42,000 whose name is NumScores, 144 00:06:42,000 --> 00:06:44,000 and so it looks a little bit like a variable declaration. 145 00:06:44,000 --> 00:06:48,000 From this point over, you see I have int NumScores = 4;. 146 00:06:48,000 --> 00:06:50,000 It looks a lot like a variable operation. 147 00:06:50,000 --> 00:06:52,000 The const in the front of it 148 00:06:52,000 --> 00:06:55,000 is our way of telling the complier that this particular value 149 00:06:55,000 --> 00:06:58,000 once assigned will not change. 150 00:06:58,000 --> 00:06:59,000 It is 151 00:06:59,000 --> 00:07:03,000 equivalent to kind of the use of final in the Java language 152 00:07:03,000 --> 00:07:04,000 in many ways. 153 00:07:04,000 --> 00:07:05,000 154 00:07:05,000 --> 00:07:07,000 This is being declared 155 00:07:07,000 --> 00:07:11,000 outside of context. One point to make here is that 156 00:07:11,000 --> 00:07:13,000 C++ 157 00:07:13,000 --> 00:07:15,000 drives on more than one 158 00:07:15,000 --> 00:07:19,000 way of expressing code and what we call its paradigm. Java is the completely 159 00:07:19,000 --> 00:07:21,000 object oriented language. 160 00:07:21,000 --> 00:07:24,000 Every line of code you wrote went inside a class. It's all about classes. You wrote this 161 00:07:24,000 --> 00:07:27,000 class that manipulated the thermometer. You wrote this class that 162 00:07:27,000 --> 00:07:29,000 drew with the 163 00:07:29,000 --> 00:07:33,000 turtle. There was no code that ever existed outside of a class. 164 00:07:33,000 --> 00:07:36,000 You started off with public class something and you started writing methods 165 00:07:36,000 --> 00:07:39,000 that operated on that class. Maybe they were static or maybe not. 166 00:07:39,000 --> 00:07:43,000 The way you started code was always to say start from this class' 167 00:07:43,000 --> 00:07:46,000 main to get work done. 168 00:07:46,000 --> 00:07:48,000 C++ is a hybrid language. 169 00:07:48,000 --> 00:07:51,000 It has object oriented features. It has the ability to find and use classes, and we'll 170 00:07:51,000 --> 00:07:52,000 see that 171 00:07:52,000 --> 00:07:54,000 quite extensively, 172 00:07:54,000 --> 00:07:55,000 but it also 173 00:07:55,000 --> 00:08:00,000 inherits the legacy of C, which is pre this whole object oriented 174 00:08:00,000 --> 00:08:00,000 development 175 00:08:00,000 --> 00:08:04,000 where it operates more procedurally, they call 176 00:08:04,000 --> 00:08:05,000 it, 177 00:08:05,000 --> 00:08:09,000 where there are functions that exist at the global level 178 00:08:09,000 --> 00:08:09,000 that 179 00:08:09,000 --> 00:08:13,000 don't have a context of a class that they operate in. There's not some [inaudible] 180 00:08:13,000 --> 00:08:16,000 that's being received. They're just functions that 181 00:08:16,000 --> 00:08:17,000 exist outside. It's a little 182 00:08:17,000 --> 00:08:19,000 set of things to do 183 00:08:19,000 --> 00:08:21,000 that don't live inside a class. 184 00:08:21,000 --> 00:08:25,000 For much of the term, we're going to be doing stuff in a very procedural way, 185 00:08:25,000 --> 00:08:26,000 at least from the code we write, 186 00:08:26,000 --> 00:08:29,000 and we will use a lot of objects. We'll actually be 187 00:08:29,000 --> 00:08:30,000 188 00:08:30,000 --> 00:08:33,000 mixing and matching a little bit of those paradigms together, so using the 189 00:08:33,000 --> 00:08:37,000 procedural paradigm to write a lot of code that happens to manipulate objects 190 00:08:37,000 --> 00:08:39,000 using an object oriented 191 00:08:39,000 --> 00:08:41,000 set of features. 192 00:08:41,000 --> 00:08:42,000 In this case, 193 00:08:42,000 --> 00:08:45,000 the program here ? this is actually up at the top level, and it's saying this is a 194 00:08:45,000 --> 00:08:49,000 global constant that's available anywhere in the file. After this declaration, I can 195 00:08:49,000 --> 00:08:51,000 use the NumScores, 196 00:08:51,000 --> 00:08:52,000 which will refer back to this 197 00:08:52,000 --> 00:08:54,000 without 198 00:08:54,000 --> 00:08:57,000 any further adornment. 199 00:08:57,000 --> 00:08:59,000 The next thing underneath that 200 00:08:59,000 --> 00:09:02,000 is a prototype. 201 00:09:02,000 --> 00:09:04,000 This is actually a little bit of a novelty 202 00:09:04,000 --> 00:09:05,000 203 00:09:05,000 --> 00:09:07,000 that in Java 204 00:09:07,000 --> 00:09:09,000 you are allowed to declare 205 00:09:09,000 --> 00:09:12,000 if you have three methods, A, B and C, and maybe A calls B and B calls C, you could declare A, B 206 00:09:12,000 --> 00:09:15,000 and C in any order that's convenient for you. You want to put them 207 00:09:15,000 --> 00:09:18,000 alphabetically? You want to put them in the order they're called? You want to put them 208 00:09:18,000 --> 00:09:19,000 in the order 209 00:09:19,000 --> 00:09:22,000 top to bottom down or bottom up? 210 00:09:22,000 --> 00:09:25,000 It doesn't matter. C++ tends 211 00:09:25,000 --> 00:09:28,000 to want to look 212 00:09:28,000 --> 00:09:31,000 at a file once top to 213 00:09:31,000 --> 00:09:34,000 bottom and not have any 214 00:09:34,000 --> 00:09:35,000 reason to go back and revisit things. 215 00:09:35,000 --> 00:09:39,000 It happens that in the main function, which is the one where code starts from, 216 00:09:39,000 --> 00:09:43,000 I want to make a call to this GetScoresAndAverage function. 217 00:09:43,000 --> 00:09:46,000 If I had left this prototype off and I had this code here, 218 00:09:46,000 --> 00:09:49,000 when it gets to this line where I'm making that call, the compiler hasn't yet seen 219 00:09:49,000 --> 00:09:53,000 anything about GetScoresAndAverage. It's on the next page. 220 00:09:53,000 --> 00:09:56,000 It will complain. It will say I don't know what the function is. You haven't told me about it. 221 00:09:56,000 --> 00:09:58,000 It came out of nowhere. 222 00:09:58,000 --> 00:10:02,000 This prototype is our way of informing the compiler about something 223 00:10:02,000 --> 00:10:05,000 that's coming up later, and so the prototype tells about the return type, 224 00:10:05,000 --> 00:10:07,000 the name of the function, 225 00:10:07,000 --> 00:10:10,000 the type and names of the arguments and the order of them and then ends 226 00:10:10,000 --> 00:10:12,000 with a semicolon. 227 00:10:12,000 --> 00:10:15,000 It matches exactly the function header that we'll see on the next page, 228 00:10:15,000 --> 00:10:18,000 and it just tells the compiler in advance here's something that's coming 229 00:10:18,000 --> 00:10:23,000 up later in the file. It's a function with this name and this signature. 230 00:10:23,000 --> 00:10:26,000 It's something you didn't have to do in Java but is a part of 231 00:10:26,000 --> 00:10:28,000 the 232 00:10:28,000 --> 00:10:29,000 C++ world. 233 00:10:29,000 --> 00:10:31,000 There's actually an alternate way I could have done this, 234 00:10:31,000 --> 00:10:34,000 which is when I show you the code for GetScoresAndAverage, if I actually 235 00:10:34,000 --> 00:10:35,000 defined them in the other order, 236 00:10:35,000 --> 00:10:38,000 I could avoid that separate prototype. 237 00:10:38,000 --> 00:10:43,000 It's a little bit of a matter of personal taste which way 238 00:10:43,000 --> 00:10:45,000 you feel comfortable doing it. 239 00:10:45,000 --> 00:10:49,000 Let's look at this guy. 240 00:10:49,000 --> 00:10:50,000 Main is special. 241 00:10:50,000 --> 00:10:54,000 Lowercase 242 00:10:54,000 --> 00:10:58,000 m-a-i-n 243 00:10:58,000 --> 00:11:00,000 244 00:11:00,000 --> 00:11:03,000 has no arguments and returns an integer type. 245 00:11:03,000 --> 00:11:06,000 This is the function in the C++ program where execution 246 00:11:06,000 --> 00:11:09,000 starts. There can be exactly one main function 247 00:11:09,000 --> 00:11:11,000 with this name and this signature, 248 00:11:11,000 --> 00:11:13,000 and when the 249 00:11:13,000 --> 00:11:16,000 program gets up and running, the first thing it does is start executing the 250 00:11:16,000 --> 00:11:20,000 lines of code that come in this function. No objects are created or any kind of 251 00:11:20,000 --> 00:11:23,000 fancy outer structure the way it might be in Java. It immediately goes here 252 00:11:23,000 --> 00:11:25,000 and starts doing what you told it to do. 253 00:11:25,000 --> 00:11:28,000 Main has a special role to play in 254 00:11:28,000 --> 00:11:31,000 any program. 255 00:11:31,000 --> 00:11:32,000 What this program 256 00:11:32,000 --> 00:11:33,000 does 257 00:11:33,000 --> 00:11:35,000 is first prints 258 00:11:35,000 --> 00:11:39,000 something. This is the C++ syntax for printing things out. Even if 259 00:11:39,000 --> 00:11:41,000 you haven't ever seen it, 260 00:11:41,000 --> 00:11:44,000 you probably can get a little bit of an idea of what it's trying to do if you just 261 00:11:44,000 --> 00:11:46,000 step back from it and squint a little. It looks a little like system.out.print 262 00:11:46,000 --> 00:11:48,000 263 00:11:48,000 --> 00:11:49,000 lin. 264 00:11:49,000 --> 00:11:54,000 This is what's called the consult out, or the standard out that's here on the left-hand 265 00:11:54,000 --> 00:11:55,000 side. 266 00:11:55,000 --> 00:12:00,000 The use of the double less than here is what's called the stream 267 00:12:00,000 --> 00:12:04,000 insertion operator. The standard output operation is saying insert into that 268 00:12:04,000 --> 00:12:05,000 stream 269 00:12:05,000 --> 00:12:07,000 and then in the double quotes is a string. 270 00:12:07,000 --> 00:12:09,000 This program averages. 271 00:12:09,000 --> 00:12:14,000 It says insert next to that the NumScores, which is this constant. It 272 00:12:14,000 --> 00:12:16,000 got the value from above. And then another string, 273 00:12:16,000 --> 00:12:19,000 and at the very end, there's this little endl, 274 00:12:19,000 --> 00:12:22,000 which is the end line 275 00:12:22,000 --> 00:12:26,000 insertion of what's called a stream manipulator. It says at the end of this, 276 00:12:26,000 --> 00:12:28,000 put a new line and start any 277 00:12:28,000 --> 00:12:30,000 subsequent text on the next line. In 278 00:12:30,000 --> 00:12:32,000 Java, that would look like a system.out.print lin of this 279 00:12:32,000 --> 00:12:37,000 program averages plus the NumScores plus that using the string 280 00:12:37,000 --> 00:12:40,000 [inaudible] and conversion stuff in Java. It looks a little bit different in 281 00:12:40,000 --> 00:12:42,000 C++, but in the end result, 282 00:12:42,000 --> 00:12:45,000 it's a matter of printing out a bunch of numbers and strings 283 00:12:45,000 --> 00:12:47,000 intermingled with new lines. 284 00:12:47,000 --> 00:12:53,000 Nothing too fancy there. The 285 00:12:53,000 --> 00:12:57,000 next line is making that call to that function that we earlier told it about 286 00:12:57,000 --> 00:12:59,000 but we haven't yet seen the code for 287 00:12:59,000 --> 00:13:02,000 that makes a call to get scores and averages. It passes the constant NumScores 288 00:13:02,000 --> 00:13:05,000 and says that's how many scores that parameter has used to decide 289 00:13:05,000 --> 00:13:08,000 how many iterations to run the loop requesting the values. Then it 290 00:13:08,000 --> 00:13:10,000 returns 291 00:13:10,000 --> 00:13:11,000 the average of the values entered 292 00:13:11,000 --> 00:13:14,000 and then we take that number. This just shows 293 00:13:14,000 --> 00:13:17,000 that in C++, you can declare 294 00:13:17,000 --> 00:13:18,000 variables anywhere, 295 00:13:18,000 --> 00:13:21,000 either at the start of a block, in the middle of a block or wherever they are at. 296 00:13:21,000 --> 00:13:27,000 We took that value, stored it in this 297 00:13:27,000 --> 00:13:30,000 variable and then used it in the next print statement. 298 00:13:30,000 --> 00:13:33,000 The last thing we have is a return zero. 299 00:13:33,000 --> 00:13:34,000 The 300 00:13:34,000 --> 00:13:37,000 signature for main in C++ always returns an integer. 301 00:13:37,000 --> 00:13:39,000 That integer is 302 00:13:39,000 --> 00:13:42,000 not all that useful in most situations. It tended to be a kind of 303 00:13:42,000 --> 00:13:45,000 return that tells the 304 00:13:45,000 --> 00:13:48,000 operating system who invoked the program whether everything completed 305 00:13:48,000 --> 00:13:49,000 successfully or not. 306 00:13:49,000 --> 00:13:52,000 By default, the value that's used to show successful completion is zero, 307 00:13:52,000 --> 00:13:56,000 so unless you have some other reason, it is return zero that you'll always see at the end 308 00:13:56,000 --> 00:13:57,000 there. 309 00:13:57,000 --> 00:14:00,000 In reality, if you were to return something else, 310 00:14:00,000 --> 00:14:03,000 you won't see a lot of other differences. If you return -1 311 00:14:03,000 --> 00:14:05,000 or 642, 312 00:14:05,000 --> 00:14:08,000 in most contexts, that number's almost ignored. 313 00:14:08,000 --> 00:14:13,000 It is there just to make tidy. 314 00:14:13,000 --> 00:14:16,000 I'm about to flip to the next part, 315 00:14:16,000 --> 00:14:18,000 and this is the 316 00:14:18,000 --> 00:14:21,000 decomposed function that main made a call out to that does the 317 00:14:21,000 --> 00:14:23,000 318 00:14:23,000 --> 00:14:26,000 averaging of the scores. We've got a comment on the top that tells a 319 00:14:26,000 --> 00:14:27,000 little bit about how it works. 320 00:14:27,000 --> 00:14:28,000 It's 321 00:14:28,000 --> 00:14:32,000 always a good idea to get some documentation in there that's 322 00:14:32,000 --> 00:14:36,000 helping the reader get oriented. That same matching of the prototype that was given 323 00:14:36,000 --> 00:14:37,000 earlier ? 324 00:14:37,000 --> 00:14:40,000 same name, same argument, same things, but instead of ending with that semicolon, 325 00:14:40,000 --> 00:14:41,000 it goes into the body. 326 00:14:41,000 --> 00:14:45,000 Sometimes we'll call these very similar terms ? this one we will call the 327 00:14:45,000 --> 00:14:49,000 definition or the implementation of a function, and that earlier 328 00:14:49,000 --> 00:14:52,000 just introduction of it is called its declaration or its prototype. 329 00:14:52,000 --> 00:14:55,000 They should match, and they're just 330 00:14:55,000 --> 00:15:01,000 there to give warning of something coming up later in the file. It 331 00:15:01,000 --> 00:15:02,000 initializes a sum 332 00:15:02,000 --> 00:15:06,000 to zero. It runs a standard four loop here. Like 333 00:15:06,000 --> 00:15:10,000 Java, we tend to count from zero as computer scientists 334 00:15:10,000 --> 00:15:14,000 for a long legacy reason. Instead of 335 00:15:14,000 --> 00:15:17,000 going from one to NumScores, we actually go from zero to 336 00:15:17,000 --> 00:15:19,000 NumScores minus one. 337 00:15:19,000 --> 00:15:19,000 338 00:15:19,000 --> 00:15:24,000 Each time through the loop, we are prompting the user to 339 00:15:24,000 --> 00:15:25,000 give us a score. We 340 00:15:25,000 --> 00:15:29,000 are reading their score with a GetInteger call, 341 00:15:29,000 --> 00:15:32,000 and we'll see what GetInteger is. GetInteger is a CS106 function 342 00:15:32,000 --> 00:15:35,000 that retrieves a number that the user types in the console. 343 00:15:35,000 --> 00:15:41,000 We add that into the sum and keep going, and after we've done the whole thing, 344 00:15:41,000 --> 00:15:47,000 at the very end, we're doing a division of the sum by the number of scores to 345 00:15:47,000 --> 00:15:51,000 compute the average score that was [inaudible] and return that. Did 346 00:15:51,000 --> 00:15:54,000 you say why we didn't 347 00:15:54,000 --> 00:15:56,000 have end 348 00:15:56,000 --> 00:15:59,000 line? In this case, I didn't have an end line just because I think it makes it 349 00:15:59,000 --> 00:16:02,000 nicer for the user. If you say next score and end line, then you're typing on the line 350 00:16:02,000 --> 00:16:05,000 underneath it, and so if you don't put the end line there, it's like you type 351 00:16:05,000 --> 00:16:07,000 right next to the question mark, 352 00:16:07,000 --> 00:16:09,000 and so it actually it an aesthetic 353 00:16:09,000 --> 00:16:11,000 thing. 354 00:16:11,000 --> 00:16:12,000 If 355 00:16:12,000 --> 00:16:15,000 I put it there, it would say next score and then I would be typing underneath it, and 356 00:16:15,000 --> 00:16:22,000 instead, I'm typing right next to it. I hit the return and then the next 357 00:16:22,000 --> 00:16:26,000 one comes on the 358 00:16:26,000 --> 00:16:30,000 next line. So [inaudible] ? and I will talk about that a little bit later today, but it 359 00:16:30,000 --> 00:16:32,000 is ? 360 00:16:32,000 --> 00:16:34,000 that's a good question but a little bit advanced, so 361 00:16:34,000 --> 00:16:36,000 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 362 00:16:36,000 --> 00:16:40,000 363 00:16:40,000 --> 00:16:42,000 with 364 00:16:42,000 --> 00:16:46,000 return zero? Pretty much. As I said, you could put return anything, 365 00:16:46,000 --> 00:16:48,000 but as good form, 366 00:16:48,000 --> 00:16:54,000 return zero is a way of saying I successfully completed my job 367 00:16:54,000 --> 00:16:56,000 and I'm done. Do you 368 00:16:56,000 --> 00:16:59,000 use a void return type for 369 00:16:59,000 --> 00:17:01,000 the main? In C++, no. In C, 370 00:17:01,000 --> 00:17:04,000 it's a little more lenient about that, and it will allow 371 00:17:04,000 --> 00:17:08,000 that. In C++, 372 00:17:08,000 --> 00:17:12,000 [inaudible]. I have to decide if I can get this 373 00:17:12,000 --> 00:17:13,000 374 00:17:13,000 --> 00:17:15,000 to you without 375 00:17:15,000 --> 00:17:16,000 hurting you. Let me 376 00:17:16,000 --> 00:17:20,000 do a little coding with you guys 377 00:17:20,000 --> 00:17:22,000 just to play 378 00:17:22,000 --> 00:17:25,000 around with this 379 00:17:25,000 --> 00:17:29,000 and show you the complier. I'd like to do a little bit of 380 00:17:29,000 --> 00:17:35,000 this. I think it's really easy for me. I talk really fast. You might have noticed. When 381 00:17:35,000 --> 00:17:39,000 I get going on something, it's easier for me to put up a lot of code on a slide and say 382 00:17:39,000 --> 00:17:42,000 here it is. It's all fully formed. The reality is when you're 383 00:17:42,000 --> 00:17:44,000 running your programs yourself, 384 00:17:44,000 --> 00:17:46,000 it's not going to come to you fully formed on 385 00:17:46,000 --> 00:17:49,000 a slide all ready to go. You're going to have to figure out how do you stack through making this 386 00:17:49,000 --> 00:17:52,000 stuff work? I'm going to do a little bit of development of a program that looks very 387 00:17:52,000 --> 00:17:54,000 similar to the one I just wrote, but I'm going to 388 00:17:54,000 --> 00:17:55,000 play around with it a little bit 389 00:17:55,000 --> 00:17:57,000 to get at 390 00:17:57,000 --> 00:17:59,000 some of the things that are different about C++ and how the tools work. 391 00:17:59,000 --> 00:18:00,000 392 00:18:00,000 --> 00:18:04,000 This is basically what an empty project starts me off with. 393 00:18:04,000 --> 00:18:05,000 I've got 394 00:18:05,000 --> 00:18:06,000 the 395 00:18:06,000 --> 00:18:09,000 include for the genlib, but I'll always have my int main and return zero and nothing 396 00:18:09,000 --> 00:18:10,000 else doing. I'm 397 00:18:10,000 --> 00:18:13,000 going to sit down. I'm going to start typing. 398 00:18:13,000 --> 00:18:16,000 I'm going to say 399 00:18:16,000 --> 00:18:19,000 welcome. 400 00:18:19,000 --> 00:18:20,000 401 00:18:20,000 --> 00:18:23,000 I'm going to 402 00:18:23,000 --> 00:18:26,000 move to ? let's do this. We'll play with this for a second. There 403 00:18:26,000 --> 00:18:27,000 are some things that I can 404 00:18:27,000 --> 00:18:31,000 do. I can say welcome a lot of times. 405 00:18:31,000 --> 00:18:32,000 406 00:18:32,000 --> 00:18:35,000 That wasn't really so hot there. You guys 407 00:18:35,000 --> 00:18:36,000 can help me when I 408 00:18:36,000 --> 00:18:37,000 fail to type correctly. 409 00:18:37,000 --> 00:18:41,000 I say welcome a lot of times. Let's go see and make sure this works. 410 00:18:41,000 --> 00:18:43,000 Look at that. It doesn't like what I did. 411 00:18:43,000 --> 00:18:45,000 Let's see what it has to say. 412 00:18:45,000 --> 00:18:48,000 It says cout was not [inaudible] the scope. Endl was not declared in the scope. 413 00:18:48,000 --> 00:18:51,000 This is an error message you're going to see a lot of times 414 00:18:51,000 --> 00:18:55,000 in various other symbol lanes that will show up there, which is the C++ compiler's 415 00:18:55,000 --> 00:18:57,000 way of saying to you I don't read your mind. 416 00:18:57,000 --> 00:18:59,000 You used cout. You used endl. 417 00:18:59,000 --> 00:19:02,000 These things come from somewhere, but they're not known to me until you 418 00:19:02,000 --> 00:19:05,000 make them known to me. The way I make those known is that those are part of 419 00:19:05,000 --> 00:19:06,000 the 420 00:19:06,000 --> 00:19:08,000 standard IO stream, 421 00:19:08,000 --> 00:19:11,000 which the input/output streams facility for the C++ language, 422 00:19:11,000 --> 00:19:13,000 and once I tell it about those things, it's going to be 423 00:19:13,000 --> 00:19:15,000 much happier, 424 00:19:15,000 --> 00:19:18,000 and it's going to say welcome to me a lot of times. 425 00:19:18,000 --> 00:19:20,000 In fact, I could say this 426 00:19:20,000 --> 00:19:23,000 ? 106B rocks. I'll 427 00:19:23,000 --> 00:19:24,000 do this. 428 00:19:24,000 --> 00:19:26,000 I'll say 429 00:19:26,000 --> 00:19:28,000 how awesome 430 00:19:28,000 --> 00:19:31,000 equals get integer 431 00:19:31,000 --> 00:19:33,000 and I will ? it may help if 432 00:19:33,000 --> 00:19:35,000 I move this up a little bit so we can see 433 00:19:35,000 --> 00:19:37,000 434 00:19:37,000 --> 00:19:38,000 how much do 435 00:19:38,000 --> 00:19:41,000 you love 106B? 436 00:19:41,000 --> 00:19:43,000 Here, I won't use my endl, so we'll see how the prompt comes out. I'm going to get an integer 437 00:19:43,000 --> 00:19:47,000 from them, and I'm going to use that 438 00:19:47,000 --> 00:19:49,000 to 439 00:19:49,000 --> 00:19:51,000 write how awesome that many times. 440 00:19:51,000 --> 00:19:54,000 What does it not like about that? Again, it 441 00:19:54,000 --> 00:19:57,000 says get integer not declared in the scope. This is going to be the bane of our existence today, which 442 00:19:57,000 --> 00:19:58,000 is remembering 443 00:19:58,000 --> 00:20:03,000 that there are headers out there that have features that we're going to be using, and getting 444 00:20:03,000 --> 00:20:06,000 familiar with the ones that we're gonna need ? there are probably about 445 00:20:06,000 --> 00:20:09,000 ten total that we'll use again and again, and we have to get 446 00:20:09,000 --> 00:20:12,000 our bearings about which ones are where. The documentation for all of our CS106 447 00:20:12,000 --> 00:20:17,000 headers is available on the website in a nice HTML-ized version, 448 00:20:17,000 --> 00:20:21,000 and the ones from the standard libraries are 449 00:20:21,000 --> 00:20:26,000 detailed in the book in chapter three. 450 00:20:26,000 --> 00:20:28,000 How much do 451 00:20:28,000 --> 00:20:31,000 you love 106B? I could say well, I love it two times. 452 00:20:31,000 --> 00:20:37,000 I say no, I love it 1,900 and 453 00:20:37,000 --> 00:20:39,000 so. 10,000 454 00:20:39,000 --> 00:20:42,000 times later, if you're not convinced now, I don't know what will convince 455 00:20:42,000 --> 00:20:46,000 you. We're seeing some stuff. 456 00:20:46,000 --> 00:20:47,000 Let's see 457 00:20:47,000 --> 00:20:51,000 if we can make that averaging thing work for us. I'll go 458 00:20:51,000 --> 00:20:54,000 back to having it say welcome. 459 00:20:54,000 --> 00:20:57,000 460 00:20:57,000 --> 00:21:00,000 That's what the endl was doing for me there. The prompt is 461 00:21:00,000 --> 00:21:02,000 waiting at the end of that line. If I have the endl, it will actually be 462 00:21:02,000 --> 00:21:05,000 waiting on the 463 00:21:05,000 --> 00:21:10,000 next line. Let 464 00:21:10,000 --> 00:21:12,000 me 465 00:21:12,000 --> 00:21:16,000 put a bogus message here. Then I'm going to say double average equals GetScoresAndAverage 466 00:21:16,000 --> 00:21:18,000 467 00:21:18,000 --> 00:21:19,000 468 00:21:19,000 --> 00:21:21,000 and then let's 469 00:21:21,000 --> 00:21:23,000 go ahead and 470 00:21:23,000 --> 00:21:27,000 ? I'm going to blow off the constant for a little bit because 471 00:21:27,000 --> 00:21:30,000 I'm going to be mucking with it anyway, and I'm not going to use that constant for very long, 472 00:21:30,000 --> 00:21:31,000 so I'm 473 00:21:31,000 --> 00:21:33,000 not going to worry about it 474 00:21:33,000 --> 00:21:36,000 yet. I put my prototype up here 475 00:21:36,000 --> 00:21:39,000 so it knows what I'm talking about, and then I'm going to 476 00:21:39,000 --> 00:21:44,000 print out average is. I've got a little 477 00:21:44,000 --> 00:21:47,000 bit of structure there. I 478 00:21:47,000 --> 00:21:50,000 can even go in here. Copy and paste is a really good idea. Once I've written 479 00:21:50,000 --> 00:21:54,000 the function header once, I don't really want to make a mistake. Something that's 480 00:21:54,000 --> 00:21:56,000 interesting 481 00:21:56,000 --> 00:22:00,000 to know is that you can always write code that 482 00:22:00,000 --> 00:22:03,000 doesn't do what you want to start with but that lets you test 483 00:22:03,000 --> 00:22:06,000 some of the other pieces around it. In this case, I haven't 484 00:22:06,000 --> 00:22:08,000 finished fleshing out what GetScoresAndAverage is, 485 00:22:08,000 --> 00:22:11,000 but I can write code around it that says well, it passes in this number and it does some 486 00:22:11,000 --> 00:22:12,000 stuff. 487 00:22:12,000 --> 00:22:13,000 Right now, it just returns zero. 488 00:22:13,000 --> 00:22:16,000 It's totally bogus. It's not solving any of my problems, but it does allow 489 00:22:16,000 --> 00:22:19,000 me to test some other part of the code 490 00:22:19,000 --> 00:22:20,000 491 00:22:20,000 --> 00:22:23,000 to make sure that connection and other parts are working before I go on 492 00:22:23,000 --> 00:22:25,000 to work on this part in isolation. One 493 00:22:25,000 --> 00:22:28,000 of the things I'll try to show you whenever I'm doing code 494 00:22:28,000 --> 00:22:32,000 is to try to give you a little of the insight to how I approach 495 00:22:32,000 --> 00:22:34,000 thinking about the code. I do think that one of the things that really distinguishes 496 00:22:34,000 --> 00:22:37,000 somebody who works effectively and productively 497 00:22:37,000 --> 00:22:40,000 from someone who spends a lot more time doing it is often just strategies and 498 00:22:40,000 --> 00:22:44,000 tactics for how you attack your problem. I'm hoping that along the way, I can 499 00:22:44,000 --> 00:22:45,000 kind 500 00:22:45,000 --> 00:22:49,000 of share a little bit of the insights that I use to keep myself making forward progress. 501 00:22:49,000 --> 00:22:52,000 I am going to put in that four loop that I'm 502 00:22:52,000 --> 00:22:54,000 looking for. 503 00:22:54,000 --> 00:22:56,000 504 00:22:56,000 --> 00:22:58,000 I'm really not this bad of a typist in real life, but 505 00:22:58,000 --> 00:23:00,000 it's 506 00:23:00,000 --> 00:23:03,000 easy to 507 00:23:03,000 --> 00:23:06,000 ? the use of the braces here is what's 508 00:23:06,000 --> 00:23:09,000 defining the block structure. Without the 509 00:23:09,000 --> 00:23:12,000 open curly brace close curly brace, the four loop and the if and the other 510 00:23:12,000 --> 00:23:16,000 statements only grab that next line as being part of the indented 511 00:23:16,000 --> 00:23:18,000 body of the four loop. 512 00:23:18,000 --> 00:23:21,000 I'm going to go ahead with my 513 00:23:21,000 --> 00:23:21,000 next 514 00:23:21,000 --> 00:23:24,000 idea. I'm going to put that in the sum, 515 00:23:24,000 --> 00:23:29,000 and then I'm going to do some things that 516 00:23:29,000 --> 00:23:33,000 seem bogus. We're going to get there. I'm going to make some mistakes. 517 00:23:33,000 --> 00:23:36,000 Some of them I'll make on purpose. Some of them I'll actually be 518 00:23:36,000 --> 00:23:40,000 making accidentally. We'll see which ones I'm better at finding. I 519 00:23:40,000 --> 00:23:43,000 have my GetScoresAndAverage. It asks them for the scores. It adds it into the 520 00:23:43,000 --> 00:23:46,000 integer and then doesn't return anything. First, let's 521 00:23:46,000 --> 00:23:48,000 see how the compiler feels about that. 522 00:23:48,000 --> 00:23:50,000 The first thing you'll notice is that 523 00:23:50,000 --> 00:23:53,000 compiled. 524 00:23:53,000 --> 00:23:57,000 That might alarm you just a little bit, because if you go back and look at this code, 525 00:23:57,000 --> 00:23:59,000 it certainly seems wrong. 526 00:23:59,000 --> 00:24:02,000 Anybody want to help me with one of the things that's wrong with it? 527 00:24:02,000 --> 00:24:05,000 It doesn't return anything. That seems 528 00:24:05,000 --> 00:24:09,000 a little shocking. It doesn't return anything at all. What is it then 529 00:24:09,000 --> 00:24:14,000 actually going to produce? What is it doing that is a little bit bogus? 530 00:24:14,000 --> 00:24:15,000 Sum is 531 00:24:15,000 --> 00:24:16,000 not initialized. These are two 532 00:24:16,000 --> 00:24:20,000 examples of errors that Java doesn't let you get away with. 533 00:24:20,000 --> 00:24:21,000 You may very well remember from having 534 00:24:21,000 --> 00:24:24,000 typed in some code like this in Java 535 00:24:24,000 --> 00:24:27,000 that it will complain. If you have a variable and you declare it and you don't initialize it and you 536 00:24:27,000 --> 00:24:30,000 start using it, the compiler says not happening. You need to go back there 537 00:24:30,000 --> 00:24:33,000 and give that guy an initial value before you start reading it. 538 00:24:33,000 --> 00:24:36,000 Similarly, you fall off the end of a function. It said it was going to return a 539 00:24:36,000 --> 00:24:36,000 double ? 540 00:24:36,000 --> 00:24:40,000 you better return a double. Every code path needs to return a double. You can't 541 00:24:40,000 --> 00:24:44,000 bail out early in some cases and leave it fall off the end. It 542 00:24:44,000 --> 00:24:46,000 considers both of those 543 00:24:46,000 --> 00:24:48,000 big enough errors that it won't let you get away with them. 544 00:24:48,000 --> 00:24:52,000 C++ compilers are a little bit more lax. It's crack mom, I told you. 545 00:24:52,000 --> 00:24:54,000 It's like well, if you don't want to initialize that, 546 00:24:54,000 --> 00:25:00,000 go ahead. What value does it have 547 00:25:00,000 --> 00:25:02,000 then? 548 00:25:02,000 --> 00:25:06,000 It doesn't make it zero. It 549 00:25:06,000 --> 00:25:09,000 doesn't make it empty. It doesn't do anything nice or clever or 550 00:25:09,000 --> 00:25:14,000 helpful. It's like whatever kind of contents were lying around in that particular location, 551 00:25:14,000 --> 00:25:16,000 now it's [inaudible] initial 552 00:25:16,000 --> 00:25:19,000 value. The result will be that we could get some pretty strange results. If I run this 553 00:25:19,000 --> 00:25:22,000 thing right now ? 554 00:25:22,000 --> 00:25:24,000 let's see. I type in some numbers. I type in 555 00:25:24,000 --> 00:25:27,000 somebody got a nine, somebody got a 67, somebody got an 87. 556 00:25:27,000 --> 00:25:31,000 I type in a 557 00:25:31,000 --> 00:25:35,000 bunch of numbers, and the average is zero. 558 00:25:35,000 --> 00:25:36,000 559 00:25:36,000 --> 00:25:39,000 That was interesting. Zero seems like it had some rhyme or reason to 560 00:25:39,000 --> 00:25:43,000 it, but there's no guarantee. I could run it again and I 561 00:25:43,000 --> 00:25:50,000 might get very different numbers. I got zero the 562 00:25:50,000 --> 00:25:52,000 second 563 00:25:52,000 --> 00:25:53,000 time. 564 00:25:53,000 --> 00:25:56,000 Good luck for me. 565 00:25:56,000 --> 00:25:57,000 Let's go in and start fixing some of this stuff. 566 00:25:57,000 --> 00:26:01,000 The thing to note is the compiler isn't nearly as aggressive about being 567 00:26:01,000 --> 00:26:05,000 on your case about things, so it does require a little bit more self-monitoring 568 00:26:05,000 --> 00:26:07,000 on some of these things. Seeing this 569 00:26:07,000 --> 00:26:11,000 result and going back and figuring out how I got into this situation 570 00:26:11,000 --> 00:26:13,000 ? let me 571 00:26:13,000 --> 00:26:14,000 get that guy an initial 572 00:26:14,000 --> 00:26:22,000 value, and I'll go ahead and put a return down here. 573 00:26:22,000 --> 00:26:25,000 We'll see if I like how this works any 574 00:26:25,000 --> 00:26:27,000 better. I put in five, 575 00:26:27,000 --> 00:26:28,000 six, 576 00:26:28,000 --> 00:26:31,000 seven and six, and the average is six. 577 00:26:31,000 --> 00:26:32,000 It kind of looks good, right? It 578 00:26:32,000 --> 00:26:36,000 makes sense to me that the five and seven cancelled each other out. 579 00:26:36,000 --> 00:26:39,000 But if I do a little bit more deep testing where I put in numbers like five, 580 00:26:39,000 --> 00:26:42,000 six, five and six, my 581 00:26:42,000 --> 00:26:43,000 average is five. In 582 00:26:43,000 --> 00:26:44,000 this 583 00:26:44,000 --> 00:26:46,000 case, I would think that I would be 584 00:26:46,000 --> 00:26:49,000 getting something like five and a half. I go back 585 00:26:49,000 --> 00:26:52,000 here and take a look at what I did. I took sum and divided it by NumScores. 586 00:26:52,000 --> 00:26:56,000 What have I failed to do? 587 00:26:56,000 --> 00:27:00,000 Cast that thing to get it out of there. The default ? this is the same in Java as it is 588 00:27:00,000 --> 00:27:01,000 in C++ 589 00:27:01,000 --> 00:27:06,000 is that if you are combining two operands in an arithmetic expression, 590 00:27:06,000 --> 00:27:10,000 if they are of mixed type ? one is double and one is int ? it promotes to the 591 00:27:10,000 --> 00:27:12,000 richer type. If one of those was a double, 592 00:27:12,000 --> 00:27:16,000 it would convert the other one to a double and do the calculation in double 593 00:27:16,000 --> 00:27:20,000 space. As it is, sum is an integer and NumScores is an integer. It's doing integer division. 594 00:27:20,000 --> 00:27:24,000 Integer division has [inaudible] built into it, so if I divide ten by three, I get three 595 00:27:24,000 --> 00:27:26,000 and that remaining one third is just thrown away. 596 00:27:26,000 --> 00:27:29,000 Even though the result is double, 597 00:27:29,000 --> 00:27:33,000 that happens after the fact. It does the calculation 598 00:27:33,000 --> 00:27:34,000 integer space ? the 599 00:27:34,000 --> 00:27:36,000 ten divided by 600 00:27:36,000 --> 00:27:36,000 three. 601 00:27:36,000 --> 00:27:40,000 It gets the three-integer result and then it says oh, I have an integer here. I need 602 00:27:40,000 --> 00:27:43,000 to convert it to a double on my way out. It takes that and turns it into 603 00:27:43,000 --> 00:27:44,000 604 00:27:44,000 --> 00:27:48,000 3.0. It doesn't recover that truncated part that got thrown away. If I really want to get 605 00:27:48,000 --> 00:27:50,000 a real double result, I've got to make one of those a double 606 00:27:50,000 --> 00:27:54,000 to force the calculation into the double space. 607 00:27:54,000 --> 00:27:58,000 The mechanism of C++ is a little bit different than it is in Java. In fact, 608 00:27:58,000 --> 00:27:59,000 the Java mechanism 609 00:27:59,000 --> 00:28:03,000 does work, but the preferred form in C++ looks a little bit like this where you take the 610 00:28:03,000 --> 00:28:06,000 name of the type you're trying to convert it to. It looks a little bit like you're making a function 611 00:28:06,000 --> 00:28:07,000 call-passing sum 612 00:28:07,000 --> 00:28:10,000 to a function that will change it into a double. 613 00:28:10,000 --> 00:28:12,000 By doing this, 614 00:28:12,000 --> 00:28:15,000 I now have one of them being double. Int on the other side gets promoted. 615 00:28:15,000 --> 00:28:17,000 If I do my five, six, five, six, 616 00:28:17,000 --> 00:28:19,000 that truncated part is now 617 00:28:19,000 --> 00:28:27,000 part of that calculation and preserved and pulled out and printed. 618 00:28:27,000 --> 00:28:30,000 Let 619 00:28:30,000 --> 00:28:32,000 me change this a little bit 620 00:28:32,000 --> 00:28:33,000 to something else. 621 00:28:33,000 --> 00:28:37,000 Right now, it assumes that there's a fixed number of scores that you want to do. 622 00:28:37,000 --> 00:28:40,000 Maybe what I have is a big stack of exam papers, and I 623 00:28:40,000 --> 00:28:42,000 don't actually know how many are in the pile, and I don't actually want to count them ahead 624 00:28:42,000 --> 00:28:43,000 of time. 625 00:28:43,000 --> 00:28:46,000 I just want to keep entering scores until I get to the bottom of the pile. I'm 626 00:28:46,000 --> 00:28:48,000 going to change this loop 627 00:28:48,000 --> 00:28:48,000 to 628 00:28:48,000 --> 00:28:51,000 allow me to enter numbers until I say I'm done. 629 00:28:51,000 --> 00:28:53,000 630 00:28:53,000 --> 00:28:55,000 Let's take a look at what that's gonna look like. 631 00:28:55,000 --> 00:28:57,000 I'm going 632 00:28:57,000 --> 00:28:59,000 to get the value 633 00:28:59,000 --> 00:29:00,000 here. I'm going to change my code a little bit. 634 00:29:00,000 --> 00:29:03,000 Then if the value is the sentinel, 635 00:29:03,000 --> 00:29:05,000 636 00:29:05,000 --> 00:29:09,000 then I don't want to include it in the sum. Otherwise, I 637 00:29:09,000 --> 00:29:14,000 do. Maybe we'll do this. We'll say if the value doesn't equal the sentinel, 638 00:29:14,000 --> 00:29:15,000 then sum 639 00:29:15,000 --> 00:29:16,000 adds into there. 640 00:29:16,000 --> 00:29:20,000 I also need to make this thing stop, and so I'm going to have to rearrange 641 00:29:20,000 --> 00:29:23,000 my loop a little bit here, because if you 642 00:29:23,000 --> 00:29:26,000 think about what I'm going to try to do, I'm going to prompt and get a value and then 643 00:29:26,000 --> 00:29:27,000 either add it in the sum or quit. 644 00:29:27,000 --> 00:29:30,000 Then I'm going to prompt and get a value and add it in the sum or quit. I have a loop where 645 00:29:30,000 --> 00:29:33,000 I need to rearrange my 646 00:29:33,000 --> 00:29:35,000 notion a little bit here. 647 00:29:35,000 --> 00:29:38,000 One way I could do this is something like this. I could say this. While 648 00:29:38,000 --> 00:29:42,000 value does not equal sentinel, 649 00:29:42,000 --> 00:29:43,000 then 650 00:29:43,000 --> 00:29:46,000 add it into the sum. I'm going to kind of change my code around. Watch 651 00:29:46,000 --> 00:29:48,000 carefully as I 652 00:29:48,000 --> 00:29:52,000 reorganize it. If the value is not equal to the sentinel, then I'm going to 653 00:29:52,000 --> 00:29:54,000 add it into the sum 654 00:29:54,000 --> 00:29:56,000 and then I'm going to get the next 655 00:29:56,000 --> 00:29:58,000 656 00:29:58,000 --> 00:30:02,000 and overwrite the previous 657 00:30:02,000 --> 00:30:07,000 value. I inverted that loop that was there a little bit. 658 00:30:07,000 --> 00:30:10,000 It says while the value is not the sentinel, 659 00:30:10,000 --> 00:30:13,000 add it into the sum and then get the next value and come back around. 660 00:30:13,000 --> 00:30:17,000 The problem with this code as it stands is there's something a little bit wrong about value. It's not 661 00:30:17,000 --> 00:30:20,000 initialized. I need to initialize it to something. 662 00:30:20,000 --> 00:30:23,000 What 663 00:30:23,000 --> 00:30:25,000 do I need to initialize it to? What the 664 00:30:25,000 --> 00:30:28,000 user wants. This little piece of code down here 665 00:30:28,000 --> 00:30:31,000 needs to come up 666 00:30:31,000 --> 00:30:33,000 and be repeated up here. 667 00:30:33,000 --> 00:30:38,000 I need to get a value from them and then go into if it wasn't a sentinel add it 668 00:30:38,000 --> 00:30:40,000 and get the next one. 669 00:30:40,000 --> 00:30:42,000 This is what's called a classic loop and a 670 00:30:42,000 --> 00:30:45,000 half construction where there's a few things I need to do and then I need to do 671 00:30:45,000 --> 00:30:49,000 some tests and decide whether to go on with that iteration. As it is, 672 00:30:49,000 --> 00:30:52,000 there's half of the loop that starts up. They call it 673 00:30:52,000 --> 00:30:55,000 priming the loop. 674 00:30:55,000 --> 00:30:57,000 That's a little bit 675 00:30:57,000 --> 00:30:59,000 unseemly. There's something about that that's a little bothersome. Even though it's a very 676 00:30:59,000 --> 00:31:03,000 small piece of code ? two lines of code is not going to rock the world. I do 677 00:31:03,000 --> 00:31:07,000 want to try to get us into the habit of thinking if we can combine that code and 678 00:31:07,000 --> 00:31:08,000 unify it, that's better. 679 00:31:08,000 --> 00:31:11,000 It's better than repeating it. Repeating it means that if I ever change something about 680 00:31:11,000 --> 00:31:13,000 it, I have to change it in two places. 681 00:31:13,000 --> 00:31:15,000 There's an opportunity for error to creep in 682 00:31:15,000 --> 00:31:18,000 that I can avoid if I can get it to where there's really just one and only one version of 683 00:31:18,000 --> 00:31:19,000 the truth. I'm 684 00:31:19,000 --> 00:31:21,000 going to change this. 685 00:31:21,000 --> 00:31:24,000 It's going to use the break statement, 686 00:31:24,000 --> 00:31:26,000 which you may have had a little experience with. I'm going to run a 687 00:31:26,000 --> 00:31:30,000 while true loop, which looks like it's going to run forever. 688 00:31:30,000 --> 00:31:32,000 I'm going to prompt to get the value 689 00:31:32,000 --> 00:31:37,000 and if the value equals the sentinel, 690 00:31:37,000 --> 00:31:38,000 then I'm going to use break 691 00:31:38,000 --> 00:31:41,000 and immediately exit the loop here 692 00:31:41,000 --> 00:31:44,000 without completing the bottom of this loop iteration. It causes it to move on 693 00:31:44,000 --> 00:31:46,000 to the statements after the loop. 694 00:31:46,000 --> 00:31:48,000 By doing it this way, I now have 695 00:31:48,000 --> 00:31:49,000 one prompt, 696 00:31:49,000 --> 00:31:50,000 then a check, 697 00:31:50,000 --> 00:31:53,000 and based on that check, I either finish this iteration and keep coming 698 00:31:53,000 --> 00:31:54,000 around 699 00:31:54,000 --> 00:31:57,000 or I immediately break out saying that was 700 00:31:57,000 --> 00:32:01,000 the clue that we're done with this. 701 00:32:01,000 --> 00:32:02,000 702 00:32:02,000 --> 00:32:03,000 I would 703 00:32:03,000 --> 00:32:08,000 say that it's a little bit of an advanced question, but it turns out that the do while loop is 704 00:32:08,000 --> 00:32:10,000 not really that much cleaner in the end 705 00:32:10,000 --> 00:32:12,000 706 00:32:12,000 --> 00:32:16,000 because you still have to do the prompt and test and decide when to add it in. 707 00:32:16,000 --> 00:32:19,000 Do well loops are just so unusual that they cause everybody to slow down a little 708 00:32:19,000 --> 00:32:22,000 bit when you read them. If you can write it using a more [inaudible] construct, 709 00:32:22,000 --> 00:32:24,000 there is some advantage to that. 710 00:32:24,000 --> 00:32:27,000 I got this guy together here, 711 00:32:27,000 --> 00:32:27,000 712 00:32:27,000 --> 00:32:31,000 and then maybe I should actually tell the user ? this might be a good thing to say. 713 00:32:31,000 --> 00:32:33,000 714 00:32:33,000 --> 00:32:37,000 Tell them what the sentinel is. 715 00:32:37,000 --> 00:32:41,000 I have to type better than I do. 716 00:32:41,000 --> 00:32:47,000 717 00:32:47,000 --> 00:32:51,000 NumScores 718 00:32:51,000 --> 00:32:54,000 equals zero, 719 00:32:54,000 --> 00:32:58,000 and then each time we 720 00:32:58,000 --> 00:33:01,000 721 00:33:01,000 --> 00:33:05,000 get one, we 722 00:33:05,000 --> 00:33:10,000 increment. 723 00:33:10,000 --> 00:33:13,000 Let's go back. We'll change our call to use a more ordinary value like -1. 724 00:33:13,000 --> 00:33:17,000 We'll type in 725 00:33:17,000 --> 00:33:21,000 five, six, seven, eight, nine and then -1, and the average of 726 00:33:21,000 --> 00:33:22,000 those is 727 00:33:22,000 --> 00:33:27,000 seven. 728 00:33:27,000 --> 00:33:30,000 It 729 00:33:30,000 --> 00:33:34,000 should seem very familiar in a lot of ways but there are a few details that 730 00:33:34,000 --> 00:33:35,000 need to be different. I'm going 731 00:33:35,000 --> 00:33:39,000 to show you one of the little C++isms while I'm here because it's something that comes up in the 732 00:33:39,000 --> 00:33:44,000 standard libraries and it's worth knowing. There are some minor conveniences in the way 733 00:33:44,000 --> 00:33:48,000 that C++ provides certain facilities that are not conceptually a 734 00:33:48,000 --> 00:33:51,000 big deal, but you do need to know about them because you're going to encounter them in various interfaces. 735 00:33:51,000 --> 00:33:55,000 One is the notion of a default argument. 736 00:33:55,000 --> 00:33:58,000 In the prototype for a function, 737 00:33:58,000 --> 00:34:01,000 738 00:34:01,000 --> 00:34:03,000 739 00:34:03,000 --> 00:34:05,000 740 00:34:05,000 --> 00:34:05,000 741 00:34:05,000 --> 00:34:08,000 there are arguments to a function where it's 742 00:34:08,000 --> 00:34:11,000 some very large percentage of the time always going to 743 00:34:11,000 --> 00:34:14,000 want to be a certain value, but you still want to leave it open for the user to specify 744 00:34:14,000 --> 00:34:18,000 a different value in the cases where they don't want to use that standard 745 00:34:18,000 --> 00:34:18,000 value. 746 00:34:18,000 --> 00:34:21,000 One way of doing that in C++ is to write your function using a 747 00:34:21,000 --> 00:34:22,000 default argument 748 00:34:22,000 --> 00:34:24,000 where I say int sentinel 749 00:34:24,000 --> 00:34:26,000 and I said equals 750 00:34:26,000 --> 00:34:29,000 -1. That is providing for if somebody makes a call to GetScoresAndAverage 751 00:34:29,000 --> 00:34:31,000 passing an argument, it's used instead. 752 00:34:31,000 --> 00:34:35,000 If they pass nothing, so they just have open paren close paren and they don't give a 753 00:34:35,000 --> 00:34:36,000 value for that argument, 754 00:34:36,000 --> 00:34:39,000 that means the assumption is to use that default. That means that most people 755 00:34:39,000 --> 00:34:43,000 who are making calls to GetScoresAndAverage can just drop this number 756 00:34:43,000 --> 00:34:46,000 and get the behavior of using -1 as the sentinel. 757 00:34:46,000 --> 00:34:49,000 In the case where for one reason or another -1 was one of the valid values that you 758 00:34:49,000 --> 00:34:53,000 could potentially enter, you could pick a different one. 759 00:34:53,000 --> 00:34:55,000 760 00:34:55,000 --> 00:34:57,000 761 00:34:57,000 --> 00:34:58,000 762 00:34:58,000 --> 00:35:01,000 We'll see that in the libraries. It's interesting. 763 00:35:01,000 --> 00:35:02,000 The 764 00:35:02,000 --> 00:35:06,000 mechanism of it is really quite simple. It's just a convenience to 765 00:35:06,000 --> 00:35:07,000 provide for. 766 00:35:07,000 --> 00:35:10,000 You can actually have more than one default argument. 767 00:35:10,000 --> 00:35:11,000 The 768 00:35:11,000 --> 00:35:15,000 idea is that you can only leave off when you're making the call the last most 769 00:35:15,000 --> 00:35:18,000 argument, and then from there, other ones, because it has to figure out how to match them 770 00:35:18,000 --> 00:35:20,000 up. It matches the arguments left and right, 771 00:35:20,000 --> 00:35:24,000 and as soon as it runs out of arguments, it assumes everything from there has to be 772 00:35:24,000 --> 00:35:27,000 used its default argument for it to match that call. It allows 773 00:35:27,000 --> 00:35:28,000 for ? 774 00:35:28,000 --> 00:35:31,000 you could have three arguments of which you could specify two or one or three if they all 775 00:35:31,000 --> 00:35:33,000 had defaults 776 00:35:33,000 --> 00:35:36,000 if you needed that. It's not that common. Let 777 00:35:36,000 --> 00:35:39,000 me go 778 00:35:39,000 --> 00:35:41,000 back 779 00:35:41,000 --> 00:35:44,000 to my slides and tell you about a couple other 780 00:35:44,000 --> 00:35:46,000 781 00:35:46,000 --> 00:35:53,000 mechanisms of 782 00:35:53,000 --> 00:35:55,000 some C++ features 783 00:35:55,000 --> 00:35:58,000 that are new to us that we want to know a little bit about. 784 00:35:58,000 --> 00:36:01,000 There are two types of user-defined types that are very simple to get your head 785 00:36:01,000 --> 00:36:06,000 around. You want to know what the syntax for them is and what they do. It's the 786 00:36:06,000 --> 00:36:08,000 enumerated type or the enumeration 787 00:36:08,000 --> 00:36:12,000 where at the top of your program ? up there where I was defining constants and doing 788 00:36:12,000 --> 00:36:14,000 #includes, this is where we put 789 00:36:14,000 --> 00:36:17,000 information for the compiler that is of use across the entire program. This was 790 00:36:17,000 --> 00:36:19,000 also where we would put new user defined types. 791 00:36:19,000 --> 00:36:22,000 In this case, I want to define a type direction T 792 00:36:22,000 --> 00:36:25,000 which can take on one of the four values, north, south, east or west. 793 00:36:25,000 --> 00:36:28,000 That whole package up there ? that enum direction T north south east 794 00:36:28,000 --> 00:36:31,000 west is the way of defining the new type. You're saying direction T now 795 00:36:31,000 --> 00:36:32,000 comes into the 796 00:36:32,000 --> 00:36:36,000 space as a name that can be used for variables, parameters, return 797 00:36:36,000 --> 00:36:40,000 types ? any place you could have used int you can start using direction T. 798 00:36:40,000 --> 00:36:42,000 It has 799 00:36:42,000 --> 00:36:43,000 the expectation that variables 800 00:36:43,000 --> 00:36:46,000 that are declared in direction T will be holding one of those four values. 801 00:36:46,000 --> 00:36:50,000 It's like north, south, east and west got defined as constants, 802 00:36:50,000 --> 00:36:53,000 and they are, by default, assigned the values zero, one, two and three 803 00:36:53,000 --> 00:36:56,000 in order unless you do anything special. 804 00:36:56,000 --> 00:36:57,000 You can use them ? 805 00:36:57,000 --> 00:37:00,000 you can do things on enums that are very integer like. 806 00:37:00,000 --> 00:37:04,000 You can assign them. You can compare them. You could use less than and greater than. 807 00:37:04,000 --> 00:37:06,000 You can 808 00:37:06,000 --> 00:37:11,000 add them. There are things ? they are largely implemented underneath the scenes as numeric 809 00:37:11,000 --> 00:37:13,000 types, but they're a nice convenience for making it clear that 810 00:37:13,000 --> 00:37:16,000 this thing isn't just any old ordinary integer. 811 00:37:16,000 --> 00:37:20,000 It's specifically ? it should be kept into this constrained range. It means 812 00:37:20,000 --> 00:37:23,000 something a little different. It's just a nicety. It does not 813 00:37:23,000 --> 00:37:25,000 have a lot of heavy weight 814 00:37:25,000 --> 00:37:29,000 feature associated with it, but it's just a nice way of documenting that something is 815 00:37:29,000 --> 00:37:32,000 this kind of set up. 816 00:37:32,000 --> 00:37:34,000 This one ? 817 00:37:34,000 --> 00:37:35,000 the record of the [inaudible] type ? 818 00:37:35,000 --> 00:37:39,000 much more broadly useful. It's just an aggregate where you can take a 819 00:37:39,000 --> 00:37:41,000 set of fields 820 00:37:41,000 --> 00:37:43,000 821 00:37:43,000 --> 00:37:47,000 of same or different type, give them names and aggregate them together. You 822 00:37:47,000 --> 00:37:50,000 say here is student record, and the student has a name, dorm room, 823 00:37:50,000 --> 00:37:52,000 phone number and transcript. 824 00:37:52,000 --> 00:37:55,000 Aggregate it together into a new structure type. In this 825 00:37:55,000 --> 00:37:57,000 case, the point T [inaudible], 826 00:37:57,000 --> 00:38:00,000 and so like this ? this is the kind of thing you put up at the top of your 827 00:38:00,000 --> 00:38:02,000 code that says here's what's in 828 00:38:02,000 --> 00:38:05,000 a point T, the field names and their types, 829 00:38:05,000 --> 00:38:07,000 and a 830 00:38:07,000 --> 00:38:11,000 little point of noteworthy 831 00:38:11,000 --> 00:38:12,000 error 832 00:38:12,000 --> 00:38:15,000 producer is that there really does have to be a semicolon at the end of this. 833 00:38:15,000 --> 00:38:19,000 Similarly, there has to be a semicolon at the end of the enum for direction 834 00:38:19,000 --> 00:38:20,000 T. 835 00:38:20,000 --> 00:38:21,000 If you forget to do that, 836 00:38:21,000 --> 00:38:25,000 there's a cascade of errors out of that that are really a little bit 837 00:38:25,000 --> 00:38:29,000 mystical the first time you see them. You learn to identify this 838 00:38:29,000 --> 00:38:30,000 quickly later on. 839 00:38:30,000 --> 00:38:33,000 It ends that declaration and then allows the composite to move on to the next 840 00:38:33,000 --> 00:38:36,000 thing, not assuming you're still doing more work with this. 841 00:38:36,000 --> 00:38:38,000 Once you have this type declared, 842 00:38:38,000 --> 00:38:40,000 you can make variables, return types, parameters, 843 00:38:40,000 --> 00:38:45,000 all that stuff, and then the access to the members within the fields within 844 00:38:45,000 --> 00:38:46,000 a 845 00:38:46,000 --> 00:38:49,000 [inaudible] looks like access to an object did in Java where you have the variable 846 00:38:49,000 --> 00:38:51,000 name on the left 847 00:38:51,000 --> 00:38:54,000 and then a dot and then on the right, the name of the field you're accessing, 848 00:38:54,000 --> 00:38:55,000 setting, and reading. 849 00:38:55,000 --> 00:38:58,000 You can do things like P = Q which does a full assignment of one 850 00:38:58,000 --> 00:39:02,000 [inaudible] onto another, so it copies all the fields over from one 851 00:39:02,000 --> 00:39:03,000 to the other. It's 852 00:39:03,000 --> 00:39:05,000 something simple that does 853 00:39:05,000 --> 00:39:08,000 have a lot of usefulness. It makes a lot of sense when you have a 854 00:39:08,000 --> 00:39:11,000 program and you do group things together. Here's all the information about 855 00:39:11,000 --> 00:39:17,000 this part of my data structure ? a student, a class, a 856 00:39:17,000 --> 00:39:17,000 dorm 857 00:39:17,000 --> 00:39:20,000 ? all the information being aggregated together into one unit is 858 00:39:20,000 --> 00:39:23,000 actually a nice way to keep your 859 00:39:23,000 --> 00:39:30,000 data organized. 860 00:39:30,000 --> 00:39:35,000 861 00:39:35,000 --> 00:39:39,000 There 862 00:39:39,000 --> 00:39:42,000 are two point T variables. 863 00:39:42,000 --> 00:39:47,000 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. 864 00:39:47,000 --> 00:39:49,000 At this point, the P.Y's field is nonsense. It's 865 00:39:49,000 --> 00:39:52,000 just garbage. I said P = Q, which basically says take the nonsense 866 00:39:52,000 --> 00:39:56,000 in Q and override it onto P and make P be as nonsensical as Q is in terms of 867 00:39:56,000 --> 00:39:59,000 its contents. 868 00:39:59,000 --> 00:40:02,000 Not very useful code. 869 00:40:02,000 --> 00:40:05,000 The last thing I'm going to show you today 870 00:40:05,000 --> 00:40:09,000 is to talk a little bit about parameter passing. This is going to come up 871 00:40:09,000 --> 00:40:12,000 again and again, but this is just kind of a quick 872 00:40:12,000 --> 00:40:15,000 first mention of these things. Someone had asked earlier what is the 873 00:40:15,000 --> 00:40:19,000 parameter passing mechanism that's in play? The 874 00:40:19,000 --> 00:40:22,000 default parameter passing mechanism is what's called pass by value. It 875 00:40:22,000 --> 00:40:24,000 copies. 876 00:40:24,000 --> 00:40:27,000 If I have a function here binkie int X and 877 00:40:27,000 --> 00:40:30,000 Y, in the body of it, it tries to do something like double the value of binkie 878 00:40:30,000 --> 00:40:33,000 and reset Y to zero. 879 00:40:33,000 --> 00:40:36,000 When I make a call to binkie and I had A set to four and B to 20, when I made 880 00:40:36,000 --> 00:40:37,000 the call to binkie, 881 00:40:37,000 --> 00:40:42,000 the pass by value really means that the X and Y parameters of binkie are copies of A 882 00:40:42,000 --> 00:40:43,000 and B. 883 00:40:43,000 --> 00:40:46,000 They are distinct. They are new integer variables, new space, new 884 00:40:46,000 --> 00:40:47,000 storage, 885 00:40:47,000 --> 00:40:50,000 and they got their initial values by taking the current values of A and B and copying 886 00:40:50,000 --> 00:40:51,000 them. 887 00:40:51,000 --> 00:40:56,000 Changes to X and Y affect binkie's context only. 888 00:40:56,000 --> 00:41:00,000 That four that came in here and got doubled to eight, that Y that got set to zero 889 00:41:00,000 --> 00:41:04,000 are live here, but when binkie exits and we get back to main, A 890 00:41:04,000 --> 00:41:06,000 and B still are 891 00:41:06,000 --> 00:41:10,000 four and 20. It just did full copies of all the variables, and this is true for 892 00:41:10,000 --> 00:41:13,000 all types of [inaudible] and enums and ints and 893 00:41:13,000 --> 00:41:17,000 chars and all that. It's copying the data and operating on a copy. 894 00:41:17,000 --> 00:41:21,000 In most situations, that's actually fairly appropriate. You tend to be 895 00:41:21,000 --> 00:41:23,000 passing information in so it can do some manipulations. 896 00:41:23,000 --> 00:41:27,000 In the situation where you really want to be passing in and having changes be permanent 897 00:41:27,000 --> 00:41:29,000 to it, 898 00:41:29,000 --> 00:41:31,000 there is an alternate 899 00:41:31,000 --> 00:41:34,000 declaration where you and an & 900 00:41:34,000 --> 00:41:37,000 to the type. Instead of being an int, it's an 901 00:41:37,000 --> 00:41:42,000 int&, and that changes this parameter from being a pass by value to a pass by 902 00:41:42,000 --> 00:41:44,000 reference or a reference parameter. 903 00:41:44,000 --> 00:41:46,000 In such a case, 904 00:41:46,000 --> 00:41:50,000 when I make a call to binkie, this first argument will not be a copy. The second 905 00:41:50,000 --> 00:41:54,000 argument's still a copy because I haven't changed anything about it, but when I say binkie of A and B, 906 00:41:54,000 --> 00:41:58,000 what the binkie function is actually getting in that first argument is not a 907 00:41:58,000 --> 00:42:02,000 copy of the value four. It's getting a reference back to the original 908 00:42:02,000 --> 00:42:06,000 A. For the Y parameter, it's getting a copy of 20. When 909 00:42:06,000 --> 00:42:10,000 it makes this change over here of trying to take X and double its value, 910 00:42:10,000 --> 00:42:12,000 it really did reach back out and change 911 00:42:12,000 --> 00:42:14,000 A. After this, 912 00:42:14,000 --> 00:42:17,000 A would be eight. B would still be 913 00:42:17,000 --> 00:42:21,000 20. It allows for you to pass some data in and have it be manipulated and changed, so 914 00:42:21,000 --> 00:42:23,000 updated, adjusted and whatnot 915 00:42:23,000 --> 00:42:27,000 and then come back out and see those changes 916 00:42:27,000 --> 00:42:30,000 that is for certain situations very useful. 917 00:42:30,000 --> 00:42:34,000 This mechanism doesn't exist in Java. There's not a pass by reference 918 00:42:34,000 --> 00:42:38,000 mechanism, so this is likely to seem a little bit bizarre at first. 919 00:42:38,000 --> 00:42:40,000 We will see this a lot, so this is maybe just our first introduction to this. 920 00:42:40,000 --> 00:42:43,000 We're going to come back to this more and more. One 921 00:42:43,000 --> 00:42:45,000 thing I will note is that the 922 00:42:45,000 --> 00:42:49,000 primary purpose of this is to allow you to kind of change some data. 923 00:42:49,000 --> 00:42:52,000 It also gets used in a lot of situations just for efficiency reasons where if you have 924 00:42:52,000 --> 00:42:57,000 a large piece of data you're passing in a big database, 925 00:42:57,000 --> 00:42:59,000 to copy it could be expensive. 926 00:42:59,000 --> 00:43:03,000 Copying just so you could 927 00:43:03,000 --> 00:43:05,000 hand it off to a function to print 928 00:43:05,000 --> 00:43:09,000 it would be unnecessary, and so by using the reference parameter in those situations, you're 929 00:43:09,000 --> 00:43:12,000 actually just allowing it to avoid that copy overhead 930 00:43:12,000 --> 00:43:16,000 and still get access to the data. Sometimes, you'll see it used both for changing as well as 931 00:43:16,000 --> 00:43:18,000 efficiency. I'm 932 00:43:18,000 --> 00:43:21,000 not going to show my last slide, but that's what we're going to be talking about on Monday. We'll talk about 933 00:43:21,000 --> 00:43:23,000 libraries. Looking at chapter three is the place to go in the reader. I'm going 934 00:43:23,000 --> 00:43:26,000 to be walking over to Terman 935 00:43:26,000 --> 00:43:28,000 momentarily, and I would love to have somebody come and hang out with me. Otherwise, 936 00:43:28,000 --> 00:43:32,000 have a good weekend. You have nothing to do for me. Enjoy it. It's the last weekend in 937 00:43:32,000 --> 00:43:39,000 which you will not be