 Hello everyone, I am Mrs. Meenakshi Shrigandhi, Assistant Professor from Vultured Institute of Technology, Sholapur. Welcome to the video lecture on Static Data Members and Member Functions. Learning outcome, at the end of this session, students will be able to explain the properties of static data members and member functions. Let us see the memory allocation for objects. Usually, the memory space for objects is allocated when they are declared for a particular class. The memory functions are created and placed in memory space only once they are defined as a part of class specification. So, consider two member functions, member function one and member function two for a particular class. So, the memory is allocated when the member functions are defined. Since all the objects belonging to that class use the same member functions, no separate space is allocated for member function when the objects are created. That indicates that these member functions will be common to all the objects. Only the space for member variables is allocated separately for each object. Like here it is shown, for object one, for object two and for object three, the memory for the data member one and the data member two is allocated separately for object one, for object two and for object three. So, the memory is allocated when the objects are defined. Separate memory locations for the objects are essential because the memory variables will hold different data values for different objects. Let us see what are the static data members are. Static data members are the class members that are declared using the static keyword. There is only one copy of the static data member in the class even if there are many class objects. This is because all the objects share the static data member. The static data member is always initialized to zero when the first class object is created. Consider a class which contains one data member and one static data member. For that class, consider there are three objects, object one, object two and object three. And the memory for the data member will be created separately for all the objects. But for static data member, the memory is allocated only once and that will be shared by the three objects. Suppose the value which is held by the data member of object one is value one, the value for data member of object two is value two and the value of the data member of object three is suppose value three. At another instant when object one is trying to access the static data member, then the value of the static data member is now updated by object one. When object two is trying to access the static data member, the value is now updated by object two. Similarly, when object three is trying to access the static data member, then the value of that static data member is now updated by object three. Let us take an example using static data member. There is a class called as item. Before we focus the members of the class, let us see how the memory allocation for the objects is taken place. So let us focus on the main function where we have declared three objects that is A, B and C. So these are the three objects that is A, B and C. Now let us focus on the class now which contains one static data member called as integer count. So a memory is allocated for this static data member, which will be common for all the objects. In the class, there is one more data member called as number, but this data member will be created separately for all the objects. To the public visibility label, there is a member function called as getData, where it will initialize the data member number to A. And the static data member value is incremented by one. There is one more member function called as getCount, where it is trying to access the static data member and it is displaying the value of the static data member. Now here is the definition of the static data member, which consists of the data type integer, then the class name, scope resolution operator, and the name of the static data member. When the static data member has been defined, then the value of the static data member is now initialized to zero. Main function, by using object A, a getCount function has been invoked. So here, the object A is now trying to access the static data member, so now it will display the value of the count as zero. Similarly, when object B is invoking the getCount function, B will now access the static data member and it is displaying the value of the count, which is equal to zero. Similarly, C is invoking the getCount function, where the object C is accessing the static data member and it is trying to display the value of the count, which is equal to zero. Object A is now invoking the getData function, where 100 will be assigned to the number of object A and the count of the static data member has now been increased by one. After that, the object A will now invoke the getCount function, where it will display the value of the count, which is at present is equal to one. Similarly, object B will invoke the getData function, where it is assigning 200 to the data member number of object B. And after that, it is incrementing the value of the count to two. B is invoking the getCount function, where it will display the value of the count, which is equal to two. Now, C object is now invoking the getData function, by passing 300, the 300 value will be now assigned to the data member number of object C. And immediately after that, the value of the count has been incremented by one, so it becomes three. After that, the object C is now invoking the getCount function, where it will display the value of the count, which is at present is equal to three. So this example illustrates, how the static data members are used by different objects in a program. Try to think an answer. Can any other initialization, other than zero, is possible for static data member? Pause the video for some time, and note down the answer in your book. Any other initialization, other than zero, to static data member is possible? The answer to this is yes. While defining a static variable, some initial value can be assigned to the variable. So if we consider the previous example, following the definition gives the initial value of the static data member as seven. So your example is shown here, by considering the previous example, when the static data member has been defined, that time we have to initialize the static data member with some other value. So initialization other than zero to the static data member is possible. Let us see what are static member functions. The properties of static member functions are, a static function can have access to only other static members, let it be functions or variables, which are declared in the same class. A static member function can be called using the class name instead of its object, like in this way. That is the class name, scope resolution operator and the static member function name. Let us take an example using static member function. So here it contains a class called as test. Now again, before we see the members of the class, let us see how the objects for the class can be declared. So there are two objects that is T1 and T2. Now in the class, there is a data member called as code. Now the memory for this data member for different objects will be different. So code is created differently for both the objects. Now there is a static data member called as count, for which the memory will be common for all the objects. Now under public section, there is a member function called as setCode, where it will assign the increment of count to the data member code. There is one more member function called as showCode, where it will display the value of the code as the object number. Here it is the static member function, which is having a keyword called as static in the header of the function, where it is accessing the static data member only and it is displaying the value of the count. So the memory for the static member function will be again common for all the objects. Now here the static data member has been defined. After defining the value of the count will be initialized to zero. Now in the main function, first the object t1 will now invoke the setCode function, where the value of the count has been incremented by one and that is assigned to code. So here object t1 is now accessing the static data member count, where the value of the count has been incremented by one and that will be assigned to code of object t1. So code of object t1 value will be initialized to one. So similarly object t2 will invoke the setCode function, where object t2 is now trying to access the static data member and it is incrementing the value of the count by one and that value will be assigned to the code to the code of object t2. So for object t2 the value of the data member is now initialized to two. Now here is where the static member function has been called. This has been called by using the class name, scope resolution operator and the static member function name. So here when the static member function is invoked, we can see that it is accessing the static data member and it is displaying the value of the count. So here it will display the present value of the count, which is equal to two. So after this there is one more object created that is called as object t3. So a separate memory for the data member code has been created for it. Now t3 is now invoking the setCode function and it is incrementing the value of the count by one that is initialized to the code of object t3. After that the static member function has been invoked again where it will display the present value of the count that is equal to three. Now object t1 is invoking the showCode function where it is displaying the code value as the object number. So here the code of object t1 is one so that one will be displayed as object number. After that t2 is also invoking the showCode function. So here for object t2 the code value is two and it will be displayed as the object number. Similarly object t3 is now invoking the showCode function where the value of the code of object t3 will be displayed as the object number. So here it illustrates how the static data members are accessed by the static member functions. These are my references. Thank you.