 Hello and welcome back. We are continuing our lectures on the use of member functions in object oriented programming. Here is a quick recap of some of the relevant topics we have seen earlier. We have studied about structures which can be used in C++ programs to represent objects. Basically, structures are groups of related variables arrays or even other structures. And we have also seen that structures can have member functions and these serve as interfaces for interaction with this various objects. We have also seen how to access members, both data members and member functions of structures. We have studied about pointers to structures and also about dynamic allocation and deallocation of structures. In this lecture, we are going to put all of this knowledge together to write an interesting program using member functions. And in fact, we are going to see two different object oriented implementations of the same program. And we will also try to understand what the differences between the two implementations are. Some examples in this lecture are from the book and introduction to programming through C++ by Abhiram G. Ranade published by McGraw-Hell Education in 2014. All such examples will be indicated in slides with the citation AGR book. Now recall that in the last lecture, we talked about an example from AGR book in which we wanted to reason about motion in three-dimensional space. And so we had to deal with three-dimensional vectors which could represent position, velocity or acceleration. And therefore, three-dimensional vectors were chosen as the basic entities or objects in our programming endeavor. And therefore, we went ahead and defined a C++ structure to represent a vector and we used Cartesian coordinates for representing a vector. So, if you recall from the previous lecture, this is how our three-dimensional vector V3 structure looked like. It had three member data fields x, y and z each of type double. It had three member functions length, sum and scale for computing the Euclidean length of the vector for computing the vector sum of a given vector with another vector and for scaling a given vector by a constant scalar factor. So, we have studied these member functions in the previous lecture. In this lecture, we are going to use the same V3 structure that we have already studied earlier and try to solve a simple problem. So, here is an outline of the simple problem. It is a motion simulator and this is inspired by some of the examples and discussions in AGR book. So, in the simple motion simulator, we are going to be given the initial position and velocity of a solid body as three-dimensional vectors. Both the position and the velocity will be three-dimensional vectors. We are also going to be given the acceleration of the body as a three-dimensional vector and we will assume for the purpose of simplicity that this acceleration does not change with time. We are going to be given the granularity of simulation time called delta and we are also going to be given the total amount of time that we need to elapse, call it capital T. What we are required to compute is the positions as three-dimensional vectors every delta units of time for the entire elapsed time of capital T. Now, in order to do this, what we will do first is we will augment the V3 structure that we had already seen earlier by adding a member function which prints out the x, y and z coordinates of a V3 object. Now, why are we doing this? By adding this member function called print which simply prints out the x, y and z values of an object of type V3. We are basically saying that we do not need to access the members x, y and z of an object of type V3 directly in order to print their values. We can simply use this member function which acts as an interface to allow the programmer to print the values of the members x, y and z of an object of type V3. So, you can see that the use of such a member function actually acts as an interface to access the values of different members of an object and to print them out without actually directly accessing those members from the program. So, we have added the simple print function over here. This is a member function of the structure V3. Now, here is a simple outline of a possible motion simulator program. We have three-dimensional vectors velocity acceleration position which are going to denote the initial velocity, the acceleration and the initial position. Each of these is a three-dimensional vector. So, each of these are variables of type V3. Current displacement and current position are also variables of type V3. Delta t which is the granularity of simulation time is a variable of type double. Total t which is the total amount of time for which we will have to do the simulation is also of type double. And t which will keep track of the time lapse so far in our simulation is initialized to 0.0 and it is of course of type double as well. So, what do we do first? We ask for the x, y and z components of the initial velocity, read them in, ask for the x, y and z components of acceleration, read them in, ask for the x, y and z components of the initial position and read them in. Then we ask for the total simulation time, read in total t and we ask for the simulation time granularity and we read in delta t. After this, we are going to do some input validation. So, we will check if the total time is less than 0 or if delta t is less than or equal to 0, then probably this is not going to be a very meaningful simulation. So, we will say invalid input and we will return minus 1. Note that main has to return an integer value and here we are returning minus 1 to denote that an unexpected termination of main has happened. So, after we have read in and validated the values, here is what the main simulation loop of our program would look like. We are going to calculate the current displacement using the simple equation from physics, which says that if the initial velocity is given by the vector well and the acceleration is given by the vector a c c and the acceleration is constant, then at the end of time t, the displacement is given by well times t, where this is scaling of the vector well by t and this is the vector sum and this is the scaling of the vector acceleration by 0.5 t square. So, that is exactly what we are going to do here. We are going to scale the vector well by t and then we are going to do a vector sum of that with the scaled version of the vector acceleration with 0.5 t square. That will be the current displacement and the current position is of course, the vector sum of current displacement and the initial position. So, we call the member function sum on the three dimensional vector current displacement and passes its parameter the initial position vector and that gives us current pause and finally, we print out the current position using the member function print of current pause at time t and increment t by delta t and then keep iterating around this simulation loop. Now, here is an alternative implementation that one could do for the same problem of designing a motion simulator. So, in this alternative implementation, we actually define a motion simulator object. So, we say struct motion simulator in that we define three variables, three members, init pause, init velocity and acceleration all of type v 3, current pause and current velocity are also of type v 3, delta t which is the granularity of simulation is of type double. So, these are all members of the structure motion simulator and then we have this member function called initialize simulator what it does is it simply copies init pause and init well to current pause and current well and returns and then we have the most important member function of this structure which is simulate a step and here what we are going to do is we are going to actually compute the updated position and updated velocity from the current position and current velocity by using very basic notions of physics that the updated position must be the vector sum of the current position and the current velocity as a vector scaled by delta t. So, this would give me the current velocity scaled by delta t would give me the change in the position and you add that as a vector to current position you get the updated position and similarly the updated velocity is the vector sum of current velocity and the change in the velocity which is given by delta t times acceleration. So, of course, you will realize that both of these are really going to work well when delta t is small and we can assume current velocity is almost constant throughout that interval and of course, acceleration we have already assumed to be constant. So, this expression is correctly going to compute the updated velocity for any values of delta t, but this expression is going to compute the correct value of updated position only for very small values of delta t such that we can assume current velocity to be constant within that small delta t and then we simply update current pause and current velocity in the expected way. We scale current velocity by delta t and add it as a vector sum to current pause and we scale acceleration by delta t and add it as a vector sum to current velocity and of course, finally we also have a print position member function in motion simulator where it simply invokes the print member function of current pause and returns. So, here is how the motion simulator program using this alternative design might look like. So, we define a variable we declare a variable m sim of type motion simulator and we have the total elapsed time so far delta t and total t like we had earlier this is the total time for which we have to do the simulation and then of course, we read in the x y and z components of initial velocity store them in the x y and z members of the init well member of m sim. Similarly, the x y and z components of acceleration are stored in the x y and z members of the acceleration member of m sim and the x y and z components of initial position are similarly stored in the x y and z components of the init pause member of m sim and we of course take the total simulation time and the simulation time granularity and this delta t is also copied to m sim dot delta t. So here is how the main program which implements the motion simulator might look like using this alternative design. We have declared the variable m sim of type motion simulator. We have these different time quantities and then after reading in and validating the values for the initial velocity, acceleration, initial position delta t and total t. We are going to call this member function initialize simulator of m sim which is an object of type motion simulator and recall that what initialize simulator does is it simply copies the initial position and the initial velocity of this object motion simulator to the members current position and current velocity of the same object motion simulator and then this simulation loop is here while the total amount of time will have so far is less than or equal to the total time for which we have to simulate. We are simply going to call this member function simulate a step of the object m sim. What does it do? It basically simulates one step of duration delta t and thereby updates the position and updates the velocity in the object of type m sim and finally we are going to print out the position calculated within the object of type m sim at time t increment t by delta t and continue this iteration. So what you see here is that in this design, this simulation loop looks far cleaner. I just said simulate a step and I said print position increment time and we were able to do this by basically defining this new object motion simulator which was basically doing all the required computations within it when I call simulator step. So an interesting question here is are the results computed by the two simulations the same? I would encourage all of you to try it out and see and if the results are not the same it is interesting to ask why are the results not the same and particularly I would ask you to try out with various values of simulation time granularity and to see for what values of simulation time granularity with the results computed by the two simulations agree by enlarge and this should give you an idea of why implementation choices are sometimes very important when solving a problem. In this particular case the simulation time granularity is large the second implementation we talked about will be inaccurate the first implementation we talked about will be accurate for any values of delta t. So in summary we saw a simple motion simulator program using object oriented programming we saw the use of member functions we also had a glimpse of data hiding in the second implementation there were several things like current velocity which the main program never gets to see. So we saw this glimpse of data hiding and the effect of simulation time granularity on the results of simulation. Thank you.