 So, this is kind of interesting because this will probably you know we have been using C out and C in all throughout right. So, after this lecture hopefully you will be able to see what this really is doing I mean although we will not go explicitly into what C out is doing, but you will see that this is just a special case of what we are going to study in this lecture. So, to motivate this you know think about the class V 3 that we have studied and we had declared this member function, we had defined this member function called sum which could take another object of class V 3 by reference here and it could add the x y and z components and return you an object of class V 3 right. And we had used this in the motion simulator program to do something like ut plus half 80 squared right. But if you look at this, this I mean yes this is indeed doing ut plus half 80 squared, but it is really very clumsy way of doing that you know invoking putting dot member function name dot member function name all over. So, it would be really nice if I could do something like this. I could just write velocity times t plus half of acceleration times t squared right. But of course, you know the problem here is the plus that we usually know of is plus on integers or floats or doubles that really cannot. So, I am talking of a copy constructor right. So, suppose I have two objects of class my string call them S 1 and S 2, two objects of class my string of call them S 1 and S 2 and S 1. So, let us say S 1 has two data members C array and length C array is a character pointer. So, let us say C array is actually pointing to an array of characters which says maybe hello or something right. So, my string S 1 and S 1 let us say in this part of the code I have made S 1 look like this and so length is whatever 1 2 3 4 5 length is 5 right. And now I say somewhere later that my string S 2 is S 1. So, now I am trying to declare a new object S 2. So, that new object S 2 will also have C array and it will also have length right. So, I am declaring a new object which means this will be allocated and I am also initializing it along with the declaration. So, I am telling you what to initialize it with. Please initialize it with whatever this object was. So, what will the default constructor do? The default constructor will simply copy the values in these members to these members. So, length will become 5 C array is a character pointer pointing here. So, this will also now start pointing there. This is what the default constructor will do. What is our constructor going to do? Our constructor will first allocate a new character array. So, it will allocate a new character array of size whatever length plus 1 of size length of size 6 and it will set C array to that. So, it will set the member C array of the receiver object right here the receiver object is this. This object has been created just now. I am trying to call the copy constructor on this. So, it will set C array of S 2 to point to the newly allocated array. The length has already been initialized to 5 through the initialization list. And then if C array is not null for i is 0 i less than length it will copy C array 0 is source dot C array 0. So, it will copy this whole thing over here right I do not know maybe the length should be 6 here or maybe it should be copied up to the plus 1. Yeah. So, I think it should have been i less than or equal to length. So, it should copy it allocates of size length plus 1 and then it copies everything including the backslash 0. So, is it clear what the difference is? Clear? Clear to everybody? Ok. So, here we want to write this code using just plus and star, but of course, we cannot do that because plus and star as we know work only on integers or floats or doubles. So, what we have to do in order to use plus and star to do vector addition, scalar multiplication all of that is we have to overload their meaning. So, that plus can now also operate on objects of class B 3 and C plus plus actually provides a way of doing this. So, here is how you do it. So, plus is a special case of an operator. So, in general let us say I want to. So, this act symbol is kind of like a place holder for an operator you could use plus minus less than whatever you want. So, if I want a particular operator let us say plus to act on objects of let us say class V 3. So, this is not explicitly done by the C plus plus language plus only operates on integers or floats or doubles in the C plus plus language, but if I want to define plus. So, in place of act suppose I write plus. So, if I want to write if I want to have plus operate on objects of you know the non-primitive types then I can do that by defining a member function called operator plus in general if it is whatever that operator symbol is you can put that here. So, this operator is a C plus plus keyword and if you put plus at the end of it you are actually defining the operator plus to now act on to do things beyond what it does normally on primitive data types. So, if you recall at the very beginning of the course we said that when you write names of functions in C plus plus you cannot use plus minus all of these. You can use underscore letters, numbers and all of that, but here is an example of a member function where I can use plus, but just before that you have to have operator. You cannot say my operator plus that it would not accept. Operator is a keyword. So, operator plus is indeed the name of a member function, but it is used to redefine or overload the meaning of the of the plus operator acting on something else beyond the primitive data types. Is this clear? So, you know when in our program we write something like C out less than less than something. So, C out is actually an object of some class and less than less than is an operator defined on that class. So, this is really saying this is like doing a plus b 6 C out less than less than something and we will see you know what it does, but is this clear? So, if I want to define x plus y I should define a member function operator plus which should be in the receiver object x and it should take y as a parameter. So, this order that first x then at then y should also be the same as the order here first x then at then y except that this at is really a member function called operator at being invoked on the receiver object x with the parameter y. Is this clear? So, if I define a member function called operator plus for the class v 3 and in that definition. So, if I do something like this the member function operator plus or in the class v 3 and this member function takes as parameter another object of class v 3 then it is like saying I have defined operator plus for the class v 3 and it can take another object of class v 3 as parameter then I can write x plus y. So, essentially what is going to happen is whenever you write x plus y the compiler will first translated into x dot operator plus y and operator plus is a member function defined in the class v 3 which is the class of the receiver object and then it knows exactly what to do it will do whatever you specify over here and similarly for operator star. So, in fact if you see these are exactly the sum operator that we had the sum member function that we had defined earlier I have just changed the name sum to operator plus because I wanted to do exactly what sum was doing, but I want to use the plus thing now and similarly this is the exactly the same as the scale operator and of course you know when I am doing x plus y I do not want that operation to change the value of y. So, I do not want the argument here to be changed. So, I have used const there and I do not want the receiver object also to change right when I say x plus y and it translates to x dot operator plus y I want to actually get a new object which is the vector sum of x and y I do not want either y to be changed or x to be changed. So, how do I specify that y should not be changed the argument should not be changed I specify by putting const here in the argument. How do I specify that the receiver object x also should not be changed by specifying const here immediately before the function definition starts the opening brace and now I can write something like this because this effectively now gets translated to well dot operator star with parameter t and now I know what to do operator star with parameter t we know what to do right and similarly this is acceleration dot operator star t star t. So, this star is of course this t star t this star is the usual multiplication on double objects right whereas this is the operator star this accelerator star t star t is the operator star which is a member function of the class v3 right. But now if you think about it and of course this plus will be whatever vector is being written by this call operator plus on that with this side as the argument and so on. But now if you think about this here there is a problem right because 0.5 star an object of class v3 should ideally be represented as 0.5 dot operator star and then the object of class v3 right that is what we have been saying x star y should be x dot operator star y. So, 0.5 star something should be 0.5 dot operator star something, but 0.5 is just a double constant. So, it does not make sense to say 0.5 dot operator star. So, in fact if you run the code like this a compiler will complain saying it does not know what to do with that star of course you could put acceleration star t star t star 0.5 that would be fine right because then acceleration star t star t would return an object of class v3 and then you can call operator star on that with 0.5 as parameter. But if you want to put it here then you cannot use that member function, but what you can use is what are called non-member functions which also overload operator. So, you can define operator star just like any other function in your program it is not a member of any class it is not a member function of any class. So, this is the function operator star which takes two arguments the first argument is a double the second argument is an object of class v3 and then it returns you an object of class v3 right. So, this order of arguments is important because I wanted to do 0.5 star something. So, the first thing was double the second thing was object of class v3. So, the first parameter here should be double the second parameter should be an object of class v3 and then I can do whatever inside and here as you can see I have sort of cheated inside and inside I am just doing so basically I was trying to implement factor star b. So, I define this operator which takes first argument factor second argument b and then I am returning b star factor. But when I do b star factor b is an object of class v3 in which there is a member function operator star. So, this is going to get evaluated as b dot operator star factor right and of course, I was able to do here because it commutes when I a scalar times a vector is the same as the vector times the scalar. But if your operation is non commutative you have to do something else right. So, now we can write this code and then these two stars are really the member function operator star in the class v3 whereas, this star is the non member function operator star not belonging to any class. But of course, internally it is invoking the member function operator star of the class v3. But that is just for this specific. So, now what kind of operators can be overloaded pretty much all that you can think of and here you see the less than less than and greater than greater than or also there right. So, c out really is an object of a particular class and in that class less than less than is an overloaded operator. And so, when you write c out less than less than something less than less than something you know whenever we talk about operators we also have to talk about their precedence and associativity and all of that. And we had seen precedence associativity of these operators which you can of course override using parenthesis and all of this. So, when you overload an operator its precedence associativity nothing changes it is just the way that operator is going to be evaluated changes nothing else right. So, this less than less than operator actually left to right associative. So, when I say c out less than less than i less than less than j. So, c out less than less than i is first evaluated it basically calls operator less than less than which is a member function of the class of which c out is an object using the next parameter. And then what that member function returns is c out itself and it has a side effect printing out the value of i on the screen. So, it prints the value of i on the screen as a side effect, but returns. So, essentially what I am saying is if I do it like this this is left to right associative. So, first this gets evaluated which means c out dot operator less than less than with parameter i this function gets called that function has a side effect that it prints the value of i on the screen and that function returns c out itself. So, now when you are trying to evaluate this you first evaluate this it returns c out as a side effect it prints i on the screen. But since it returns c out now again c out less than less than j has to be evaluated again you call operator less than less than on the receiver object c out passing j as parameter as a side effect it prints j on the screen and it again returns c out. So, that is how you can put this chain of less than less than and actually get away it is just an overloaded operator of the i o stream class which of which c out is an object right. And similarly for c in I mean it is exactly the same thing the operator is defined in the other way. And then as a special interesting example you can also redefine I mean remember we had talked about assignment as an operator right this is pre mid-sem we had talked about assignment. For example, we could write this expression x equals y and z assign that this is a perfectly legitimate c plus plus statement what it does is this is an assignment expression this is the assignment operator it evaluates to the value of the right hand side which is y as a side effect it copies the value of y to x. And then this is another assignment expression this is an assignment statement in which this is copied to z but it also returns a value which is the right hand side whatever it evaluated to but as a side effect it copies the value of the right hand side to z right. So here the value of y is copied to x and it is returned from this expression that value is copied to z and it is again returned from the assignment expression. So the equal to sign is actually used as an assignment operator to build assignment expressions we have already seen this earlier. And so if you could overload operators cannot we overload the equals also and it turns out that you can so that is called the assignment overloading and it is just like what you would expect. So if I say LHS is assigned RHS just like what we have seen so far like plus star this actually calls LHS operator equals RHS ok it will call a member function called operator equals and whatever that member function does that will happen. So one might ask what is the difference of this overloaded assigned operator operator equals and the copy constructor the copy constructor was also used to copy one object to another object right. And if you recall we said that there are three instances where copy constructors are invoked initialization and declaration passing values to function parameter passing by value and returning of a value from a function. Those are the places where you wanted to create an object and immediately initialize it with something. And assignment is not one of those in fact none of those are assignments and assignment happens when you have something like this this is an assignment statement. So the overloaded assignment operator is going to get invoked here right. The copy constructor gets invoked there. There are two completely different member functions ok. The default copy constructor copies the value of all data members on the right hand side to the data members on the left hand side. The default assignment operator copies data members from the right hand side to the data members of the left hand side. So the default are the same but they are called in two completely different contexts. So there is no confusion about when the operator equals will be called and when the copy constructor is going to be called is that clear right. So and of course you know whenever you have an assignment expression it must return a value because an assignment is an operator which creates an expression. A copy constructor does not return a value or constructors do not even have return types right. A copy constructor never returns a value. It creates an object and immediately initialize it with something. But an assignment operator will be used to build assignment expressions and they must have values. So that is the other difference that the operator equal must return a value like all assignment expressions. And what kind of value should it return? It should return a value of the same type as the two arguments right. Typically it returns the value of the right hand side. But inside your overloaded equals operator you can make it return something else also. So for example here is a simple example where we have the operator equals for a particular class which has some private data members. And then you know you are doing something here. So you are accessing the data members of the argument that is passed. So this is just like a copy constructor right. I mean as I said LHS assigned RHS will be LHS dot operator equals RHS. So I must take as argument an object of the same class and this member function will be invoked on an object of the same class. So this member function is going to be invoked on an object of class. It takes as argument another object of class Q passed by reference. And so this part looks just like a copy constructor. But of course it's called in a completely different context. So here I am just copying the data members of the parameter that is passed to the corresponding data members of the of the receiver object. And then in a loop I am copying something over here. And finally I am returning something. So this is a crucial difference from copy constructor. Copy constructor doesn't return anything. It doesn't even have a return type. No constructors have return types right. But an assignment operator must return something because it's going to be used to build an expression. It's like saying I want to write x plus y. But I don't want this to evaluate to a value. That doesn't make sense. So this is also an operator which gives rise to an expression. And an expression must have a value a type. So this must also have a value and type. So this is the same as x dot operator equal to y. Then this function when invoked must return a value of the same type as the type of x. So that is what we are doing here. We are returning star this. What is this? This I mean this thing called this is actually a pointer to the receiver object. So in any member function, so this is a reserved keyword in C plus plus. And in the definition of any class in any member function, whenever you use this, it's actually a pointer to the receiver object. Any member function is always invoked on a receiver object. So this is a pointer to the receiver object. So here what am I doing? I'm returning star this, which means I'm returning the receiver object itself, which means if I do something like this, I'm returning the object x itself, a reference to the object x itself. This is the receiver object. And if operator equal to is going to return star this, then I'm returning star of a pointer to the receiver object, which means a reference to the receiver object. So that's the difference between a copy constructor and an overloaded assignment operator. So up to here is a portion for your ensign up to what we just thought learned now. So thank you for attending the course.