









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










ProgrammingAbstractions-Lecture
Instructor (Julie Zelenski) :It’s good to see you guys. Just a note about the handouts that are going out today so you aren’t confused about our ability to count in sequence – six, eight, nine and ten are in the lobby. Five and seven we’re going to bring at the end of the lecture, and I’m trying to avoid killing too many trees by what we do with five and seven. Five and seven are the handouts that tell you about the installing of the compilers and the debuggers of the two compilers, and there’s two versions of them. There’s a Mac version and a Windows version for the X code versus Visual Studio.
We printed an estimated amount of how many people we thought were going to be using the PCs and how many using the Mac, and so when we bring those in at the end and you’re picking them up on the way out, be sure to get the one that you need and not take both. Take the ones for the product you’re going to use. If you happen to believe you’re really going to use both, it’s fine to take both, but I figure that’s pretty rare. If you know what platform you’re on, take the one for yours and not both.
Those are the ones that tell you about how to get your compiler set up, which seems to be something a couple people this weekend were ready to get a jump on. Assignment 1 is also one of the handouts that’s going out today. For assignment one, just to kind of come up to speed is that the first assignment here is not some big, complicated program. It’s actually a set of small programs. Each is less than a page of code. They’re designed to exercise some skills in isolation – learning how loops and variables work, learning how our graphics library works, and learning how strings work in C++ and files.
The more complicated part is going to come from learning how to express yourself in C++. For those of you who are a little unsure about whether your background is good and whether you’re in the right place, this is actually a good testing ground. You should look at those problems and say oh, if I were solving this in my native language, I would be totally able to write this out without even stopping to think much. In C++, I’m definitely going to have some new things about learning how to convert what I know to how it works in C++.
If you do find these challenging to write, that’s probably a sign that you’re a little bit ahead of you. If you find them completely trivial and way too easy, you may want to think about 106X. One way to gauge that is to look at their first assignment. If you feel ready to do something like that, then maybe that’s a place that would give you a little bit more of the challenge you’re looking for.
We have all the section preference information in and we’re doing the big matching. We will email out the section assignments. Our sections mostly meet on Thursday, but some are on Wednesday and Friday. You’ll get an email tomorrow that tells you about where and when your section is meeting. If you have a conflict that has arisen post the decision making, we have a little bit of an ability to do some add late and rearrangement, but it’s pretty tricky. If you can make the section that you’re assigned to, that’s going to help us the most. We will try to accommodate you if we can to make a switch for you.
What are we going to talk about today? We’re going to talk about libraries and C++ and string and stream classes. There’s some [inaudible] mostly chapter three and then the handout that went out last week about general C++ has some information that’s useful. There’s also a library reference I gave out today which is handout ten, which is nothing but an overview that reminds you of some of the features. One other thing that’s going to be the theme for the next three lectures or so is going to be there’s a lot of material in C++. There are these huge libraries that do lots of things.
The goal of these weeks is not to make it so that you are an expert on all the minutia but that you are comfortable with the basic facilities and you know where to look to find out more. If you’re trying to figure out how the substring operation works on a C++ screen, what are the arguments? What do they mean? What are the cases I need to be worried about? You’ll know where to start and how to find that information as opposed to memorizing the whole of the library. It’s not really a feasible task and not even important.
Think about it as need to know. I’m going to get you the basics so you know what’s out there and then as you start to write code, you’ll learn the specific things you need to solve the problem at hand. Any questions administratively? I had one student come and hang out with me on Friday. I’m hoping that was because it was so last minute. This Friday, I don’t have a meeting, so put it on your calendars now for Friday hanging out.
C++ libraries – the notion of a library is really nothing more than saying you’ve got some functionality that you want to provide to all users of C++ or all students enrolled in CS106. There is some reasonable grouping to these things. You have a bunch of operations that operate on strings or that allow you to do graphic works or allow you to do event handling or something. The library is the packaging device in C++ where you say here’s a set of routines. Typically, it comes with two pieces. One is the interface or declaration or header file. It tells you about what routines are in there – what are their names? What are the prototypes? How do you use them?
It often contains good comments about the things that you would need to know as a client using that facility – how to use it effectively and correctly. There is the code that really implements it that when you make the call to a substring operation, how does it actually work? Well, there’s some code that backs it that does the operation that gets called at runtime when you make a call to that function. The libraries that we’re going to see this quarter form roughly two big groups.
There is the C++ standard library, so things that come with every C++ compiler. No matter where you continue coding in C++, you will always find things like the C++ string, the C++ IO stream, the file stream which is the F stream. There’s a math header. There are headers that deal with all sorts of other facilities that are beyond what we’re doing here. These are the ones that we’ll see most commonly in the early part are string and stream.
There is a set in our library – there are four functions that form the CS106 random library that are used for all kinds of random behavior that you want. I note here that they are free functions, and by free functions, I mean functions that aren’t on a particular class. They’re actually globally accessible. You don’t call them by sending [inaudible] particular object. They just exist at the top-level name space and you can just call them anywhere and any time. When you’re ready to get some random behavior, you make a call to one of these routines.
There is an initialization routine for this library. The randomized call – that’s called once and exactly once in your program, usually at the very beginning to set up a new random sequence. That’s what’s called seeds of the generator to get it started in a new place. Then, once you’ve made that call, you can intersperse any number of these calls to simulate certain random events.
The standard random number generator that’s in C++ provides all of this through one call. We’ll generate a random number from zero to the largest number possible, and then you can decide how to map that to other things. If you want the ability to flip a coin, you want to say half the numbers are odd and half the numbers are even. You could do something like generate a number and see if it’s odd or even, or see if it’s from the bottom half of the range versus the top half.
What these functions do is just provide that functionality and package it up for you neatly. You can say things like random integer low to high – you say one to five. It will give you a number from one to five inclusive. If you keep calling that, you should see an unpredictable sequence of the one, two, three, four, five coming in jumbled and mixing up. There are no real guarantees about what order you’ll see it that will allow you to simulate random events.
The random real – same sort of idea but in this case using boundaries that are expressed in real numbers and returning in real number. Again, the bounds are inclusive, so it can’t actually return the number low or high or anything in between. The last one is simulating a probability true/false value. Given the probability of 0.5, half the time it should return true and half the time it should return false. If you give it a probability of .25, one quarter of the time it will return true and the other three quarters of the time it will return false. It allows you to simulate coin flips or other random events where you have a [inaudible] distribution.
The point of a library is to take some set of facilities that are needed, package it up, have a vision of how they work together, a naming convention and design convention that makes them coherent, that they provide some convenience and they’re complete. They cover all the bases. These three provide a pretty good range of different kinds of random events. There are still other things you might need to simulate, but you can typically do it in terms of using one of these.
You could also have left one out and have to simulate the others from it, but each of them has a client use that’s pretty handy, so it actually has all three of them for your use.
[Inaudible] comes out of random.h in the CS106 library. The .h is just a convention for the extension for header files. .txt gets used on text files and .cpp gets used on source files. .h is for header files, which are descriptions of routines but no real code is typically in the .h file. It’s an interface file they call it.
In Java, there isn’t that distinction. Everything is all in one file, but the definition of a class serves as both the description for a client using it as well as the implementer implementing it. C++ has them separated. Let me look at C++ string as the next example of something we have to make use of in getting things done. The C++ string type is actually defined in a header file, and it’s a library that’s added into the language.
Unlike int and bool and double that are part of the language and can’t be separated from it, string is kind of an add on that’s defined through a library. It models a sequence of characters including everything – letters, numbers, digits, punctuation – and the string is defined as a class. In the same way that in Java you’re used to the class being the pattern from which you can declare and initialize objects that you can then message and do things with, string in the C++ world is the same sort of deal.
You have a string class. You initialize string objects. You send messages to those string objects to ask them to do things for you. Asking a string to give you the character at a particular position or the number of characters or to insert some characters or change some characters within the body of the string are all done by messaging the string. A couple simple operations – I put a little bit of string code to get started.
The variable name is actually string itself. There is something a little bit different about string when you declare it and you don’t initialize it relative to the things we know about primitives. When I say string S and I don’t say anything else, you might assume then similar to the primitive types that S is garbage. It has some sequence of characters. In fact, string has what’s called a default constructor, one that’s invoked when you don’t specify otherwise such that when you initialize a string with no other explicit information, it will assume you meant to set it up to be the empty string.
Making a call string S actually declares and initializes a string with no characters. If I were to ask it for its length, which is the way we ask for the number of characters in it that’s being used right here – for example, S.length and then close the open paren there. It would return zero on the empty string. We can use square brackets like the array notation you’re probably familiar with to access individual characters of the string.
Applying the square brackets to S – S sub I sometimes I’ll call this accesses the [inaudible] character within the string. The character’s index starting from zero – so if I have a ten character string, they actually are indexed zero through nine. The C++ string – the square brackets allow you to access that character both to read it and to write it. A C++ string is mutable. The Java string is immutable. Once you create a string, it has a certain sequence of characters, and although you can make a new string and overwrite that one, you can’t go in and just manipulate the string in place and change its contents.
The concatenation for the C++ string only operates on strings and characters whereas in Java, there’s this kind of automatic mechanism where things like doubles and integers are converted to string and added into the concatenation. That does not happen in C++. Concatenation is just for strings and characters. If you have something that’s in numeric form and you want to add it into a string, you’ll have to first convert it to a string. I’ll show you a routine that does that a little bit later.
I would be happy to do that. Most of the things that I’m talking about actually are in handout four was well as repeated in handout ten and in the reader. There are a million places you can look for information on strings.
Most of the heavy lifting on the strings is done via these member functions. These are part of the string class, and so these are operations that apply to string receiver objects. They’re not free functions. You can’t call them outside of a usage where you’re saying on some receiver string, apply this function using these arguments. For example, the length member function is applied to a string.
Just to note here, the word member function is vocabulary-wise the same thing as method. Java programmers tend to call the functions that are defined as part of a class methods. C++ programmers tend to call them member functions. They really mean the same thing, but I do try to use the word member function because we are a C++ class, and that is kind of the convention. I’ll probably end up using both accidentally without even noticing it. Hopefully, it won’t cause you too much grief there.
The member function here is saying str.function R is saying apply the function, send the message function to this particular string with these arguments and then get its answer back or have that operation happen. I can ask a string for its length in terms of an integer. It tells you the number of characters. I can ask a string to look for a particular character or string sequence substring within the characters that that string maintains right now. It will return the index of the first occurrence found, scanning from left to right or a string::end pause. It’s a little bit of a funny return value, but it is the return value that says I didn’t find it.
It’s a string::end pause. It’s an integer value that is distinct from any other valid index within the string itself to tell you it didn’t find it. Both of these have a default argument on them. We talked a little bit about that last time. If I do not specify that second argument when I’m making a find call, it will assume that you want to start looking from the beginning. If I do specify it, then it will start from that position and scan from there to the end of the string. It’s a way of targeting the place you’re looking for a little more precisely than just starting from the beginning and going to the end.
C++ does allow what’s called overloading. In this case, the function find that finds a char and the function find that finds a string both have the same name, and so that name can be used for multiple purposes as long as there’s a sequence of arguments that distinguishes them so that when I make a call to find, it knows whether the first version or the second version by virtue of whether the first argument is a character or the first
argument is a string. That can be extended to other types. This is typically used when you have an operation that really has the same behavior but some slightly different sequence of arguments is required to invoke it.
It is not something you want to use a lot to make a bunch of similar named functions that don’t have similar operations. It allows for a convenience when there are two or three variations of the same theme. They might all come under the same name by virtue of overloading.
Substr is something that given a receiver string and a position in a length will extract a new substring out of the middle of the string that was received. If I take the hello string and starting from position zero take two characters, I get the string he. It copies them. It’s distinct from the original, and so all it did was get its initial sequence by copying characters from there. If I go in to change the hello string into jello, that he string stays he. They’re not attached in any long-term way.
Insert, replace and erase are all of the family of something that I call modifiers or mutaters that change the receiver string. You can send these messages to a string to cause new text to get added into the string, text to be removed or text to be deleted and replaced with something else. Inserting – someone asks, well, how can I put new characters in the middle? Well, I put the position where I’d like them to go. If I say position zero and I say put the string I in there, then it would bump everything down and put I in the front and replace it. If it was hello, it would be I said hello. I inserted the string I said.
The replace at a position removes length characters starting at that position and then replaces it with that character. It’s a way to take a chunk out and put something else in instead. Erase does a straight remove at a position. Take this number of characters and throw them away, deleting them from the string and making it shorter. All of these change the receiver string. When you say str.insert, str.replace or str.erase, after that call, str now actually has new contents based on what you’ve asked it to do about changing and mutating its contents.
Here’s something I should tell you a little bit about C++ string relative to Java string. C++ is kind of an industrial strength language that’s targeted at professional programmers. It does not make any guarantees to you about what happens if you misuse these calls. If you give it a position that isn’t valid for this string or a length that isn’t valid for the string, there is no contract in the C++ libraries that said this is what will definitely happen. It doesn’t say oh, it’s definitely going to throw an exception or throw some sort of error. It doesn’t say it’s just going to truncate it at the end.
It says that the library is free to do whatever is convenient for it up to and including just crashing. It does mean that as the programmer using these calls, it is a little bit more on you to be careful that you’re using them correctly and making the numbers inbounds for the string in ways that will produce correct results. It might be that it will produce a nice error message, but there are no guarantees. You wouldn’t want to come to depend on that. You want to just be careful about knowing what the right numbers are.
change my strategy entirely just so I have a little practice using some of the other routines, and I’m going to end up using the string find.
We’ll start with this. I can actually do this. S.find of CH, and if I don’t give that second argument, it’s going to start from the very beginning and look all the way through and see if it finds it. I’m going to put a hold that result at a variable and I’m going to say while found equals the result of calling find and then I’m going to stick it in. This is a very C++ way of coding. It’s tightly combining this up. In this case, I have an assignment and a comparison all in the test of the Y loop.
I’m making the call to ask the string to find a particular character, storing that result in an integer here so I can use it and then comparing that resultive string end pause. So the string end pause is a little bit of a funny C++ syntax there, but the way to read that is within the string class – string:: says within the string class, scope within the string. There’s a particular constant called end pause, which is used as the return value in cases like find when it’s looking for something. End pause being part of the class is a way of avoiding it conflicting and interfering with any other usages where you might have variables named end pause or similar functionalities.
It’s tied to the string class through the scoping mechanism. I check and see if it’s not string end pause, and then if it’s not, then I go into the loop here and I can do an erase of one character at position. Erase takes the position in the count and removes the number of characters I specified from that position. Then, it will come back around. I have passed string by reference coming into here, and that’s a very important part of what’s happening here because these calls to erase that are modifying string – if I have not passed string by reference, they’d be operating on my copy.
I’d go through all the trouble of erasing all the Cs in my copy, but when I got back out to the main call, none of those effects would have been permanent. Passing by reference really means that what remove occurrences got was access to the original S. I should make these names – I’ll call this my string out here so that we don’t get any confusion about the two names. The my string variable in main is really being accessed by remove occurrences without a copy. It’s reaching back out into main and making changes to the my string itself.
What does it not like about that? Oh, pause. I called it found. I’ve achieved the same thing. That’s one of the things about the string library is it’s so big and has so many different ways of doing things that often two people or ten people running the same task won’t even come up with the same solutions. I could have used a replace where I replaced the character it found with the empty string.
I can build it up through concatenation. I can take it down with erase and replace. I could insert the other way around. There are a bunch of things I can do that in the end will achieve the same effect but show that there are a lot of ways to accomplish the same things. The library is pretty rich and has a wide variety of tools in it.
I’m going to make one change to this to show you how I can make it slightly more efficient. This is silly because strings are typically very short, so it doesn’t really matter, but I’m going to do this and I’m going to use found as my index on subsequent calls. I can say starting at found from zero, do my search from zero and then found, and then any subsequent calls will pick up where I left off. The next time around through the loop, found is at the place where I found a previous occurrence of that character and it says starting from that position now, look forward and see if you see any more from here to the end.
For a very long string like this, it ends up doing a lot less work. It doesn’t start at the beginning each time. It just picks up where the previous occurrence was found and goes from there to the end. It’s a small change, but no big deal.
Basically, what you’re saying is find needs to return something that says I didn’t find it. It could return to you zero, one, two, three, all these indices. It actually needs to return to you, and it uses a special sentinel value that says I didn’t find it. You might think that might be negative one or some other thing. A good programming form would be to have a constant for it so that you don’t have any magic numbers embedded in your code. That constant is defined as part of the string class. Just the syntax for accessing that constant of the string class is using the string class name :: end pause.
It’s basically the syntax for I have a constant that was defined within a class. How do I get to it? I use its class name, two colons and then the name of the constant. It’s just C++ for something that in Java looks a little bit more like class name dot. Question?
Student: Sometimes I see a function declaration before [inaudible] and then the definition afterwards. Is that just a matter of preference?
Instructor (Julie Zelenski) :It totally is. Probably I’m being a little bit lazy in class, which is if I put the function definition up here, then I can call it down here because it’s already been seen. If I put it down here, then I need a prototype up there. The prototype means I have to be a little bit more careful. When I change the name, I have to change it in both places. If I change the argument, I have to change it in both places. The problem, of course, is that when you read the code, it probably reads a little better to say here’s the main which makes calls to A, B and C.
Some of it has to do with it’s a little bit harder to maintain in that form, but I think it’s easier when you’re done to read it. You’re totally free to do it either way. You should probably pick a strategy and go with it. Maintain the prototypes is not really that much work once you get used to it, and I think in the end, it probably is a little bit cleaner. When I’m being lazy in class, I’m much more likely to just throw them up there to save myself some time. It’s good to note that there are a lot of things that will slip by me if I’m not being careful.
Let me go back and pick up a few last details about string that I don’t want to overlook before I move away from this. There are library functions that are need to know. I have
There is a member function on the string class that will return to you an old style string from a new style C++ string, and it’s called the C_str [inaudible]. They let you convert. Why do you care? It turns out there’s one thing you’ll definitely run into, which is when you’re opening a file stream, you want to say this is the file on disk that you want to identify. It turns out that that library requires the use of a C string as the name. There was a little bit of an issue trying to get all the libraries to come together at the right time, and it turns out the stream library got finalized before the string library was done, and so it depended on what was available at the time, which was the old style string.
Even years later when they’re both happily debugged and working, it is still the case that when you use the stream library, you have to describe the file you want by using the old style string. If you had a C++ string variable that held the name you wanted, you’ll actually have to convert it. Converting in the other direction comes up in one case, and I’m going to show you this one.
It has to do with concatenation. The plus operator that does concatenation really wants to work on C++ style strings, so if one of your operands is a C++ string, it’s all fine, as long as the left or the right side is a C++ string. The other side can be a string literal, a constant, another string, a character variable – all those things work fine. As long as at least one of the operands really is a true C++ string already, you’re good.
That’s almost always the case. But in the case where you somehow have two things on either side of the plus, neither of which is already a C++ string – typically, that means you have a C string on one or both sides, a character on one or both sides – you are not going to get concatenation. If you try to add two C style strings, it actually won’t compile.
The sad thing about these two things, about taking a string literal and adding either a character constant or a character variable is that it does compile and it just does not do what you want at all. It does so in a silent but deadly way. I’m not going to tell you what it does, but if you are curious, you can come and talk to me and I’ll lay it out for you. What I want you to come away with is this memory that when I’m using concatenation to be sure that one of the two operands is a C++ string. If you have to, force one. If you have a string literal and you want it to be a C++ string, then make it one to avoid running into this.
The mistake that you get from this is actually quite mystical and very confusing. Probably 95 percent of you would never run into this, and so mostly, I’ve just confused you for reasons that seem unclear, but for the five percent that are going to run into this, I’m really trying to do you a favor by giving you a heads up before it causes you a lot of grief later. Just a little bit of a legacy. C and C++ go back a long way, and as a result, we sometimes have little quirks we have to deal with even in the modern world.
Student: Is that the only way you can convert a C string into a C++ string?
Instructor (Julie Zelenski) :Not exactly. A lot of times, it just happens automatically is the truth. In almost all situations – if you had a routine that expected a string argument and you passed the string literal, it will automatically convert. Mostly, you won’t need to do this is the truth. It happens all the time behind the scenes without any effort on your part. This is the official way to say I’ve got a string and I really want to force it and I’m not waiting for the compiler to do it on my behalf.
In particularly, for example, in a situation like this, it doesn’t realize what you really wanted to do was convert this and then to concatenation. It does something kind of goofy based on what the old meaning of taking a C string and adding a character to it was, which was not concatenation. That’s a little moment of silence for old language C that comes back to haunt us a little bit like a ghost in the attic.
Student: What do you mean by a string literal?
Instructor (Julie Zelenski) :A string literal just means a string constant. It’s something in quotes.
Student: Without explicitly declaring it to be a string.
Instructor (Julie Zelenski) :Yeah. A string literal is when you see open double quotes, some characters, close quote, that’s a string constant or a string literal. In any situation where you see exactly that – not a string variable is basically what I’m saying there.
How do we do IO? How do we do input/output in C++? Let me first say that input/output is probably one of the more distinctive features of any language. C’s IO, for example, looks very different than C++’s IO, which looks kind of different from Java’s IO. These are areas where for some reason, even though they all do the same things underneath it all
For one reason or another, these are the areas where they’re widely divergent in their syntax and the way you express what you want to do. That makes them particularly annoying to learn is the truth. I know a lot of IO systems, and they’re all very jumbled up in my head. At any given moment if you asked me how could I print a decimal number with three digits of precision in this language, I’m going to have to go look it up. My motto is look it up. Don’t worry about memorizing these details, because they are very tied to any particular language and its formatting system.
That said, we’re going to use a little bit of IO. We’ll need to be able to read and write things to the console to interact with the user. We’re going to do a little bit of file reading, reading numbers and strings from files, maybe even producing some files. We’re going to use some very simple set of features. We’re not going to go too deep. When you need to know more, there are great resources to go check into for that. I wouldn’t in advance go make yourself an expert on any form of IO. Figure it out when you need to.
this way. I’d have to stop it after one anyway, check to see if it failed, if not, go back in. It’s kind of misleading to even show this form, because that form assumes that the user is a perfect typist and never makes mistakes, which is in this day and age not too likely.
We’ll talk more about file streams on Wednesday. On your way out, look for handout five and seven for Mac or PC depending on what you’re using and good luck getting your compiler set up. I’ll see you on Wednesday.
[End of Audio]
Duration: 48 minutes