 Hello and welcome back. In this lecture, we are going to continue our study of object-oriented programming. Specifically, we will look at access control of members and we will also see a very brief introduction to classes. Here is a quick recap of some of the relevant topics we have already studied. We have studied about structures as a way to represent objects in C++ programs. Structures are basically groups of related variables, arrays or even other structures. They have member functions which act as interfaces for interacting with them and we have also seen how to access both data members and member functions of structures. One thing that you might have noticed in our study of structures is that there were absolutely no restrictions on accessing members of a structure, whether data members or member functions from anywhere in a program. In this lecture, we are going to study about access control of members and structures. Specifically, we are going to study about private and public members and this will naturally lead to the definition of classes in C++ programs. Much of this lecture is motivated by the treatment in the book and introduction to programming through C++ by Aviram G. Ranade published by McGraw Hill Education in 2014. Recall that we had said that when we try to do object-oriented programming, we basically first try to identify entities or objects that are involved in the working of the system that we are trying to program. Then we think of the system functionality in terms of operations on and interactions between these objects and we have already studied that member functions are basically the interfaces for these operations. And something that we had seen but not studied in detail is this aspect of abstracting away or hiding details of objects not necessary to be exposed. So, this is also called data hiding or encapsulation and more generally it relates to controlling access to information and interfaces that are present in objects and this is going to be the focus of this lecture. Recall the three-dimensional vector structure that we had already studied in the last couple of lectures. We had basically defined a C++ structure called V3 which had three member data members named x, y and z to store the Cartesian coordinates of a three-dimensional vector. Each of these members was of type double and we also had three member functions named length, sum and scale for performing different kinds of operations on these three-dimensional vectors. Recall how we also accessed data members of the structure V3. For example, we had written a motion simulator program in the last lecture and in the main function of that program we had variables like velocity, acceleration and position all of type V3 and we actually read in the x, y and z components of velocity, acceleration and so on in this manner. So, here as you can see we directly read in the value of the member x of velocity of the member y of velocity and of the member z of velocity and similarly for acceleration. So, we had unrestricted access to the data members of the variable velocity of type structure V3. Similarly, in the motion simulator program in the main simulation loop we actually accessed several member functions of objects of type structure V3. For example, in this line of the code in the main simulation loop we were actually accessing the member function scale of the object velocity with the parameter t. Similarly, we were accessing member function scale of the object acceleration with parameter 0.5 t square. Here we are accessing the member function sum of the object current displacement with parameter cos. So, once again there were absolutely no restrictions on accessing the member functions we accessed them whenever we wanted at whichever point in the program. Now, it turns out that in C++ there are actually ways to access control for every member whether it is a data member or a member function of an object and the way we do this in C++ is using these three keywords private, public and protected. When we say that a member is private it means it can be accessed only from member functions of the same structure. When we say a member of a structure is public it means that member can be accessed from anywhere in the program and the protected keyword is actually used to denote access control of a particular type which is outside the scope of the current discussion. Now, as we will appreciate that declaring member functions as well as data members as private or public is very important if we want to hide some data or expose some data or even if we want to hide some interface or expose some interface to the rest of the program. Now, if you look at the structured v3 that we had studied earlier then all three data members of v3 as well as the three member functions of v3 could be accessed from anywhere in the program they were all public and in C++ all members of a structure are public by default. However, you could specify finer grain access control of different members of a structure. For example, here I have specified that x, y and z these three data members are private to structure v3 whereas length, sum and scale are public in structure v3. Note that once I write this keyword private followed by colon everything after that is going to be considered as private to that structure until I see a public colon and then everything after that will be considered public in the structure. It is not necessary to group all the privates together at one place and all the public together at another place. I could also declare public and private members like this here I have said private colon so x, y and z are private data members then I have written public colon so until I see the next private colon everything that I see in between are going to be public so here sum and scale are public member functions of v3 and once again this is private colon so length is a private member function of v3 in this definition of struct v3. Now that you know how we can declare some members as private and some members as public it is appropriate to take note that in C++ we also have a construct called class which is very similar to a structure except that all members of a class are private by default just like all members of a structure are public by default and in C++ programs you will more often encounter classes than structures in general. So here is the same three-dimensional vector structure now written as a class it has the same three data members which are private now and it has the same three member functions which are declared public. So if you compare this with the definition of struct v3 you will see that the definition is more or less exactly the same except that we have used the C++ keyword class here instead of struct. Now what happens when I declare a class and declare some data members as private I can no longer access those data members directly when I am reading in values or even when I am trying to access them for whatever other purpose. So if I write a code like this I will get a compiler error and the compiler will say I cannot access these private data members of the object velocity of class v3. So if the data members of v3 have been declared to be private how are we going to read or write these data members at all in a program. So one option could be the extreme option saying okay let's not make these private let's make all the data members public but this is certainly not preferred because this defeats the very purpose of data encapsulation it breaks the modularity of code by exposing internal details of class definitions to the program. For example remember that when we were representing three-dimensional vectors we had made a choice of using Cartesian coordinates and that is why we had data members like x, y and z in the class v3. Now suppose that after I have written my motion simulated program using class v3 in which I had data members x, y and z at a later point of time I decide to use cylindrical coordinates to represent three-dimensional vectors so of course in the class v3 instead of double x, y, z I have to now use the cylindrical coordinates rho, phi and z for a three-dimensional vector but not only that in the main function for the motion simulated program I can no longer directly read in velocity.x, velocity.y and velocity.z where velocity is an object of class v3. I now must read in velocity.rho, velocity.5 and velocity.z. So I now need to make a change in the main function because I chose to represent three-dimensional vectors in a different way and this is because I had exposed these data members of the class v3 to the main function by making them public and that is what allowed me to access these directly in main function and now I am in trouble when I have decided to change the representation of three-dimensional vectors. So if it is not a good idea to declare the data members of the class v3 as public is it the case that all data members of all classes should always be declared as private well not necessarily so you have to make a judicious choice and you should expose and allow access to only those members that other functions really need access to and you should hide and prevent access to other data members for example data members like bookkeeping data members or implementation specific data members like we just saw data members like x, y, z which are specific to the Cartesian representation of a three-dimensional vector and data members like rho, phi and z which are specific to the cylindrical coordinate system of representing three-dimensional vectors. Similarly you could have data members that are recording internal state as different objects interact and all of these data members should really be private and only member functions of the same class should require access to these at other parts of the program you should not require access to these and so these should be hidden and other parts of the program should not be allowed to access these data members. So in reality the choice of what you should declare as private and what you should declare as public is very important and this does affect the quality modularity and maintainability of code particularly if you're working in a large team where other team members are going to use the same class definition that you have created and this is relevant not only for data members but also for member functions and I would venture to say that understanding what is the best choice for declaring as private and for declaring as public is more of an art that you learn as you gain experience in programming. So well if the data members of the class v3 are going to be private then how do we read or write them? Well there is a preferred way of doing it which is through what are called accessor functions. These are functions that really return values of those data members that other functions are allowed to read that other functions have any business reading. So for example here is the class v3 I have declared xyz as private data members but I have declared getx gety and getz as public accessor member functions which can be used anywhere in the program to access the values of the private data members xyz. Similarly to write private data members of the structure v3 I can use mutator functions these are functions that can be used to update the values of data members but only those data members that other functions have any business updating. So for example here in the class v3 xyz are again private and here I have a public mutator member function named setxyz which takes three double parameters and simply copies those three parameters to the private data members xyz and set. There is also another advantage of accessing private data members through accessor and mutator functions. In the class v3 once I have changed my representation of a three-dimensional vector from the Cartesian system to the cylindrical system because my data members are private I know that nowhere in the main program or nowhere in any other function I could have accessed these directly. So there is no need to change those places in the main program or in any other function and in the main program or in any other function if I had accessed the x, y and z coordinate of a three-dimensional vector using these accessor functions I can now simply change the definitions of these accessor functions using known transformations between cylindrical coordinate system and the Cartesian coordinates. Similarly this mutator function setxyz now takes three double quantities and it sets rho, phi and z once again according to known transformations between cylindrical and Cartesian coordinate system. Of course here I have written vy divided by vx if vx is zero some special cases need to be handled here that is not shown here just for the sake of simplicity. So in summary in this lecture we saw how to control access to members and structures through the public and private keywords and we also saw a brief introduction to C++ classes. Thank you for your attention.