 Welcome back, in the last segment we gave the definitions, the complete definition of our string class. Now we are going to use that first to solve the problem with which we started this lecture. So the problem was how to store many names of varying sizes. So here is the program, so we are going to read 100 names and store them. So for that we are going to allocate an array of strings. So it should be instructive to just see what this actually does. So in the main program this is going to create an array of string objects. But let me remind you that a string object is really very simple, it just contains a single PTR. So this is the PTR. So we have 100 PTRs really because each object just contains one PTR. So 100 words will be allocated in the activation frame and these PTRs will be null by the way at the beginning. Then to do the reading of the names we are going to use this buffer of 80 characters. This is just meant to be a large enough number. And now we are going to read each of the 100 names. So in this loop iteration we are going to do that. So this time let us say we use the safe alternative but this will also allow us to get in the spaces. So this will cause the name to be read from the keyboard, new line terminated into our char array buffer. And of course inside the buffer it will be stored with a null termination. And then we are just going to assign names of i equal to buffer, that is it. So if you remember what is this, how is this going to work, names of i equal to buffer. So this is going to be viewed by C++ as names of i dot operator equal to of buffer. And if you remember we defined this operator equal to, a minute ago, an operator equal to which takes a char array as argument. We defined that a minute ago. And so what that will do is it will allocate some space on the heap and it will start pointing this pointer to this and it will copy whatever was in the buffer into the space. So in this code we are not mentioning things like how many characters did we read into buffer. But all that will happen as a result of this assignment. So that is it, that is the code. Now you can use this names array however you want. You can do whatever you want with the names inside that array. So again I just want to point out that we have solved the problem that we started off this lecture with. The number of bytes used for names i will be equal to 1 plus the number of characters. And incidentally this was exactly the solution we got. So even in the solution that we got very early on in the lecture we had the same situation. In the activation frame we had an array of pointers. And here we effectively have an array of pointers, although we call it an array of string objects the string objects themselves are containing only one member called which is a pointer. And that pointer points to something on the heap, on the names which are stored on the heap. But the beauty is that over here in this solution we are not talking about the heap at all. We are not talking about how many characters we are transferring, how much we are allocating and anything like that. So all this is implemented automatically and we are not mentioning memory allocation. And we do not have to worry about memory leaks, we do not have to worry about tangling pointers. Our implementation of the string class takes care of all of these things. So I am now going to give a quick demo which uses our string class. In this program we did not use the concatenation feature but in this demo we will be doing so. So let us take a look at that. So yeah, so this is numbers. So here I have copied the length and S copy functions and then over here our string class starts. So let me just move it to the side a little bit so that the first character is not cut. The string class is here with the member functions. So all the operators, operator equal to's and everything is there. And this is the destructor which we also wanted. So what is our problem? So the problem that we are going to look at is sort of a fun problem, nothing useful by any means but just a fun problem. So what is the problem? So I guess I am not going to be able to fit this in a screen. So I will tell you what we are going to do. So this program is going to read in a number from the user first and then it is going to print out something. So that is about what it is going to do. So what is it going to print out? So it is going to read in a number which it expects to be a two digit number. Actually it expects the number to be between 20 and 99. So if you type a number between 20 and 99, say you type 37, then this program will print out 37. So it just writes the number in words and it does this only for a two digit number. But it illustrates the kinds of things that we have been doing over here. So what happens here? So first we have an array of strings. By the way once we define strings then the C++ machinery will allow us to define arrays of strings, that is no problem. So we will have an array of strings, one array called units and another array called tens. So in units we are going to put 1, 2, 3, 4, 5, 6, 7, 8, 9, we could put a zero over here as well but we really do not want to because we will see why we are not putting a zero. And in tens again we are not putting a zero but we are putting 10, 20 all the way till 90. So what is this doing over here? So you give me a number then I am going to extract the tens digit from it. So that is simply z divided by 10 and then I am going to use that to index into the tens array. So suppose the number I typed was 37 then the tens digit or 37 divided by 10 is going to be 3. So I am going to index into this and now that will get me my 30. So remember I am supposed to print 37. So I have now been able to get 30 from this. Then I want a dash. So dash is another string called dash which I have assigned to this character dash. So I am going to add it or I am going to concatenate these two strings together but I also want the units place. So to get the units place I just have to do z mod 10. So for 37 this would be 7. So units of 7 would be this so I would get a 7. So from this I would get 30, from this I would get dash and from this I would get 7. So I would get 30 dash 7 concatenated which I am going to print. And since print is not putting an end line I am going to put an end line myself. So that is all there is to the program. It is kind of a trivial program but it does illustrate some ideas. One of the ideas is that you can use an array of characters to do some interesting things because you can index into that array. But most importantly I am doing this because I want to show you that here we are able to concatenate the objects which we are storing in our string class. So let us compile this and run it and let us run it. So let us say we type 37 so we do get 37. Let us do one more say 85 and we will get 85 but let us do something improper. So let us say we type 5. So let us see what we got. So we got the 5 alright but that 0 in the tense place did not come as it should not have come but we did get that dash because the dash is there. So maybe we should write, we should modify our code so that the dash also does not appear. And of course you really should make some special cases, you should put in some if statements to check whether the numbers are 11, 12, 13, 14, all the teens basically and for all the teens and 10 itself and 11, 12 there should be a special case and those cases should also be handled. So anyway so that is what this program does and as you can see it uses the capital string class that we just created and we are really at the end of the lecture so I just want to make a few concluding remarks. So our class string performs memory allocation and deallocation behind the scenes and it happens automatically in the sense that the user is not really aware of what is going on. Now string variables appear as simple as care variables. So when you use care variables you do not really need to worry about where are these stored or you could think of them as being stored in the activation frame and if you want you can think of the string variables also as being stored in the activation frame that would not really cause any major problems. The only difference is that the string variables can contain character strings of arbitrary length rather than individual cares. So in the next lecture we are going to see a class from the standard library called string where S is not capitalized and we will see that it is a richer version of our string class. But the point of discussing this capital string was to see how such classes can be implemented. So the basic idea is that these classes that we are going to see in the that appear in the standard library and some of which we are going to see soon use memory allocation and deallocation from the heap and as an example we have studied this, we have designed and studied this capital string class and there are other classes in the standard library which we will discuss and one of them will directly enable you to store polygons, variable sized polygons as was mentioned at the beginning of the lecture. So that concludes this lecture, thank you.