 Hello and welcome back. In this lecture, we are going to continue our study of object oriented programming and specifically, we will look at the use of member functions in object oriented programming. Here is a quick recap of some of the topics that we have already studied. We have looked at a brief introduction to object oriented programming, where we basically considered the program as a collection of interacting objects. We have also seen that in C++, we can use structures to represent objects, whereas structure is basically a group of related variables arrays or even other structures. We have seen how to access members of structures, how to deal with pointers to structures and also how to dynamically allocate and deallocate structures. In this lecture, we are going to study another aspect of structures called member functions and we will see that these are really going to serve as interfaces of objects in object oriented programming. And of course, we will look at how to access member functions. Some examples in this lecture are from the book, An Introduction to Programming through C++ by Aviram G. Ranade published by McGraw Hill Education in 2014. All such examples will be indicated in slides with the citation AGR book. Recall from a few lectures back where we had seen a very quick overview of object oriented programming. We had basically seen there that when you are trying to write a program for a complex system, it is perhaps best to first identify entities which could be physical or conceptual that are involved in the working of the system. And these entities are also going to be called objects. And then we could think of the overall functionality of the system in terms of operations on these objects and interactions between these objects. And we had also seen that it is a good idea to actually not expose all the details but to hide away details that are not necessary for a particular operation. In addition, we had seen that when you are trying to design a complex system, it is best to implement the system modularily by focusing on the various entities involved in the working of the system, their interfaces and their interactions. Recall that we had also said that each entity or object is basically going to contain information specific to that object. And this information could be fixed information which usually does not change as the objects interact or it could be state information that changes as the objects interact. In any case, an object must have unambiguous well-defined boundaries. It must be very clear what part of what information is part of the information stored within an object and what is not part of the information stored within an object. Recall that we had also mentioned that ideally every interaction between two objects should happen through well-defined interfaces. And this is going to be the focus of the current lecture. Specifically, we will see how to define interfaces of objects so that these interfaces will allow us to interact with those objects. Now, the specific example that we are going to consider in this lecture is about vectors in three dimensions and this is from AGR book. So, in this example, we want to write a program to reason about motion in three-dimensional space. And so, we must deal with different kinds of three-dimensional vectors. For example, vectors representing position, velocity, acceleration and so on and so forth. So, three-dimensional vectors are going to be basic entities or objects in our program. So, we need to define a C plus plus structure to represent such a vector. And while there are several ways to represent three-dimensional vectors for simplicity, we will simply use Cartesian coordinates to represent a three-dimensional vector. So, here is how a structure for representing a three-dimensional vector also called V3 might look like. So, it will have three members named x, y, z. Obviously, they will store the values of the x, y and z coordinates of the vector and each of these three members are of type double. Now, what functions might we need to operate on objects of type V3? We might need to add two such vectors. We might need to scale such a vector by a scalar constant or we might even need to compute the Euclidean length of a vector. And there could be several more other functions we might want to use. But for the time being, let us just concentrate on these three functions. So, here is how a function that computes the vector sum of two vectors a and b might look like. Note that the two vectors are passed through reference and they are not going to be changed by the function. So, they are also passed using the keyword const. And here is a simple way of adding the x, y and z coordinates of the two input parameter vectors. And the result is stored in this vector V of type V3 which is finally returned. Similarly, if we want to scale, we are going to take a vector which is once again passed by reference here. And this vector is not going to be changed. So, we use the const keyword and we also take the scaling factor which is a double and it is also not going to be changed. So, it is used with the const keyword and then we simply scale each of the x, y and z coordinates of the vector a by the scaling factor and return the resulting vector. The function length once again takes a vector a by reference and it is not going to be changed. So, it is const and it simply calculates the square of the x coordinate, added to the square of the y coordinate, added to the square of the z coordinate and computes its square root. Now, we are going to assume that the square root function is available from a library. For example, CMath and if you include that library, you can access the function square root. Now, let us take a closer look at these three functions, some scale and length. What you notice is that each of these functions can be thought of as doing some computation on an object a of type v 3. So, if these three functions are basically doing some computation on the object a and returning some result, the natural question that comes to mind is why not associate these functions with the object a itself. So, for example, if I wanted to add a three dimensional vector b to the three dimensional vector a, I could invoke the function sum associated with the object a and pass the three dimensional vector b as a parameter to it. Similarly, if I wanted to scale up the three dimensional vector a, I could call the function scale associated with the object a and simply pass the scaling factor as a parameter. Similarly, if I were to find out the length of the three dimensional vector a, I could simply call the function length associated with the three dimensional vector a. So, what all of this is saying is that if we could associate functions with the object a, then these functions could actually serve as interfaces for interaction with the object a. If I want to do something with an object a, I could call the appropriate function associated with the object a, such that the body of that function actually does whatever operation I wanted to do on that function. Now, in C plus plus, there is actually a way to do this. Specifically, C plus plus allows you to have member functions just like member data values in structures. So, for example, here is a new version of the structure v3 that we had seen earlier. Of course, we have the three member data elements x, y and z, all of them being of type double and I could also have a member function. Here, the name of this function is length and all it does is it returns the square root of x squared plus y squared plus z squared. Now, which x is this referring to? Note that this is a member function of this structure. So, when it is referring to x here, it is actually referring to the member data of the same structure. So, as you can see here that this function can directly access the data members of this structure, compute the length of the vector and then simply return the length of the vector as a double quantity. Similarly, this is another member function which computes the sum of the current vector, the object of which this is a member function. It basically computes the sum of that object with another object of type v3 which is passed as a parameter to this function. So, therefore, what you see is within this body of the function when we say x plus b dot x, I am actually referring to the member x of the current object of which this is a member function and I am adding to it the member x of the parameter b and similarly for y and z. And similarly, this is a third member function which scales the members x, y and z of the object of which this is a member function by the parameter factor which is a parameter to this member function. So, if you take a closer look at this member function, let us say the sum member function. As I just said, you will note that this member function is actually accessing the member x of the parent object of which this is a member function and it is also accessing members of other objects passed to it as parameter. Now, how are we going to access member functions of structures in a program? Recall how we accessed member data values of structures. We used either the dot operator. For example, p is a variable of type v3 and we could say p dot x or if we are using pointers, for example, pointer p contains the address of p, we could say pointer p arrow x. Member functions can also be accessed in exactly the same way. So, for example, in this little program fragment, I have p and q as variables of type v3 and pointer q as a variable of type pointed to v3. So, I read in the values of p dot x, p dot y, p dot z. Note that I am accessing the members x, y, z of the variable p through the dot operator here and then I say q is assigned p dot scale point 5. Now, here I am actually accessing a member function and I am accessing it through the dot operator just like I had accessed a member data value through the dot operator. The parent object on which I am applying the dot operator is also called the receiver object. This is of course, the member function of the receiver object and this is the parameter of the member function. So, if you go back and look at the definition of the structure v3, within that you will find the definition of the member function scale and what you will realize is that when this function is called, basically this code is going to be executed. So, a variable of type v3 is going to be allocated and its member named x will be set to the value obtained by multiplying point 5, which is the value of the parameter factor with the value of x of the receiver object. So, basically what this will do is, it will take the value of x of the receiver object p. So, it will basically take p dot x, multiply it with factor in this particular case point 5 and it will set p dot x times point 5 to v dot x. Similarly, it will set p dot y times point 5 to v dot y and p dot z times point 5 to v dot z. This z that we are referring to here is the member z of the receiver object, which is p in this case and of course, if I am trying to access a member function through a pointer, then just like when we are trying to access member data values through pointers, we have to use the arrow operator. So, here pointer q is a pointer to the receiver object and this is a member function of the receiver object and so I can use the arrow operator to say access the member function length of the object pointed to by pointer q. So, in summary in this lecture, we saw how member functions can be used as interfaces of structures and we also saw how to access member functions of structures in a program. Thank you.