 Welcome back, in the previous segment we talked about constructors, in this segment we are going to talk about operator overloading. In mathematics arithmetic operators are used with numbers, but they are also used with other objects such as vectors and the same operators in some sense mean different things in different places, but they are somehow related and therefore we somehow prefer to use the same symbol. Now something like this can also be done in C++ and it is desirable to the extent that it helps your thinking. So if the program that you write is consistent with the way you think about the objects in your program then that is a good thing and C++ allows you to do that. How does C++ do it? Well, consider an operator at, so at could be plus, at could be multiply anything. So the operation associated is x at y, so at appears in the middle and so at is an infix operator. So how does C++ look at such an operation? Well, C++ treats this as x dot operator at with argument y. So operator at must be a member function in the structure type x, structure type of x. If it is a member function then that member function is executed. So if the member function operator at is defined then that is called to execute x at y, it is as simple as that. So you can define for example operator plus and that code will be execute if you write x plus y. So we will see an example of this right away. So say we want to do arithmetic on v3 objects. So this is our main. So we have v3 objects p and q and they have been called with these constructors. So we will see those constructors as well in a minute. But let us say we have initialized those objects p and q somehow or the other. Then we have a couple of uninitialized v3 objects. And now we can write r equals p plus q and the natural interpretation of this might be that look we add the vectors component wise. And we can make that natural interpretation hold in our implementation and that is exactly what we are going to see. So notice that as we said earlier this just means r equals p dot operator plus of q. So we are going to define the operator plus member function. Then we can write something like s equals r times 10. So far v3 for v3 star does not have any particular meaning r is of type v3 and there is nothing so far we have said as to what it means to multiply a vector by a scalar. But we can simply define r dot operator star. This expression would be effectively implemented automatically by c plus plus it would be interpreted as this operation and c plus plus would just perform this operation. So how does this whole thing work? So struct v3 if you remember has these three parts x, y, z, the three members x, y, z and let me just get this constructor out of the way. So this is the constructor taking three arguments and setting x, y, z to those three arguments. So this is kind of the most natural constructor you might want. I want to set my vector, I want to initialize my vector to some arbitrary three quantities. Now comes another constructor because we want to write this as well. So for this, this constructor will not work. So this constructor is going to do nothing. So the members x, y, z will not be initialized in any particular manner. Next we are going to write the member function operator plus. So the definition of this member function is like the definition of any other member function. So this v3 is the type of the value being returned and this is the argument. And this is effectively going to be adding two vectors. So how are those two vectors going to come? Well one is going to be the receiver. So we write, we wrote p plus q that was translated implicitly by c plus plus to this. So p is going to be the receiver and that is going to be added to q. So in this the receiver is always there and the second, the right hand side operand of the plus is going to be this b. So for this expression which got translated to this p is going to be the receiver, q is going to be the right hand side operand or the b value over here. So what happens? So in this what am I supposed to do? I am supposed to return as v3 object having x coordinate equal to the sum of the x coordinates of the receiver and the right hand object. So x plus b dot x is exactly that, y plus b dot y is the desired y coordinate and z plus b dot z is the desired z coordinate. So we are calling a constructor over here and that constructor is constructing a v3 object with these members. So these are exactly the members that we wanted for component wise addition or member wise addition and that is exactly what this member function has accomplished. What about this? So we should write a member function r dot operator star and this would be the factor argument. So it is going to look something like this. So v3 will take a, it appears to take a single argument is operator star but that is not true there is always the receiver and the receiver is, since this is inside a struct v3 the receiver is of type v3. So this is an implementation of v3 times number. So f is that number and what is going to happen over here you should be able to write this now. We are going to return an object whose numbers are x dot f, y dot f and z dot f and we want to return a v3 object so we just call the v3 constructor with these arguments. So that is it. So this allows us to do arithmetic on v3 objects. Now using this v3 arithmetic we can do something which looks quite nice. So suppose we have v3 objects u, a and s remember our ut plus half 80 squared example and let us say t is a double value. Now our formula was ut plus half 80 squared. So we have essentially written that formula but you can see that that formula is going to do what we want. Why is that? So u times t we have defined, u times t is going to be u dot operator star of t. So we have defined how this is going to happen. This is going to multiply every member, every component of u by t. So it is going to do exactly the right thing. What about this a times t that is again scaling. So this if it is calculated first will give me a vector which has been acceleration which has been scaled by t or acceleration which is multiplied by t. But again when I do this it will be again a v3 now the product multiplied by t, again a v3 multiplied by 0.5. So this is going to produce exactly the 80 squared half 80 squared term that we want. So now this is going to be a v3 object, this is going to be a v3 object and there is a plus over here. But we define the plus operator as well and therefore this is going to produce the final sum that we wanted. And if we put s equals this then we can get s dot x, s dot y, s dot z which is going to be half 80 squared for t equals, t equals 10 and these values of u, u and a. So let us do a demo of this. So this is our file kinematics dot cpp. So inside this we have put in whatever we wanted. So this constructor and I guess I have ignored putting the simple constructor over here but I guess it is not needed over here but we could have put that in as well. So this is the definition of operator plus, so the plus operator, this is the definition of times operator and you can see that I have exactly the code that we want. So let us look at this and let us calculate what answers are we expecting. So t is 10, so ut we should expect in the x coordinate 10 over here and then half 80 squared. So half of a in the x coordinate is going to be 2 and t squared is 100. So this is going to contribute 200 plus 10, so 210. So similarly you can do the other things but let us just check whether this will indeed produce s dot x to be 200, so 210. So let us execute this and run it, yes 210 it is the x coordinate. So here are some exercises. So overload the multiplication to define the dot product of two vectors. As you might remember this is also something that is needed in physics a lot. Now we already have overloaded the multiplication but that was a multiplication between a V3 object and a number. This is going to be a V3 object and another V3 object and that is perfectly fine that can sit next to our old definition because the signatures are different. And so when you write it in your code C++ will know exactly which of those functions to use because it will look at the signatures and it will look at how many arguments, what kinds of arguments you have specified. So what have we discussed? So we have said that expressions involving vectors can be made to look very much like what you studied in physics, the exact formulae you can almost see in your code. Other operators can also be overloaded including unary operators and this is discussed at length in the book. Although for this course we will not really worry so much about all such detailed overloading. So if you understand plus and times that is quite enough for this course and maybe a few other operators which we will specifically point out a little bit later. Now overloading operators seems very cute but it is also a little bit strange. So in principle you could take the plus operator and make it do multiplication. So that could be amusing but in practice it is a terrible idea because you are fooling people and that is the last thing that you want to do when you are trying to cooperatively solve a problem. So you have to be careful with this kind of overloading. You should use overloading when the operators are really consistent with the intuition that you have or intuition that in general people have otherwise you will just end up confusing the reader of your program and believe me you will also confuse yourself. But if the intuition is consistent then you should overload because one of the ideas in modern programming is that your programs should look like the language should be like the language you use in dealing with that domain. So if while doing the physics you think of multiplying vectors by scalars then if you can make that happen in programming it will also be a good thing. It will sort of it will increase the readability of your code and generally make things more appealing. So I just want to point out that we saw three ways of writing s equal to ut plus half it square using ordinary functions for some end product using member functions and operator overloading and I guess you probably agree with me that operator overloaded looked very very natural. The next topic is going to be access control and classes but before that let us take a short break.