[MUSIC] So we're going to take up C++ Functions and Generics. And then our other big topic, is going to be graph algorithms. And, graph algorithms are, if you haven't studied this in your backgrounds if you recall, I said most of you should have at least the equivalent of one year of computer science especially data structure topics. And graphs are one of the critical data structures in all of computer science. And the critical homework in this class is going to involve homework on graphs. So that's going to be how we learn our C++. We're going to learn C++ or we going to learn some interesting computer science ideas, or at least for many of you, you probably already know this that will be review. So it will make it easier for you if you already have a background in which you understand the minimum spanning tree algorithm or the Dijkstra's shortest path algorithm. In any case those will all be reviewed and there are also many sources in which you can go to and read carefully about the notably the Wikipedia. So here's some of the new stuff we've had. We have default parameters in C++. We have variable argument lists in C++. We have the use of const in C++ including const as part of the signature for function parameters. So far when we have looked at generics we only our recipes, our generic recipes have had a single meta variable typically of t that we get substituted for. But it's also possible to have generics that are far more. complex, and can involve multiple types in the generic recipe. And while I've alluded to operator overloading. Especially the notion of overloading the bit shift operators to be IO operators. I am going to also begin to explain how you do your own operator overloading. So this is new stuff. We are going to see. So little review, how in C did you write a function to sum for example on a rate of doubles. And we've seen such a function in C. A couple of things that we notice is, is the signature for an array. Alternatively that could have been written in pointer notation. So this also would've worked. Either one, cause basically in the C community arrays are just base pointers. Now we have an initialization of the summing variable. This is going to be the loop variable. And again this is just a very standard idiom for handing, handling arrays an idiom for array's we have a size. I guess passed them, that tells us how many elements to sum. So just t remind you again of the dictionary definition generics relate to entire group or class. It's general, some senses were using here just a general style of programming. Biology, it means. Very specifically relating to a genus. Not having a brand name is another designation where we could have generic soap. And that's very often used in the pharmaceutical industry. Where you mean something non-proprietary. And then finally it, in grammar, it refers to neither masculine or feminine genders. So again, there's a little bit of universality. So generic programming is somehow or universal kind of programming. Writing code that can use an arbitrary type or types in C++ done with a template. And we saw it. And the one argument situation. So here again is let's see how you would do that for the generic sum of an array. That was what? what we were asking you to do as, as, a little quiz assignment. And, we typically when we're summing something like an array, we want the summing variable to start at 0, but we will also see that we want to, might want to generalize that. For example, we might have two data sets, and we want to pass in one data set, sum it And then whatever its value, its summable value is. For example here, it would have been 24 in dataset one. We want to continue with dataset two and sum it as well, starting at 24. How would we do this, generically? Here we see some of those C++ features. Here we see ideas that are very standard, any C programmer could manage this. But immediately, you get a lot more power in C++. So again, we have this metavariable, T. possibly I should have called it, summable, that maybe a better name, keep in mind that this won't work universally, it will work when we see this operation working properly. Now we have here a default value. [BLANK_AUDIO]. And what this means is, whatever that type is. The summing, where we're summing S, will ordinarily the usual case, the usual suspects, the usual case. So this means that I don't have to specify things when it's the usual case, ti's going to be assumed to be 0. But if it's not the usual case I will have to substitute an actual parameter for this formal parameter. And here also we see this other idea in C++ where we can localise to the for statement through a declaration in this first component of the for statement. a local counting variable. So we have in C++ the concept of default parameter. We also have in C++ the concept that we can use. const correctness. So we can't do this in C but in C++ by specifying const here that means any data sitting in that array is not mutable. That means the compiler can, presumably, check that, that criteria is met. So that you don't inadvertently in this program mess with your array contents. So again that's good practice. If I leave const off, nothing changes. Nothing changes. So constants really a little added documentation. And something the compiler can automatically check. Now, with a default parameter we are allowed to have two different signatures. [SOUND]. Here, the actual parameter would mean s started at 58. Here, by default, because things got left out, s would start at 0. So, again, we have to, in C++, really understand what we call the signature matching algorithm. Its selection mechanism. The compiler uses it. And uses it to properly choose and write code. We could, by the way, have had a default to size. We can use more than one default in the argument list. So lets say most of my data sets are size 100 for whatever reason. So then if I wanted to this could be defaulted. Now it turns out, and you might want to think about it, I can have a bunch or arguments, but if I have default arguments or default values All the defaults. There can't be a default that skips over an argument. Because there's no way of positionally determining what is the default value for an argument. Okay, you can play with that if you didn't. understand what I said and think about here's the main program and that main program we're going to use that a template. There are two different types of arrays this is an array event. And this is an array of double. And then a compiler knows what to select and in both cases there was a default, in this case it was the sum s equals 0. In this case it's also going to be s equals 0 but this is going to be in the double domain. So you might want to, you might realize that, that notationally is really 0.0, it's better thought of as 0.0, and the compiler will write that code for you. So again, this shows you the power of templates, the power of generic programming. Elements of the array source are not to modified, that's the meaning of const correctness. Simple parameters, such as size, are typically called by value, and cannot be modified by the calling routine. So, we didn't declare size to be const, because There's no need to, that's just a standard call by value and it would be redundant, and this concept is called const correctness and is emphasized throughout the C++ language. Alright, as an exercise, take that code And instead of iterating over addition, iterate over subtraction. And as a further exercise to show that you really can manage templates and see their power, write a template function that instead of using an arithmetic operation does output of the array elements. And I'll leave that for, for an exercise for you. So, the answer is to provide a different operation here. And we could provide arbitrary operations. That this would be repeated multiplication. In this case, we provide a subtraction. That would have been the answer. And here is a different form of processing entirely. Pretty simple. Again We have chosen to use a very standard idiom for processing arrays and then in that idiom we can chose whatever we need to do when we handle the array contents, and templates become very powerful. Because if those are universal methods, methods necessary for the contents of an arbitrary array, the template just means we can write it once. debug it on a standard type and, and then stick it away in our library for use whenever.