Initialization of private static members in C++

Hey learners, welcome to the tutorial on the initialization of private static data members in C++.

Before going to the topic directly, let us have a glance at what a static member means!

Static Data Member Variables

Let us assume that we have three member variables that are not static say, a, b, and c of a class named Example whose objects are object1 and object2. Then, object1 will have its own memory locations of its data member variables a,b, and c. Similarly, object2 will also have its own memory locations for its data member variables a, b, and c. Now, if you want to maintain a common member variable for all the objects in the entire program then it is possible with the Static Data members.

The main objective of static member variables is all the objects of the class will use the same memory location for the static member variables. so we can say that the static member variables are associated with class but not with the object. Therefore, the object1 and object2 objects of a class named Example will use the same memory location of the static data member variable. To know more about static data members, click here.

when the static data members variables are made private using the access specifiers then those data member variables are known as private static data member variables.

Declaration of private static data member variables

Private static data members can be declared with the help of private, static keywords.

The syntax for the declaration of private static member variable:

private:
static data-type variableName;

If static member variables are declared inside the class then we have to define them outside the class.

Syntax for the initialization of private static member variable:

return-type :: staticMemberVariable = value;

By default, if we don’t initialize the static member variable then ‘0’ is assigned to it automatically.

Now, let us understand this concept with the help of its implementation part.

Implementation: private static members in C++

Step 1:

Include the required headerfiles and namespaces. We include the iostream headerfile since we use cout and cin which are available in iostream.

#include <iostream>
using namespace std;

Step 2:

Create a class say, Example where we declare the static variable of integer type and a function named func() which increments the static member variable and prints its value.

class Example {
   private:
   static int variable;
   public:
   void func() {
       variable++;
       cout<<variable<<endl;
}
};

Step 3:

Initialize the variable by using the syntax for initialization of the private static member variable which was discussed earlier.

int Example::variable=100;

Step 4:

Creation of the main() function from where the execution of the program starts in which we create the objects for the class and call the function which increments the value by 1.

int main() {
    Example object1, object2;
    object1.func();
    object2.func();
}

Output:

101
102

Here, we observe that even though if we create two or more objects for the class, those objects refer to the same variable since it is declared as static.

Leave a Reply

Your email address will not be published. Required fields are marked *