 Welcome back, in the last segment we saw implementations of two assignment operators for our class string. In this segment we are going to look at implementation of other operators. So first the square bracket or the indexing operator. So if we wish to access the individual characters of the stored character string we need to define this. So as we know if what has been written in the program is A1 this is read by C++ as A dot operator square bracket, operator square bracket operating on one. So this operator must be overloaded and that overloading is quite simple. So the receiver has a certain string and we want character 1 from it and where is the string of the receiver it is at A dot PTR. So we just have to get character 1 from A dot PTR that is about it. So we do not have to write A in this because the receiver is implicit. So this is going to be the receiver's PTR and we are going to take a displacement of I from it. So that is about it and notice that we are returning a reference. So we are returning a reference because that way we can change the character as well. So we would be able to write something like suppose we have A and A is PQR then we can write A of 0 equal to A of 1. So this A of 0 will also be translated or will also be interpreted by this operator. So this will return a reference to A0, so A0 is a variable and it will be a reference so it will mean that variable itself, it will not mean the value and so that variable itself will appear here so to say and therefore we can store into it. So that is why we are returning a reference rather than a value. So if we do this then if we have this defined and if we write this and we are allowed to write this then this will cause A to become QQR. Now there is an interesting operator concatenation for which we are going to overload our plus. So we would like A plus B to mean the concatenation of A and B. So we must create a new string first of all on the heap and store the concatenation into it. So how does that work? So we are going to return a string this time, so the result is going to be a string and our operator plus is going to be overloaded. So again let me just remind you A plus B becomes A dot operator plus of B, so therefore we need to redefine or we need to overload this function operator plus. So first we will create this string which is going to eventually contain the result and into that string we are going to store the result but this string has its own pointer and that pointer must point to the heap and how many elements, how long an array does it need on the heap? Well this array must contain the string from A as well as the string from B or looking at it here it should contain the string from the receiver as well as the string from the right hand side. So this space needed must be the length of the string pointed to by PTR as well as the length of the string pointed to by RHS dot PTR and a one so that we can append the null character to it. So we have allocated the space needed by writing this new thing and now we just have to copy. So this S copy that we had earlier is going to copy whatever is in the receiver into this result, the result area that we have created. This is created on the heap of course and then we have to copy the RHS string as well. So that same S copy is going to be used but we have to tell S copy that look I do not want it copied from the very beginning as what this would be doing but we want to copy it from index length of PTR. So we want to copy from RHS dot PTR index length of PTR, length of the string that we just copied. We are going to call S copy but this time we are going to give three arguments. So this is the destination, this is the source what we want copied and this is the displacement in the destination from where the copying has to begin and you can write this but this function is also given in the book in that same chapter 15 and finally res will be returned so the result of this will effectively be replacing this expression. So there will be a temporary which will be replacing this and then you can do whatever you want with it. It has c equal to a plus b so that then that temporary will be assigned to c but this evaluation will result in a temporary which contains the concatenation. So that is it, so that is the concatenation operator. Now we need the destructor as well and we talked about it but let us just do this again. So suppose we have this code which we had in the program that we had written. So at this point string b is created and at this point string b goes out of scope. So when string b goes out of scope c++ is automatically going to call the destructor tilde string. So tilde, well I should really say the destructor in the string class. So that will get called and it will be called on the receiver. So it will be tilde b actually the call will look like tilde b. So what do we want happening over here? So by the way there is a default destructor and the default destructor does nothing and we are going to change it. So we should delete p.ptr to prevent memory leaks. So how do we do this? So our class is going to contain this, our destructor is going to have delete p.tr inside it that is about it. So this is what will happen. If b goes out of the scope over here then it is as good as calling tilde b and this is the call that is going to happen. So this is the body of the call and b is going to be the receiver and so p.tr the p.tr of b is going to be deleted. So whatever memory b was pointing to will get deleted exactly as we wanted. And as I said earlier this works even if p.tr is null and in this case the delete does nothing. We also need to write the copy constructor. So remember that in our program we were calling some function f with a string object a as the argument. So how does c++ handle it? Well the copy constructor is called by c++ to copy a string object to the parameter. So this string object is copied to the parameter in f and that copy is a little bit of a special copy. It is not just like an ordinary assignment. It is a little bit more special than that and we will see in what way it is special and therefore c++ has a notion of a copy constructor. So the copy constructor looks like this. So it is a constructor and therefore there is no return type and it is constructing. So here we are going to use it to construct that parameter in f and so it is going to take one argument which is the object from which we are supposed to make the copy. So in this case argument is going to be a and rhs is really going to be a when this code gets executed and the receiver will be the variable which is the corresponding parameter in f. So what happens? Well ptr which is the receiver's ptr is going to be assigned, is going to be made to point to the string that rhs contains. So first it must allocate, we must allocate some memory and that is what we are doing over here and its length should be adequate for getting a copy of this. So rhs.ptr length plus 1 for the sentinel null. After that we are going to make a copy, the actual copy and that is about it. Notice that we did not delete ptr as we deleted in the case of the other assignment operations because this ptr is the ptr of the parameter of f which is of type string. So that is why we are copying this argument to that parameter. But this parameter is just being constructed as we speak and therefore it is not pointing to anything at all. So we do not have to delete ptr. And likewise there is nothing to be returned because this is a constructor and so in the assignment we were returning something. So the code looked a little bit more complex but here there is no chaining that we could be, that might be needed and therefore it is just going to be copying from this a to the parameter inside f and just this is necessary. So this is the core code of the assignment statement but before that we had to delete ptr in the assignment statement and then we had to return this object itself. But both of these things are not necessary as far as this constructor is concerned. And yes, so this is the code which is used to copy an argument to a parameter. But C++ uses the same code to copy a result back from the called function to the calling function and that same copy constructor is used and this code will work for that purpose as well. So what have we discussed in this segment? So we have given the definitions of all member functions needed to perform assignment, passing and returning from functions, concatenation, etc for our string objects. And these pieces of code should be inserted into the definition of the string. So next we will use this class to solve the problem of storing names and we will have concluding remarks for our entire lecture. But before that we will take a short break.