 Welcome back in the previous segment, we motivated the need for member functions. In this segment, we are going to talk about vectors from physics and then later on see how they can be implemented using member functions, the operations on the vectors can be implemented using member functions. Okay, so we have already talked about this, so say you are writing a program involving velocities and accelerations of particles which move in 3 dimensional space, then you would find it natural to represent the vectors using a structure with members x, y, z. So v3 is the structure type, so for 3 dimensional vectors and its members are x, y and z which are all doubles and of course other representations are also possible. So how do you use this struct v3? Well, there could be several operations that you might want to perform. So for example, you might take 2 vectors and add them up together. So a function, an ordinary function which does this would look something like this. So you have this function name and this function is going to return an object of type v3 or a structure of type v3 and it is going to take arguments which are also of type v3 and we are taking arguments by reference. So first it is going to create internally a v3 object called v and this is going to be our result. So what is the result going to look like? Well, its x component is going to be the sum of the x components of these 2 objects. Similarly the y, similarly the z. Then you could have a scaling operation, so which takes a vector and a scale factor and simply multiplies the scale factor with each of the members and so the resulting vector is going to be returned. As you remember this operation is going to return this vector that means the values will get copied in some suitable place in that calling program. Here is another possible operation that you may want with vectors which is a length operation. So the length operation simply takes a single vector and it returns the square root of the sum of the individual members. So these might be some operations that you might want to perform on your structure type and objects of your structure type v3. Well here is an example of use of vectors in which these operations can come in quite handy and this is so called motion under uniform acceleration. So we have a particle which has initial velocity u and uniform acceleration a. Then its displacement at time t, s is given as u times t plus at squared by 2 and this is applicable even if u, a and s are vectors and t is a scalar, it is a time. To find the total distance covered or the total displacement rather you must take the length of the vector s. So this might be whatever program looks like. So we have v3, we have 3 vectors of type v3, u, a and s, velocity acceleration and displacement and first we read values into u and a. Next we could calculate this for a single value of t but just for fun we will calculate it for multiple values of t. So t ranging from say 0 through 9. So what will this, how will we do it? So s is going to be some of these two quantities and what are these two quantities? Well they are u scaled by t and then a scaled by t squared upon 2. So what is going on here? a is a vector, scale of a and t squared by 2 is also a vector. That vector and scale of u times t are being added up and the result is this some, is this displacement s and this displacement is also a vector. So this looks like an ordinary assignment but really it is more complicated than that. When you look at an ordinary assignment you are tempted to think of it as sort of basic data types whereas over here three components are being assigned in parallel or three components are being passed to this function and this function is returning three components. So is this. So then we print out as a function of t the displacement that has happened and that is it. So that is the program. So as you can see we could have written this program by accessing each component of u, s and t but rather than that we chose to use these functions some scale and length and the reason for that is that they reflect better what operations we are actually performing. And furthermore once you or somebody writes those operations some scale and length and tests them and make sure that they are right then you can be more confident that your code is actually correct. So let us do a quick demo. So first we have the structure type, then the sum, then the scale, the length and then we have the main program. So it is really what we saw on the slides. So let us just compile it and run it. So now we are supposed to type in the velocities and the accelerations. So just so that we can see what is going on let me type the initial velocity to be 0 and let me type the acceleration to be say 1. So this is a plot of what happens to the particle as a function of time. So by making these things simple you should be able to check that these are in fact the correct answers. So what is going on over here is I guess you can think of it as a particle which is initially at rest is moving say maybe falling down due to gravity or something like that. Now we could ask can we do the same thing with member functions and this is how it looks like. So here we have a member function which calculates the length and that member function is being called over here this red text is the call of that member function. So length is a member function you can see it is inside the structure definition and a member function f of a structure type x should be invoked on a structure S of this type x by writing S dot f of arguments which is exactly what has happened over here. So v is a structure of type v3 and we are invoking this function on v by writing v dot length there are no arguments specified over here so no arguments are given here as well. Now this S or this v is called the receiver of the call so there is another analogy here as well that this part is sometimes even called a message being sent, a message is being sent to this object and that object responds. So that is another metaphor that is used but anyway we are going to call this thing before the dot as the receiver of that call. So in v dot length v is the receiver and it is a function call so like all function calls it executes by creating an activation frame and the reference to the members or the body of the definition of the function refer to the corresponding members of the receiver. So see this you have x dot x, y dot y, z dot z what are these x, y, z well they are these but as far as these calls are concerned they are members of the receiver. So this x dot x when this call is being made will actually refer to v dot x this will refer to v dot y this will refer to v dot z. So when v dot length executes x, y, z refer to v dot x, v dot y, v dot z so as a result of this v dot length is going to return 1 square plus 2 square plus 3 square plus 2 square again its square root or so 1 plus 4 plus 4 9 square root of that is 3. Now the receiver itself is also an argument I mean otherwise how would you know what this x means. So this receiver is an argument and it is kind of a hidden argument well it is not really hidden it is actually upfront it is really at the front so it is kind of a special argument but it is not there in the argument list in the parameter list. But it is an argument nevertheless and it is an argument which is passed by reference. So in this code if you change x then the x member of v is going to get changed. So it is so this v is being passed by reference that is why this happens if v was passed by value then if you change x that would not be that would not happen but indeed the receiver is passed by reference. So we can write the other functions as member functions and here is the complete definition. So this is our struct v3 the members then this is the first member function that we just saw. This is the sum operation written as a member function. So sum of v3 well v3 and what we need two arguments to make the sum. Now that sum the first argument is the receiver itself. So coming over here so we are doing ut dot sum of at by 2. So ut is 1 v3 which is being added to at by 2 which is another v3. So in this case this b is going to be at by 2 and ut is going to be the receiver for this. So now how does this function work? We are supposed to add up the two vectors and pass the resulting vector. So we are going to create that result over here v3 of v is going to be our result vector. Now the x the x member of this should be the sum of this ut is x and this at by 2 is x. So this is ut is x and this is at by 2 is x similarly y similarly z and then we return this vector. So that is how this sum works scale is similar for the scale. So we have u dot scale of t so u is the receiver and t is the argument. So t is this the value of t is placed in f. So again what are we calculating? We are supposed to calculate a result which is a v3 vector but which is a scaled version of the receiver. So to implement that this is the x member of the receiver we scale it up by f and put it in the x member of our result v3. And so we construct all v3 or the three vx, vy, vz and then we return v. How does it look like in this function? Well we wanted to calculate u times t so that is scaling. So we partial result we have put over here. Then we wanted to calculate at squared so that is a times scaling by t squared by 2. So a dot scale of t squared by 2 and then we have to add up these two things. So that is what is happening over here. Similarly we have to take the length of that sum and so that is happening over here. And I should say that we do not necessarily need to have so many variables if we do not want. What is happening over here is we are going to scale and then the result is going to be summed with the scaled version of this. We can take the length, we can apply the length member function to this entire result and we are going to, we will then get the length. So here we can think of this as sort of one long complicated expression instead of that we have broken up that expression into three separate expressions which are of course related. So what have we discussed? So we have discussed the syntax of member functions, we have said that a member function is physically placed inside the stuck definition and we said that a call to a member function looks like receiver dot function name followed by the arguments. The member function executes like an ordinary function with the receiver being an additional argument and the syntax makes the receiver look very different and indeed it is very different, it has a special relationship. It really, so there is a nominal special relationship in that the receiver is being definitely passed by reference but in addition to that somehow if I write say V dot something then I know that this is something which is happening and it has some special meaning in the context of V usually or it tells me that look this, the code for this function will be found in wherever V is itself defined. We also said that the receiver is implicitly passed by reference and so the call can actually modify the members in the receiver and this syntax is reasonable but you might ask well did we really need it, could we have used the usual function calls and tags. So that is not quite clear yet but it will become clear quite soon. So we will take a quick break over here.