#12-Static data member and Static Member Function-Programs with Explanation

Static data members of a class

The data members of a class can be made static by adding keyword static infront of data members.

It is just like a global variable of a class.

i.e only single copy of static data member is stored in a memory and all the objects of class commonly share the static data member of a class.

static data member, static member function

Now, let us discuss about the characteristics of the static data member.

  • Only single copy of static data member is stored in a memory. While multiple copies of non static data members are available for different objects of a class
  • Static data member of a class are declared within a class and is defined outside the class definition. It is defined outside the class definition by omitting the keyword static. Remember this point that the static data member of class are defined outside of a class without using the keyword static.

Now, in our exams, questions may come like this.

Why don’t you use an object to call the static member function, explain with example.

What do you understand by static data member and member functions? Explain their use in the program?

So, let’s start programming.

We will first solve a problem related to static data member.

The question is as follows.

  1. Q) Write a program that will have a class with members name, roll, directory, file name and other functions as required. All of the objects of the class will share 2 members director and filename where the information of the student is stored.

Its output will come like this. static data member, static member function

So, let’s discuss about the working of this program.

It all starts with the int main function.

Inside that function, we can see that, initially, at first s1 and s2 are made as objects of class student.

Now, an input is taken with s1.input() and similarly with s2.

Inside the input function, we can see that, the user enters his name and roll number for s1 and s2 objects.

Then, display is called as s1.display and similar process is done for s2.

Inside the display function, we can see that the user will get the output of website, language, name and roll.

Here’s the important part.

Think .

I will tell you about this again.

Inside the display function, we showed directory and filename. How the hell did we manage to do it? How, can I even display the thing that I didn’t even take as input from the user?

So, its answer is static data member.

As you can see we have made website and language as static in the class members declaration.

c++ tutorials, c++ tutorials with code explanation

So, we have completed the first tutorial about the static data members of a class. Now, let us head towards the static member function of a class.

Static member function

A C++ function can be made static by adding a keyword static to it.

Non-static member functions of a class can have access upon static as well as non static data members of a class. For remembering, NS=S+NS i.e non static=static+non static.

Meanwhile, the static member function of a class can have access only upon the static data member of a class.

Static member function of a class can be called static either by the help of object or without the help of object as like.

Syntax-:

Class_name::static_function_name

(Q)Write a program to show the concept of the static member function. 

Its output will come like this.

static data member, static member functions, oop

So, let’s discuss the working of the above program.

Initially, everything will start from the int main function.

As seen, we have made two objects.

Then, x1 and x2 invoked get_data function by passing inputs directly.

Then, inside the get_data() function, we see that he integer value is being passed. Then, the id was initialized to be i, whereas the count was added and updated.

So, after that what we did was to invoke the static member function. As we had seen in the previous syntax that the static member function will be called through the int main function as follows.

Class_name::static_function_name

So, here also we have done x::show_count. In that way, the show count function displayed count as the value of count.

We were able to display the value of count because we have initialized count as the static data member as-:

static int count.

After that, we can see that, x1 and x2 objects invoked the show_count function. And it will display the count which is static member inside the static member function. After that, we can see that we made an object x3 of class x. with x3, we again did the same thing as we did for x1,x2. Ie to take the input from the user and display count.

Finally, we displayed the required outputs for x1,x2 and x3.

Hence in this way, the programming of static member function was done.

The most important point to take out from this program are as follows-:

  1. To make a definition to the static function, we do void x::show_count().
  2. Non static member function of a class can have access to the static as well as non static data member of a class. here, non static member function is get_data and display, which are able to show the both data members, i.e id and count. But, in the same program above, we have defined show_count as a static member function. So, from show_count, we can only show the static member output. And we are doing accordingly. Because we have shown count, which is a static data member through the static member function. So, remember

NS fxn=(S+NS)data members

c++ tutorials on static member function, static data member

Be the first to comment

Leave a Reply

Your email address will not be published.


*