Welcome. A couple things that I wanted to go through administratively to get us all up to speed -- if you did miss Wednesdayfs class, the handouts are available on the web and the online video also, so you can take a look at what we did. We just did a course overview and a little marketing and just gave you some ideas of what wefre planning to do together this quarter to make sure youfre in the right place. That might be a good thing to review if you did happen to miss is. The section times are now available on the web. They went up last night. Theyfll be up for the next couple of days. You go in and list the preferences of times that fit into your schedule, and then we do this big matching to get everybody into a section that works. Therefs no reason to do it early versus later. We do all the matching once we have everybodyfs preferences in. You do want to do your preferences at some point before it closes so we can make sure we have your information in the mix. If you forget to do this, youfll be at the mercy of where we can fit you, and that might be much less convenient for you or, in fact, even impossible. Be sure to get your preferences in for us to do the match with. One thing I should have spent a little bit of time on on Wednesday -- how the honor code applies in computer science and how to make sure that all of us are doing our part to uphold the honorable community that we have pledged to be a part of. Itfs a little bit unfortunate and a little bit of an embarrassment to computer scientists that we actually account for a disproportionately large number of cases that are brought before the judicial board at Stanford. My personal thought about that is not that we somehow attract the dishonest student, but I think there are certain things that combine to make some of the pressures of CS a little bit outstanding relative to some of the other things. If youfre up working late and youfre trying to write a paper and itfs not really coming together, at some point, you can say itfs good enough and you can send it off, even if it is not your best work. Itfs a lot harder to do that when you have a program that doesnft do what itfs supposed to do. It may crash right away or give the blue screen of death, and that feeling of failure or lack of accomplishment is one thatfs hard to stomach as the talented people you are. Sometimes, that leads you into temptation about how you might be able to get the program working through means that arenft really your own work. Let me be clear about what our rules are. I put out a big handout last time. Therefs also a big follow-up thing on the web that I think is worth reading, because I think everybody needs to know where the boundaries are. In general, you need to be doing your own work. Your own work means youfre sitting down and coding and thinking and designing and debugging independently. That said, we have a lot of staff hours that can help you when you get into trouble, and asking questions about any general features of the language or the library or how this works or strategies is totally fine. You should definitely rely on each other to get through those rough spots. When it gets to the point where youfre looking at somebody elsefs code or looking at code together or looking at someonefs code from a passporter or giving your code to someone else, youfre crossing that boundary we donft want you to cross. When that happens, it often leads to the code coming out much more similar than you might imagine. In fact, we use automatic tools to help us identify situations where that might come up. We do move forward with bringing those cases to judicial. Itfs personally a very sad thing for me. I want to get this out now. The point is wefre here to help you, and we want you to succeed. If youfre getting stuck and youfre having trouble, the right thing to do is to ask us for help. We can often get you past that sticking point and get you onto doing the right work for yourself, but using someone elsefs code -- people do make big efforts to try to conceal what theyfve done, and it doesnft work is the truth. We do catch it. The result is not a pretty outcome for anyone. That said, you do not need to worry about us mistakenly identifying your code as looking like someone elsefs because wefre asking you to solve the same problems. It turns out that code is as individual as a fingerprint. The way two people are writing an essay about the treatment of class in Dickensf gA Christmas Carolh donft look the same. It doesnft matter that we ask you to do the same thing. They really do look different. You have nothing to worry about as long as you are doing your own work. Please do read those things, the handout and the web stuff just to go through all the minutia of how to know when youfre getting into those gray areas, and certainly ask if you have any questions. About the alternate final exam -- I donft want to get into the slippery slope of having much alternate exams. That actually creates issues with security and fairness that Ifm not prepared to figure out how to navigate, but I did get a certain number of requests, and Ifve decided to be a little bit more lenient than I usually am. Ifm going to offer exactly one alternate final time, which is 24 hours earlier than our regular exam. Wefll have more information about that as we get close to that, but for people who have an exact conflict or need to leave a day early, that will accommodate you. I will make no other accommodations. If this means you cannot take this class, then donft take this class. I am not capable of managing an excessive number of people who all want to take it at a time that works best for them. The universityfs scheduled slot is Friday, 12:15 to 3:15. Everyone really should have that time available, but if that for some reason is completely unavoidable for you, then Thursday, 12:15 is it. One thing Ifm going to try to do on Fridays is take a little time to sit and talk about stuff and having something less formal than office hours. Itfs not really about having CS questions answered. Itfs just getting to know each other and having a chance to talk. I think most students really wish they had spent more time with their professors, and I think Ifm a fascinating and interesting person. Thatfs something that comes up again and again is the feeling that, especially in a large class like this, the feeling of being anonymous and not being able to make that connection. Itfs something that Ifd like to try to fix, and I want you to try to fix it with me. Fridays, therefs a school of engineering undergraduate council meeting that will meet every third Friday, but on the other two, my plan is to walk out of here and go over to that cafe' thatfs down in the basement of Terman and get a nice cup of coffee and a cookie and sit down and talk. Youfre welcome to come and talk about anything and everything thatfs on your mind and just get to know me. If not this week, then maybe some other Friday will work for you. I will be happy to make your acquaintance. What Ifm going to talk about today -- Ifm going to do some C++. Ifm going to talk about how C++ relates to Java, show you some sample programs, do a little coding with you, see if you can kind of get the feel for how things are going to work, try to talk about some of the specific mechanisms that are different in C++ that we need to get familiar with. The reading that goes along with this is the handout four, which tries to go through the high-level overview of the Java/C++ differences as well as the reader chapters, which fill out more of the details about the language. What wefre moving onto next week is the C++ library, so wefre talking about string and stream and the CS106 libraries that go along with it that form the foundation of things we have to build on. Any questions administratively? Anyone read the handout two and had some questions about policies, procedures, or stuff I talked about Wednesday? I also brought something. A former student of mine brought me her homemade caramels, and if they sit in my office, Ifm going to eat them all, and I donft want to do that, even though they are extremely delicious, and I thought I would bring them as a bribery offering where itfs hard to talk in a class thatfs this big, and you feel like everybodyfs staring at you, but I figure Ifm going to offer up sugar as reward for courage, and so Ifll have this little bag of stuff here, and if you have a question, just figure not only will you get your question answered magnificently, there will be a bonus butter/sugar bolus that goes with it. This is Java. Itfs like a death match. Whatfs the same? The first thing I want to talk about is that there are a lot of things that are the same, and so you are building on a background that is going to serve you well without having to make a lot of adjustments. Theyfre almost so similar that at times, it can be a little bit unhelpful in the sense that itfs easy to get tripped up because they almost but not quite are exactly the same. There was one unfortunate quarter where I was teaching two classes -- one that was being taught in C++ and one that was in Java, and I was a mess the whole quarter. Luckily, Ifm not doing that now, so hopefully, Ifll have my C++ hat on straight the whole quarter. The general syntax -- Ifm going to mention these things, and then Ifm going to show you a sample program. Things like the comment sequence, the slash star or the slash slash -- the use of almost all the punctuation characters is exactly the same. The statement terminator that the semicolon is used for, the commas to separate arguments going into a function call, the way you make variable and parameter declarations -- exactly the same. All of the primitive variable types -- char, int, double, plus the kind of more esoteric log and float and short and things like that that are present in Java are present in C++ and have basically the same semantics. There is one minor adjustment there, which is Javafs Boolean type, the true/false variable type is actually called Bool in C++, so you have to type a few less characters on that end. All of the operators that manipulate the basic types, the assignment operator [inaudible] the equal sign, the equals equals to compare, not equals to see if theyfre not equal, the arithmetic plus, the plus equal, multiply equal shorthands, the plus plus that comes in both pre and post increment, the relational less than, less than or equal to, the logical operators and/or have the same behaviors. They have the same short circuiting, the same precedence table, so all of that is without change. The control structure -- so for redirecting control flow and making choices about what code to execute when, the floor and [inaudible] mechanisms, the if with the optional else, the switch statement, which you may or may not have seen, but you can definitely look up in the text if you havenft -- that kind of has a variant of the if else, so it lets you decide based on the value of something which case to move into, the return thatfs used for the function -- also things like the break and continue, a little less known keywords there or the do while -- they work the same way in Java and C++. Letfs look at a complete C++ program. This one is printed in handout four, if you want to follow along there and make notes on it as we talk you through. What Ifm going to do here is Ifm actually planning on just walking through this line by line to kind of point out some of the things that are different. Then Ifm going to move to the compiler, and wefll do a little bit of experimentation with developing and writing code and manipulating something that looks like this to get some of our bearings about what things are going on. First off, just some general structure. A C++ program is traditionally written in a text file with a suffix some name.CPP. Therefs actually some other names that get used like .CC or .C that are also accepted by a lot of compilers. Some text extension that identifies that this file has C++ code -- wefll use CPP. The program is traditionally in a simple form just one file with all of the code thatfs necessary to be in there. Letfs take a look at it. This one starts with a comment -- always a good idea. This is average.CPP, and it adds scores and prints their average. Itfs just like all the good style habits that you have in Java. The next three lines are #include statements. #include is analogous to the Java import. The C++ compiler is pretty rigid about wanting to see things in the proper order. Before you start using some facility, it wants to know about it. Thatfs part of the safety of making sure youfre using it correctly. If you plan on drawing something in the screen and doing some graphics, it wants to be sure youfre making calls to the graphics function correctly with the right arguments, the right names, the right arrangements, and so in order to know that, it has to ahead of time have seen that bit of structure. The way this is done in C++ is through these interface or include files. The #include mechanism is fairly primitive. It basically says look for a file with the name genlib.h and take its contents and dump them right here. The contents of genlib.h and simpio.h are actually to give you a little insight. Wefre going to see them more later. It lists the operations and facilities that are available in this particular header that wefre trying to include. In this case, the genlib is a 106 specific header that all our programs will include. It gets us set up with some basic foundations. The simpio is our simplified IO header that adds some features for interacting with the user at the console. The next one down there is include . is a C++ standard header, and that helps to point out the difference between why in one place I was using double quotes to name the header file Ifm looking for and in one place Ifm using angle brackets. This is a way thatfs distinguished for what header files are, what are called system standard, and it looks for them in a particular place where the standard header files are. Those are always enclosed in the angle brackets. In double quotes are local headers that are specific to this project or this environment that are not C++ standard. Whenever you see these double quotes, youfll think itfs my own local headers or headers from the CS106 library. Anything in angle braces is a system standard header youfd find everywhere. It matches what you do in import. You say Ifm going to be using these features in this class. Let me import its features into here so that the compiler knows about them. The next line Ifve got here is a declaration of a constant. This is the const whose name is NumScores, and so it looks a little bit like a variable declaration. From this point over, you see I have int NumScores = 4;. It looks a lot like a variable operation. The const in the front of it is our way of telling the complier that this particular value once assigned will not change. It is equivalent to kind of the use of final in the Java language in many ways. This is being declared outside of context. One point to make here is that C++ drives on more than one way of expressing code and what we call its paradigm. Java is the completely object oriented language. Every line of code you wrote went inside a class. Itfs all about classes. You wrote this class that manipulated the thermometer. You wrote this class that drew with the turtle. There was no code that ever existed outside of a class. You started off with public class something and you started writing methods that operated on that class. Maybe they were static or maybe not. The way you started code was always to say start from this classf main to get work done. C++ is a hybrid language. It has object oriented features. It has the ability to find and use classes, and wefll see that quite extensively, but it also inherits the legacy of C, which is pre this whole object oriented development where it operates more procedurally, they call it, where there are functions that exist at the global level that donft have a context of a class that they operate in. Therefs not some [inaudible] thatfs being received. Theyfre just functions that exist outside. Itfs a little set of things to do that donft live inside a class. For much of the term, wefre going to be doing stuff in a very procedural way, at least from the code we write, and we will use a lot of objects. Wefll actually be mixing and matching a little bit of those paradigms together, so using the procedural paradigm to write a lot of code that happens to manipulate objects using an object oriented set of features. In this case, the program here -- this is actually up at the top level, and itfs saying this is a global constant thatfs available anywhere in the file. After this declaration, I can use the NumScores, which will refer back to this without any further adornment. The next thing underneath that is a prototype. This is actually a little bit of a novelty that in Java you are allowed to declare if you have three methods, A, B and C, and maybe A calls B and B calls C, you could declare A, B and C in any order thatfs convenient for you. You want to put them alphabetically? You want to put them in the order theyfre called? You want to put them in the order top to bottom down or bottom up? It doesnft matter. C++ tends to want to look at a file once top to bottom and not have any reason to go back and revisit things. It happens that in the main function, which is the one where code starts from, I want to make a call to this GetScoresAndAverage function. If I had left this prototype off and I had this code here, when it gets to this line where Ifm making that call, the compiler hasnft yet seen anything about GetScoresAndAverage. Itfs on the next page. It will complain. It will say I donft know what the function is. You havenft told me about it. It came out of nowhere. This prototype is our way of informing the compiler about something thatfs coming up later, and so the prototype tells about the return type, the name of the function, the type and names of the arguments and the order of them and then ends with a semicolon. It matches exactly the function header that wefll see on the next page, and it just tells the compiler in advance herefs something thatfs coming up later in the file. Itfs a function with this name and this signature. Itfs something you didnft have to do in Java but is a part of the C++ world. Therefs actually an alternate way I could have done this, which is when I show you the code for GetScoresAndAverage, if I actually defined them in the other order, I could avoid that separate prototype. Itfs a little bit of a matter of personal taste which way you feel comfortable doing it. Letfs look at this guy. Main is special. Lowercase m-a-i-n has no arguments and returns an integer type. This is the function in the C++ program where execution starts. There can be exactly one main function with this name and this signature, and when the program gets up and running, the first thing it does is start executing the lines of code that come in this function. No objects are created or any kind of fancy outer structure the way it might be in Java. It immediately goes here and starts doing what you told it to do. Main has a special role to play in any program. What this program does is first prints something. This is the C++ syntax for printing things out. Even if you havenft ever seen it, you probably can get a little bit of an idea of what itfs trying to do if you just step back from it and squint a little. It looks a little like system.out.print lin. This is whatfs called the consult out, or the standard out thatfs here on the left-hand side. The use of the double less than here is whatfs called the stream insertion operator. The standard output operation is saying insert into that stream and then in the double quotes is a string. This program averages. It says insert next to that the NumScores, which is this constant. It got the value from above. And then another string, and at the very end, therefs this little endl, which is the end line insertion of whatfs called a stream manipulator. It says at the end of this, put a new line and start any subsequent text on the next line. In Java, that would look like a system.out.print lin of this program averages plus the NumScores plus that using the string [inaudible] and conversion stuff in Java. It looks a little bit different in C++, but in the end result, itfs a matter of printing out a bunch of numbers and strings intermingled with new lines. Nothing too fancy there. The next line is making that call to that function that we earlier told it about but we havenft yet seen the code for that makes a call to get scores and averages. It passes the constant NumScores and says thatfs how many scores that parameter has used to decide how many iterations to run the loop requesting the values. Then it returns the average of the values entered and then we take that number. This just shows that in C++, you can declare variables anywhere, either at the start of a block, in the middle of a block or wherever they are at. We took that value, stored it in this variable and then used it in the next print statement. The last thing we have is a return zero. The signature for main in C++ always returns an integer. That integer is not all that useful in most situations. It tended to be a kind of return that tells the operating system who invoked the program whether everything completed successfully or not. By default, the value thatfs used to show successful completion is zero, so unless you have some other reason, it is return zero that youfll always see at the end there. In reality, if you were to return something else, you wonft see a lot of other differences. If you return -1 or 642, in most contexts, that numberfs almost ignored. It is there just to make tidy. Ifm about to flip to the next part, and this is the decomposed function that main made a call out to that does the averaging of the scores. Wefve got a comment on the top that tells a little bit about how it works. Itfs always a good idea to get some documentation in there thatfs helping the reader get oriented. That same matching of the prototype that was given earlier -- same name, same argument, same things, but instead of ending with that semicolon, it goes into the body. Sometimes wefll call these very similar terms -- this one we will call the definition or the implementation of a function, and that earlier just introduction of it is called its declaration or its prototype. They should match, and theyfre just there to give warning of something coming up later in the file. It initializes a sum to zero. It runs a standard four loop here. Like Java, we tend to count from zero as computer scientists for a long legacy reason. Instead of going from one to NumScores, we actually go from zero to NumScores minus one. Each time through the loop, we are prompting the user to give us a score. We are reading their score with a GetInteger call, and wefll see what GetInteger is. GetInteger is a CS106 function that retrieves a number that the user types in the console. We add that into the sum and keep going, and after wefve done the whole thing, at the very end, wefre doing a division of the sum by the number of scores to compute the average score that was [inaudible] and return that. [Student:]Did you say why we didnft have end line? In this case, I didnft have an end line just because I think it makes it nicer for the user. If you say next score and end line, then youfre typing on the line underneath it, and so if you donft put the end line there, itfs like you type right next to the question mark, and so it actually it an aesthetic thing. If I put it there, it would say next score and then I would be typing underneath it, and instead, Ifm typing right next to it. I hit the return and then the next one comes on the next line. So [inaudible] -- and I will talk about that a little bit later today, but it is -- thatfs a good question but a little bit advanced, so just take my answer and then wefll get to that in maybe 20 minutes. [Student:]So for every main function, you always have to end with return zero? Pretty much. As I said, you could put return anything, but as good form, return zero is a way of saying I successfully completed my job and Ifm done. [Student:]Do you use a void return type for the main? In C++, no. In C, itfs a little more lenient about that, and it will allow that. In C++, [inaudible]. I have to decide if I can get this to you without hurting you. Let me do a little coding with you guys just to play around with this and show you the complier. Ifd like to do a little bit of this. I think itfs really easy for me. I talk really fast. You might have noticed. When I get going on something, itfs easier for me to put up a lot of code on a slide and say here it is. Itfs all fully formed. The reality is when youfre running your programs yourself, itfs not going to come to you fully formed on a slide all ready to go. Youfre going to have to figure out how do you stack through making this stuff work? Ifm going to do a little bit of development of a program that looks very similar to the one I just wrote, but Ifm going to play around with it a little bit to get at some of the things that are different about C++ and how the tools work. This is basically what an empty project starts me off with. Ifve got the include for the genlib, but Ifll always have my int main and return zero and nothing else doing. Ifm going to sit down. Ifm going to start typing. Ifm going to say welcome. Ifm going to move to -- letfs do this. Wefll play with this for a second. There are some things that I can do. I can say welcome a lot of times. That wasnft really so hot there. You guys can help me when I fail to type correctly. I say welcome a lot of times. Letfs go see and make sure this works. Look at that. It doesnft like what I did. Letfs see what it has to say. It says cout was not [inaudible] the scope. Endl was not declared in the scope. This is an error message youfre going to see a lot of times in various other symbol lanes that will show up there, which is the C++ compilerfs way of saying to you I donft read your mind. You used cout. You used endl. These things come from somewhere, but theyfre not known to me until you make them known to me. The way I make those known is that those are part of the standard IO stream, which the input/output streams facility for the C++ language, and once I tell it about those things, itfs going to be much happier, and itfs going to say welcome to me a lot of times. In fact, I could say this -- 106B rocks. Ifll do this. Ifll say how awesome equals get integer and I will -- it may help if I move this up a little bit so we can see how much do you love 106B? Here, I wonft use my endl, so wefll see how the prompt comes out. Ifm going to get an integer from them, and Ifm going to use that to write how awesome that many times. What does it not like about that? Again, it says get integer not declared in the scope. This is going to be the bane of our existence today, which is remembering that there are headers out there that have features that wefre going to be using, and getting familiar with the ones that wefre gonna need -- there are probably about ten total that wefll use again and again, and we have to get our bearings about which ones are where. The documentation for all of our CS106 headers is available on the website in a nice HTML-ized version, and the ones from the standard libraries are detailed in the book in chapter three. How much do you love 106B? I could say well, I love it two times. I say no, I love it 1,900 and so. 10,000 times later, if youfre not convinced now, I donft know what will convince you. Wefre seeing some stuff. Letfs see if we can make that averaging thing work for us. Ifll go back to having it say welcome. Thatfs what the endl was doing for me there. The prompt is waiting at the end of that line. If I have the endl, it will actually be waiting on the next line. Let me put a bogus message here. Then Ifm going to say double average equals GetScoresAndAverage and then letfs go ahead and -- Ifm going to blow off the constant for a little bit because Ifm going to be mucking with it anyway, and Ifm not going to use that constant for very long, so Ifm not going to worry about it yet. I put my prototype up here so it knows what Ifm talking about, and then Ifm going to print out average is. Ifve got a little bit of structure there. I can even go in here. Copy and paste is a really good idea. Once Ifve written the function header once, I donft really want to make a mistake. Something thatfs interesting to know is that you can always write code that doesnft do what you want to start with but that lets you test some of the other pieces around it. In this case, I havenft finished fleshing out what GetScoresAndAverage is, but I can write code around it that says well, it passes in this number and it does some stuff. Right now, it just returns zero. Itfs totally bogus. Itfs not solving any of my problems, but it does allow me to test some other part of the code to make sure that connection and other parts are working before I go on to work on this part in isolation. One of the things Ifll try to show you whenever Ifm doing code is to try to give you a little of the insight to how I approach thinking about the code. I do think that one of the things that really distinguishes somebody who works effectively and productively from someone who spends a lot more time doing it is often just strategies and tactics for how you attack your problem. Ifm hoping that along the way, I can kind of share a little bit of the insights that I use to keep myself making forward progress. I am going to put in that four loop that Ifm looking for. Ifm really not this bad of a typist in real life, but itfs easy to -- the use of the braces here is whatfs defining the block structure. Without the open curly brace close curly brace, the four loop and the if and the other statements only grab that next line as being part of the indented body of the four loop. Ifm going to go ahead with my next idea. Ifm going to put that in the sum, and then Ifm going to do some things that seem bogus. Wefre going to get there. Ifm going to make some mistakes. Some of them Ifll make on purpose. Some of them Ifll actually be making accidentally. Wefll see which ones Ifm better at finding. I have my GetScoresAndAverage. It asks them for the scores. It adds it into the integer and then doesnft return anything. First, letfs see how the compiler feels about that. The first thing youfll notice is that compiled. That might alarm you just a little bit, because if you go back and look at this code, it certainly seems wrong. Anybody want to help me with one of the things thatfs wrong with it? It doesnft return anything. That seems a little shocking. It doesnft return anything at all. What is it then actually going to produce? What is it doing that is a little bit bogus? Sum is not initialized. These are two examples of errors that Java doesnft let you get away with. You may very well remember from having typed in some code like this in Java that it will complain. If you have a variable and you declare it and you donft initialize it and you start using it, the compiler says not happening. You need to go back there and give that guy an initial value before you start reading it. Similarly, you fall off the end of a function. It said it was going to return a double -- you better return a double. Every code path needs to return a double. You canft bail out early in some cases and leave it fall off the end. It considers both of those big enough errors that it wonft let you get away with them. C++ compilers are a little bit more lax. Itfs crack mom, I told you. Itfs like well, if you donft want to initialize that, go ahead. What value does it have then? It doesnft make it zero. It doesnft make it empty. It doesnft do anything nice or clever or helpful. Itfs like whatever kind of contents were lying around in that particular location, now itfs [inaudible] initial value. The result will be that we could get some pretty strange results. If I run this thing right now -- letfs see. I type in some numbers. I type in somebody got a nine, somebody got a 67, somebody got an 87. I type in a bunch of numbers, and the average is zero. That was interesting. Zero seems like it had some rhyme or reason to it, but therefs no guarantee. I could run it again and I might get very different numbers. I got zero the second time. Good luck for me. Letfs go in and start fixing some of this stuff. The thing to note is the compiler isnft nearly as aggressive about being on your case about things, so it does require a little bit more self-monitoring on some of these things. Seeing this result and going back and figuring out how I got into this situation -- let me get that guy an initial value, and Ifll go ahead and put a return down here. Wefll see if I like how this works any better. I put in five, six, seven and six, and the average is six. It kind of looks good, right? It makes sense to me that the five and seven cancelled each other out. But if I do a little bit more deep testing where I put in numbers like five, six, five and six, my average is five. In this case, I would think that I would be getting something like five and a half. I go back here and take a look at what I did. I took sum and divided it by NumScores. What have I failed to do? Cast that thing to get it out of there. The default -- this is the same in Java as it is in C++ is that if you are combining two operands in an arithmetic expression, if they are of mixed type -- one is double and one is int -- it promotes to the richer type. If one of those was a double, it would convert the other one to a double and do the calculation in double space. As it is, sum is an integer and NumScores is an integer. Itfs doing integer division. Integer division has [inaudible] built into it, so if I divide ten by three, I get three and that remaining one third is just thrown away. Even though the result is double, that happens after the fact. It does the calculation integer space -- the ten divided by three. It gets the three-integer result and then it says oh, I have an integer here. I need to convert it to a double on my way out. It takes that and turns it into 3.0. It doesnft recover that truncated part that got thrown away. If I really want to get a real double result, Ifve got to make one of those a double to force the calculation into the double space. The mechanism of C++ is a little bit different than it is in Java. In fact, the Java mechanism does work, but the preferred form in C++ looks a little bit like this where you take the name of the type youfre trying to convert it to. It looks a little bit like youfre making a function call-passing sum to a function that will change it into a double. By doing this, I now have one of them being double. Int on the other side gets promoted. If I do my five, six, five, six, that truncated part is now part of that calculation and preserved and pulled out and printed. Let me change this a little bit to something else. Right now, it assumes that therefs a fixed number of scores that you want to do. Maybe what I have is a big stack of exam papers, and I donft actually know how many are in the pile, and I donft actually want to count them ahead of time. I just want to keep entering scores until I get to the bottom of the pile. Ifm going to change this loop to allow me to enter numbers until I say Ifm done. Letfs take a look at what thatfs gonna look like. Ifm going to get the value here. Ifm going to change my code a little bit. Then if the value is the sentinel, then I donft want to include it in the sum. Otherwise, I do. Maybe wefll do this. Wefll say if the value doesnft equal the sentinel, then sum adds into there. I also need to make this thing stop, and so Ifm going to have to rearrange my loop a little bit here, because if you think about what Ifm going to try to do, Ifm going to prompt and get a value and then either add it in the sum or quit. Then Ifm going to prompt and get a value and add it in the sum or quit. I have a loop where I need to rearrange my notion a little bit here. One way I could do this is something like this. I could say this. While value does not equal sentinel, then add it into the sum. Ifm going to kind of change my code around. Watch carefully as I reorganize it. If the value is not equal to the sentinel, then Ifm going to add it into the sum and then Ifm going to get the next and overwrite the previous value. I inverted that loop that was there a little bit. It says while the value is not the sentinel, add it into the sum and then get the next value and come back around. The problem with this code as it stands is therefs something a little bit wrong about value. Itfs not initialized. I need to initialize it to something. What do I need to initialize it to? What the user wants. This little piece of code down here needs to come up and be repeated up here. I need to get a value from them and then go into if it wasnft a sentinel add it and get the next one. This is whatfs called a classic loop and a half construction where therefs a few things I need to do and then I need to do some tests and decide whether to go on with that iteration. As it is, therefs half of the loop that starts up. They call it priming the loop. Thatfs a little bit unseemly. Therefs something about that thatfs a little bothersome. Even though itfs a very small piece of code -- two lines of code is not going to rock the world. I do want to try to get us into the habit of thinking if we can combine that code and unify it, thatfs better. Itfs better than repeating it. Repeating it means that if I ever change something about it, I have to change it in two places. Therefs an opportunity for error to creep in that I can avoid if I can get it to where therefs really just one and only one version of the truth. Ifm going to change this. Itfs going to use the break statement, which you may have had a little experience with. Ifm going to run a while true loop, which looks like itfs going to run forever. Ifm going to prompt to get the value and if the value equals the sentinel, then Ifm going to use break and immediately exit the loop here without completing the bottom of this loop iteration. It causes it to move on to the statements after the loop. By doing it this way, I now have one prompt, then a check, and based on that check, I either finish this iteration and keep coming around or I immediately break out saying that was the clue that wefre done with this. I would say that itfs a little bit of an advanced question, but it turns out that the do while loop is not really that much cleaner in the end because you still have to do the prompt and test and decide when to add it in. Do well loops are just so unusual that they cause everybody to slow down a little bit when you read them. If you can write it using a more [inaudible] construct, there is some advantage to that. I got this guy together here, and then maybe I should actually tell the user -- this might be a good thing to say. Tell them what the sentinel is. I have to type better than I do. NumScores equals zero, and then each time we get one, we increment. Letfs go back. Wefll change our call to use a more ordinary value like -1. Wefll type in five, six, seven, eight, nine and then -1, and the average of those is seven. It should seem very familiar in a lot of ways but there are a few details that need to be different. Ifm going to show you one of the little C++isms while Ifm here because itfs something that comes up in the standard libraries and itfs worth knowing. There are some minor conveniences in the way that C++ provides certain facilities that are not conceptually a big deal, but you do need to know about them because youfre going to encounter them in various interfaces. One is the notion of a default argument. In the prototype for a function, there are arguments to a function where itfs some very large percentage of the time always going to want to be a certain value, but you still want to leave it open for the user to specify a different value in the cases where they donft want to use that standard value. One way of doing that in C++ is to write your function using a default argument where I say int sentinel and I said equals -1. That is providing for if somebody makes a call to GetScoresAndAverage passing an argument, itfs used instead. If they pass nothing, so they just have open paren close paren and they donft give a value for that argument, that means the assumption is to use that default. That means that most people who are making calls to GetScoresAndAverage can just drop this number and get the behavior of using -1 as the sentinel. In the case where for one reason or another -1 was one of the valid values that you could potentially enter, you could pick a different one. Wefll see that in the libraries. Itfs interesting. The mechanism of it is really quite simple. Itfs just a convenience to provide for. You can actually have more than one default argument. The idea is that you can only leave off when youfre making the call the last most argument, and then from there, other ones, because it has to figure out how to match them up. It matches the arguments left and right, and as soon as it runs out of arguments, it assumes everything from there has to be used its default argument for it to match that call. It allows for -- you could have three arguments of which you could specify two or one or three if they all had defaults if you needed that. Itfs not that common. Let me go back to my slides and tell you about a couple other mechanisms of some C++ features that are new to us that we want to know a little bit about. There are two types of user-defined types that are very simple to get your head around. You want to know what the syntax for them is and what they do. Itfs the enumerated type or the enumeration where at the top of your program -- up there where I was defining constants and doing #includes, this is where we put information for the compiler that is of use across the entire program. This was also where we would put new user defined types. In this case, I want to define a type direction T which can take on one of the four values, north, south, east or west. That whole package up there -- that enum direction T north south east west is the way of defining the new type. Youfre saying direction T now comes into the space as a name that can be used for variables, parameters, return types -- any place you could have used int you can start using direction T. It has the expectation that variables that are declared in direction T will be holding one of those four values. Itfs like north, south, east and west got defined as constants, and they are, by default, assigned the values zero, one, two and three in order unless you do anything special. You can use them -- you can do things on enums that are very integer like. You can assign them. You can compare them. You could use less than and greater than. You can add them. There are things -- they are largely implemented underneath the scenes as numeric types, but theyfre a nice convenience for making it clear that this thing isnft just any old ordinary integer. Itfs specifically -- it should be kept into this constrained range. It means something a little different. Itfs just a nicety. It does not have a lot of heavy weight feature associated with it, but itfs just a nice way of documenting that something is this kind of set up. This one -- the record of the [inaudible] type -- much more broadly useful. Itfs just an aggregate where you can take a set of fields of same or different type, give them names and aggregate them together. You say here is student record, and the student has a name, dorm room, phone number and transcript. Aggregate it together into a new structure type. In this case, the point T [inaudible], and so like this -- this is the kind of thing you put up at the top of your code that says herefs whatfs in a point T, the field names and their types, and a little point of noteworthy error producer is that there really does have to be a semicolon at the end of this. Similarly, there has to be a semicolon at the end of the enum for direction T. If you forget to do that, therefs a cascade of errors out of that that are really a little bit mystical the first time you see them. You learn to identify this quickly later on. It ends that declaration and then allows the composite to move on to the next thing, not assuming youfre still doing more work with this. Once you have this type declared, you can make variables, return types, parameters, all that stuff, and then the access to the members within the fields within a [inaudible] looks like access to an object did in Java where you have the variable name on the left and then a dot and then on the right, the name of the field youfre accessing, setting, and reading. You can do things like P = Q which does a full assignment of one [inaudible] onto another, so it copies all the fields over from one to the other. Itfs something simple that does have a lot of usefulness. It makes a lot of sense when you have a program and you do group things together. Herefs all the information about this part of my data structure -- a student, a class, a dorm -- all the information being aggregated together into one unit is actually a nice way to keep your data organized. There are two point T variables. One is P. One is [inaudible]. Ifm saying P.X. Ifm saying the X field of the P variable is to be zero. At this point, the P.Yfs field is nonsense. Itfs just garbage. I said P = Q, which basically says take the nonsense in Q and override it onto P and make P be as nonsensical as Q is in terms of its contents. Not very useful code. The last thing Ifm going to show you today is to talk a little bit about parameter passing. This is going to come up again and again, but this is just kind of a quick first mention of these things. Someone had asked earlier what is the parameter passing mechanism thatfs in play? The default parameter passing mechanism is whatfs called pass by value. It copies. If I have a function here binkie int X and Y, in the body of it, it tries to do something like double the value of binkie and reset Y to zero. When I make a call to binkie and I had A set to four and B to 20, when I made the call to binkie, the pass by value really means that the X and Y parameters of binkie are copies of A and B. They are distinct. They are new integer variables, new space, new storage, and they got their initial values by taking the current values of A and B and copying them. Changes to X and Y affect binkiefs context only. That four that came in here and got doubled to eight, that Y that got set to zero are live here, but when binkie exits and we get back to main, A and B still are four and 20. It just did full copies of all the variables, and this is true for all types of [inaudible] and enums and ints and chars and all that. Itfs copying the data and operating on a copy. In most situations, thatfs actually fairly appropriate. You tend to be passing information in so it can do some manipulations. In the situation where you really want to be passing in and having changes be permanent to it, there is an alternate declaration where you and an & to the type. Instead of being an int, itfs an int&, and that changes this parameter from being a pass by value to a pass by reference or a reference parameter. In such a case, when I make a call to binkie, this first argument will not be a copy. The second argumentfs still a copy because I havenft changed anything about it, but when I say binkie of A and B, what the binkie function is actually getting in that first argument is not a copy of the value four. Itfs getting a reference back to the original A. For the Y parameter, itfs getting a copy of 20. When it makes this change over here of trying to take X and double its value, it really did reach back out and change A. After this, A would be eight. B would still be 20. It allows for you to pass some data in and have it be manipulated and changed, so updated, adjusted and whatnot and then come back out and see those changes that is for certain situations very useful. This mechanism doesnft exist in Java. Therefs not a pass by reference mechanism, so this is likely to seem a little bit bizarre at first. We will see this a lot, so this is maybe just our first introduction to this. Wefre going to come back to this more and more. One thing I will note is that the primary purpose of this is to allow you to kind of change some data. It also gets used in a lot of situations just for efficiency reasons where if you have a large piece of data youfre passing in a big database, to copy it could be expensive. Copying just so you could hand it off to a function to print it would be unnecessary, and so by using the reference parameter in those situations, youfre actually just allowing it to avoid that copy overhead and still get access to the data. Sometimes, youfll see it used both for changing as well as efficiency. Ifm not going to show my last slide, but thatfs what wefre going to be talking about on Monday. Wefll talk about libraries. Looking at chapter three is the place to go in the reader. Ifm going to be walking over to Terman momentarily, and I would love to have somebody come and hang out with me. Otherwise, have a good weekend. You have nothing to do for me. Enjoy it. Itfs the last weekend in which you will not be my slave.