 Welcome back. In the last segment, we made a high level plan for designing our string class. Now we are going to jump into the implementation of it. Okay, so let us start off with a equal to pqr. So this is an assignment statement. We want the character string pqr to be stored in our string variable a. How should that happen? Clearly as we said a.ptr should be pointing to the string pqr and our rule was that a.ptr should point to things on the heap. Now since we said that we cannot immediately just directly set a.ptr to pqr. After all we know pqr and we know that pqr is a const char ptr. So we cannot just write a.ptr equal to whatever that pointer is because we decided beforehand that the member ptr will point only to the heap memory. Okay, why are we doing that? Well if we know that it is only going to point to the heap memory then we can delete it. If we do not know that, if there is a possibility that sometimes maybe it points to the heap memory, sometimes it points to activation frame then we cannot delete it. And therefore in order to be able to delete it confidently whatever data it points to should always be on the heap. And therefore we must first copy the character string constant to the heap. Alright, so that is the first consideration that we must copy pqr to the heap because it in general it may not be on the heap. But then there is also another consideration a.ptr we want to set to point to this pqr but it is already pointing to some variable on the heap that is what our convention is. Well unless it is null but in general it could be pointing to some variable on the heap. So what do we do? We are making a.ptr point to something else so this variable that it points to is going to be useless and therefore we should be deleting it. Why is that? Because we are guaranteed that no other pointer points to that variable. So therefore we can just delete a.ptr. So that is all there is to it. So the way the implementation works a equal to pqr for a equal to pqr we should do what I just said but the way to do it is that this statement for this statement c++ makes a call to a.operator equal to with a being the receiver and pqr being the argument. And so whatever I just said has to be put inside this function. So we have to overload this function. So what are the types of argument? What is the type of the argument to this member function operator equal to? So this is a character string constant and we know that this is represented by a variable of type const char star and whose address is the starting address of this array. So the member function operator equal to will have a parameter const char star so we can just so this call will be a valid call. Then what value should it return? So this is a little tricky. Now a equal to pqr wants us to assign pqr into a but if you remember we can chain assignments. So what does that mean? We can write something like b equal to a equal to pqr. So if this assignment operator is a part of this expression then this assignment operator is supposed to produce a value and that value should be returned by the member function operator equal to. So what is the value that this is supposed to produce? Well it is supposed to produce a reference to this a itself. So from which we can pick up the value. So the return value should be the left hand side. So it should be this a itself and the return type must be a reference to string because a has type string so we should be returning a reference to that string. So here is the core so we are defining a member function operator equal to in the string this will go into the string class and it will take so it will be operating on a string will be the receiver and it will be receiving as argument a character string constant so some variable of type const char star and let us call that variable rhs. So first of all we said that if we are implementing this so we are changing a.ptr so a is the receiver we are changing a.ptr so whatever a.ptr is pointing to should be deleted but since a is the receiver then this is just ptr remember that if it is a member appears here without any qualification then it is just the pointer member of that receiver so we are going to delete that ptr. So effectively what has happened now is that we were assigning we were executing this statement the memory that a was pointing to a.ptr was pointing to has now been returned back to the heap. Now we said that we have to make a copy of this pqr on the heap so how do we do that well we are going to allocate an array of size length of this rhs so for pqr it will be just 3 plus 1 byte to store this the null character. So how do we do that so in section 15.1.4 we had this length function which we talked which we mentioned earlier and we just call that so length of rhs will tell us how long rhs is we add one to that and we allocate an array on the heap so mu does that and its address is put into ptr. So this is the array that has been allocated we have not yet moved the data into it so this whatever that string constant rhs is it only its length has been used up the data actually has not been used up. So the next step is to actually do the copy so again we had this scopy function which is very simple which actually just copies every element of this into this until we reach the null and the null is also copied so there is a simple loop but anyway you can get that function from this section and finally we are going to return this receiver itself so remember a equal to pqr got translated into a.operator equal to of pqr. So this is the receiver and we want to return the receiver and here is how we return the receiver so this is a special word in C++ so let me just explain that to you so this is a C++ keyword and inside a member function this is a pointer to the receiver so star this would mean the receiver itself. So here that is exactly what we want we want to return the receiver itself why are we not returning a pointer because this says that we want to return a reference this says we want to return a reference and the references and references are two variables so here we should not be having pointers but we should be having references so then reference can be picked up so we should write star this over here so this is a keyword and this would mean the receiver itself and the value of an assignment var equal to expression is var so the assignment must return var as a result and hence the return value is star this so that was assigning a character string constant to one of our string variables but we would also like to be able to assign one of our string variables to another of our string variables. So we want to allow code such that a equal to b a equal to pqr and b equal to a so how do we do this so the statement b equal to a is going to call is going to be looked upon by c plus plus as this statement so it is the object b and we are making a member function call on it so b is the receiver and the argument is a so this is another assignment and it is very similar to the assignment that we made earlier except that now the argument is not char star but it is a string variable and if you want to get the actual string that we want copied we have to go through its pointer so we will do that so we want a member function operator which takes string as our argument we already have a member function which takes cares const char star as an argument but we can have another so we can overload we can have more functions so long as the signatures are different okay so this time we have string reference returning being returned name of the member function is operator equal to in in the class string and the argument this time is not const char star but const string reference RHS so I could have had RHS over here but I do not really want unnecessary copying and therefore I am going to just get a reference to it so we have to be a little bit careful in all of this okay and in principle our programmer is allowed to write something like a equals a so a equals a is really nonsense but it is not disallowed and therefore we had better deal with it okay so the best way to deal with things like this is to just make a special case so what we are going to say is that if this if the receiver is the same okay if the address of the receiver is the same as the address of the right hand side then that means we are in this situation so in that case we have nothing to do but we can just return this object itself okay and of course a reference to that gets returned but we are returning this object itself okay so suppose now for the rest of the code that the receiver is different from this right hand side okay so we are in a situation not like this but we are in a situation like whatever a equals b where a and b are different strings okay in that case as argued earlier we are going to change ptr and if ptr points to something and ptr will be pointing to something okay we have to release that memory so for that we have to issue delete ptr and delete ptr is fine even if ptr is null by the way okay so so that is not an issue okay but if ptr is not null then we have to return that memory back to the heap and that is what this statement is going to end up doing okay then as before we are going to make a copy of the string in the RHS on the heap okay so for this as before we will allocate length which is RHS dot ptr a length of RHS dot ptr dot ptr plus one that one is again to store that null ptr okay and this is done quite easily by a very similar statement as before length of RHS dot ptr plus one okay so that much memory we are going to allocate on the heap okay and we are going to copy we are going to copy the memory that RHS points to or the string that RHS is RHS contains is being now copied to the receiver as well. So ptr which is the receivers pointer the address that it points to will now contain whatever is contained in RHS dot ptr okay so for this we again use this copy function so it just essentially transfers characters from copies characters from here to here at corresponding displacements and finally again as before we are going to return this the variable that this points to okay that is it. So what have we discussed we have discussed how to implement assignment operator okay so I should say by suitably defining member function operator equal to and we define two member functions operator equal to one which takes as argument const char star and another which takes as argument another string object. In the next segment we will be talking about the implementation of the other operators but before that we will take a quick break.