 Hello and welcome back! We are continuing our study of object-oriented programming and in this lecture we are going to study about friends and static members. Here is a quick recap of some of the relevant topics we have already studied. We have been discussing about object-oriented programming with structures and classes. We now know how to access data members and member functions. We have studied about constructors and destructors. We have seen how to make function calls with structures and classes and in the last lecture we also studied about operator overloading. In this lecture we are going to study about two additional features that are used in object-oriented programming and that C++ provides. These are called friend classes and functions and static data members and static member functions. Much of this lecture is motivated by the treatment in the book and introduction to programming through C++ by Abhiram G. Ranade published by McGraw-Hell Education in 2014. All examples taken from this book are indicated in the slides by the citation AGR book. Now recall that when we declared certain members of a class as private, we really intended to restrict access to these members to only the member functions of that class. This is how we have actually been achieving data encapsulation or hiding. However sometimes it may be desirable to bypass this access restriction for a few very specific non-member functions and we'll just see some examples of these but when we are encountered with such a situation what should we do? Should we make these specific non-member functions, member functions of the class? Well that's not very desirable because we really want to keep all that is coherent conceptually together in a class just because a particular non-member function needs to access some private members of a class does not make it eligible to become a member function of the class. Then should we make all members of the class public? Well that's certainly a bad idea since that would completely go against the principle of data encapsulation or hiding. C++ actually provides a very nice solution for this. It allows you to have a friend declaration through which a class can explicitly allow specific non-member functions to access its private members whereas all other non-member functions still do not have access to its private members. Let's see an example of this. So here I have a class point for representing points in two-dimensional space. The class has two data members x and y both of type double and these are declared private and there could of course be several member functions of the class and here I have a simple C++ function collinear which takes three objects of class point and determines whether they lie on the same straight line in the plane. Now note that collinear is not a member of the class point therefore within the function collinear I will not have access to the data members x and y of the objects p1, p2 and p3 past as parameter. However in trying to determine whether p1, p2 and p3 are collinear I really need to do this calculation. This is a standard expression that one obtains from coordinate geometry to figure out if three points lie on the same line in a plane and if this entire expression evaluates to zero then I know that the three points are collinear otherwise they are not. However how am I going to compute this expression? To compute this expression I need access to the data members x and y of all the three points and I don't have access to them because these are private members. So here is where the friend declaration comes in useful. I could declare in the definition of the class point that this function collinear which is going to return a Boolean and it's going to take three references to objects of the class point. So this is basically the declaration of the function and I could prepend that with the keyword friend to indicate to the class point that this function should be given access to its private data members. Now should I put this friend declaration in the public or private section of the class point? It does not matter. You could put it wherever you want. For example I could put it in the private section as well and it would achieve exactly the same effect. Now in general a particular function can be friend of several classes in which case it has access to the private members of all of those classes and similarly a class can have several friend functions and then each of these functions have access to the private members of this specific class. Now sometimes what might happen is that various members of the same class may need access to the private members of another class. So for example here I have the class point and here I have defined a new class called points in plane which basically keeps track of a bunch of points each being an object of the class point. How many points does it keep track of that is stored in this data member and the specific points are stored in this array of size at most hundred and then there are several public member functions of this class. One of these is what we have just seen. It takes three points and determines whether they're collinear. Here is another public member function takes three points and determines whether they form an equilateral triangle on the plane and there could be several other such member functions. Note that each of these member functions actually need access to the private data members x and y of the points that are passed as arguments. So one could go ahead and declare each of these member functions as friends of the class point or more conveniently one could simply declare this entire class points in plane as a friend class of the class point. Therefore all the members of the class points in plane will now have access to the private data members of the class point. Now let us look at what are called static data members. So here is the class point that we have been seeing. It has these two data members x and y. Here I have shown two constructors. One is a default constructor. One is a non-default constructor. And let us suppose that I'm interested in counting how many objects of class point are being created or allocated. So how would I do that? Pretty simple. I would keep an integer counter and whenever the constructor function gets called I would increment the integer counter. Well as you can see here this integer counter therefore must not be associated with any specific object of the class point but rather it should be shared across all the objects of the class point. Whenever a constructor function is called on any one of these objects I want the same integer counter to be incremented. So this is achieved in C++ by using the word static. This is a C++ keyword. So when I say static int count I'm basically declaring a static data member which in this case is public and the same single copy of the static data member is going to be shared across all objects of the class point. And inside the class definition for example inside this constructor functions when I want to refer to this shared static public data member I can directly use its name like count over here. However outside this class definition if I want to refer to that single shared integer counter then I have to use something like this. This double colon here is also called the scope resolution operator in C++. This basically tells me that I'm referring to the data member count of the class point. Note that the data member count is not associated with any particular object of the class point. This data member is associated with the class point itself and it is shared across all the objects of class point. So this is how I'm going to access that shared data member of the class point. I'm referring to the member count of the class point. And here I'm basically creating this shared data member of the class point and initializing it to 0. As I said this is not tied to the creation of any object of class point. This can get created even before the first object of class point gets created. Here is my main function. What do I do here? Here I've created three different objects of the class point so the appropriate constructor functions are going to be called. Now as these constructor functions are called each one of them will go and increment that same static data member count. As a result after all of these have been created I get the count of points created in the value of this count. And here I'm just reading out that value and printing it out. Now I'm able to access the static data member of the class point in the function main because it has been declared public over here and of course how do I access it? I'm going to access it through the scope resolution operator because this tells that I'm trying to access the static data member count of the class point. Once again this data member is not associated with any of these objects but it is shared by all the three objects. Now I could also declare static data members to be private. So here for example I have declared the same integer counter which is static but I've declared in the private section and similarly here I have actually created that static data member which in this case is private. So note that this creation happens outside of any function so it is okay to create the static private data member here. It is not that I'm trying to access that static private data member from any function here. This is outside the scope of any function including the main function. Now here I've also indicated a static member function. So here is a member function which is named reset count and what it does is it simply resets the value of that counter. I have made it static and I've put it in the public section of this class definition and we'll shortly see what that achieves and I've also introduced another member function but this is not a static member function. This is a non-static member function. This is also in the public section. What does it mean to say that it is non-static? It means that this member function is going to be associated with every object of the class point that is going to be created. What do I mean when I say that this is static? It means this member function is not associated with any individual object of the class point. This member function is rather shared across all the objects of class point. Now inside my main function I'm first going to call this static member function reset count. Recall that it is not associated with any particular object of class point. So this is how I'm going to access it. I'm going to say please invoke the member function reset count of the class point. This is the scope resolution operator. This is going to go and set the private static data member count to zero and then I have these three objects of class point being created. So when the constructor functions are called here they will all go and increment the value of that shared private data item called count and then finally I want to read out the count of points created but note that I can no longer use this statement that I had used earlier because the data member count is now private. So I cannot access that from the main function. So instead what I'm going to do is I'm going to call the non-static member function print count on any of these objects A or B or C. In this case I've shown it as being called on A. This non-static member function can access the private static data member count and it will print out the value of that count. So in summary in this lecture we studied about friend functions and friend classes and their usage. We also studied about static data members, static member functions and their usage in C++ programs. Thank you for your attention.