Static Variables and Static Functions in C++ with Examples

Static variables in C++:-

These are the type of data members which are declared with the static keyword in the class. They are basically the part of a class instead of a part of the object. They are created in a memory when their class is created. The major difference between static and normal variables is that, the static data members are shared among all objects of a class, regardless of the number of objects because all the objects have an authority to access, modify and changed it, therefore only one copy of these variables are created in a memory, on the other hand every object of a class has its own copy of normal data members in a memory. Static variables are commonly used when there is no need to define different fields for different objects of a same class.

For Example:-


#include<iostream.h>
#include<conio.h>

class game
{
static int presentplayers;

public:

game()
{
presentplayers++;
}
static void reset()
{
presentplayers=0;
}

static void inc()
{
presentplayers++;
}
static void show()
{
cout<<presentplayers<<endl;
}

};

main()
{
game.reset()
game player1;
player1.show();
game player2;
player2.show();

getch();
}

Explanation:-

Just consider a theoretical example, that is if we created a class game, then we created different objects of this class that player1, player2, and so on, and we wanted to count the number of players that are basically objects of a class game therefore we will define a static data member to calculate the number of players, so that whenever new player is created or previous player is deleted, all the present players could know how many players are left or how many are destroyed.

As you can see whenever the new object is created, the value of static member is incremented in a constructor therefore after creating two objects of a game class the value of static member becomes 2.

Static Functions in C++:-

These are the type of member function which could be directly accessed by the name of a class. These are basically the part of a class instead of objects. Therefore, only one copy of these member functions is created in a memory with the creation of a class. Sometimes we need to access the member function without creating the objects of a class, in this case static functions are used. As we have described above about the static data member that they are also the part of a class instead of its objects therefore we could access these static variables and could perform functionality upon them without creating the objects of the class. In other words the static functions are used to access the static data members of a class.

For Example:-


#include<iostream.h>
#include<conio.h>

class game
{
static int presentplayers;
public:

game()
{
presentplayer++;
}

~game()
{
presentplayer--;
}

static void reset()
{
presentplayers= 0;
}

static void show()
{
cout<<presentplayer<<endl;
}

}

main()
{
game.reset();
game player1;
player1.show();
game player2;
player2.show();
game.reset();
game.show();
game player3;
game.show();

getch();
}

Explanation:-

[yop_poll id=”18″] Just consider a theoretical example, that is if we created a class game, then we created different objects of this class that player1, player2, and so on, and we wanted to count the number of players that are basically objects of a class game therefore we will define a static data member to calculate the number of players, so that whenever new player is created or previous player is deleted, all the present players could know how any players are left, how many are destroyed or either the game is reset.

As you can see first of all we have called the static function reset () by the name of a class, it will set the value of data member “presentplayers”, then we will create two objects player1 and player2, and call the static member function by the name of an object, therefore it will display the number of players. One thing should be remembered that static functions could be called by the name of a class and we could also call them by the name of an object, but calling a static member function by the name of an object is considered a logical error, although it won’t generate compilation errors. At the end, we will again call the reset function, we will create object player3 and again call the static function show to display the number of players which are 1 now.

If you like my work and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a Reply

HellGeeks
Logo