C++ Static Member Variables and Their Initialization



The static member variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

In this article, we will understand the static member variables and their characteristics, and go through various examples explaining the characteristics of static member variables. The characteristics of the static keyword are mentioned below:

Characteristics of static Member Variable

Here are the characteristics of static member variables:

  • For any one class, there exists just one copy of that static member and is shared by all the objects of that class.
  • The static data member is a class member, i.e., independent of object creation. You do not need to create a class to access the static data members.
  • The static data member is declared inside a class but is defined outside the class using the scope resolution operator(::).
  • The static data members can be accessed using the class names. You can use objects too for accessing static data members, but that is not preferable, as static members are independent of object creation.
  • The static data members can have any access modifiers, i.e., either public, private, or protected.

Syntax to Declare an Define static Member

Here is the syntax to declare and define a static member variable in C++:

#include <iostream>
using namespace std;

class Animal {
   public:
   static int count;  //Declaring static variable
};

int Animal::count = 0;  //Defining static variable

int main() {
   // Code
}

Sharing Static Variables Across All Object Instances

The static members are class members, i.e., independent of the objects. If we define the static variable value using one object, then it can be accessed using any other object of the same class.

Example

In this example, we have created a class named 'Animal' and its two objects, i.e., 'dog' and 'cat'. We have set the value of the static variable count using the 'dog' object and returning the count value of cat:

#include <iostream>
using namespace std;

class Animal {
   public:
   static int count;  //Declaring static variable
};

int Animal::count = 0;  //Defining static variable

int main() {
   Animal dog, cat;
   dog.count = 5; //setting value of dog object
   cout << "Count of cats: " << cat.count << endl;
   return 0;
}

The output of the above program is as follows:

Count of cats: 5

Accessing Static Member Using Class

The static member variables are class variables, i.e., we do not need to use objects to access the static variables. Here is an example of accessing the static variable name using a class.

Example

In this example, we have used the Company class to directly access the static variable name without initializing an object.

#include <iostream>
using namespace std;

class Company {
   public:
   static string name;  //Declaring static variable
};

string Company::name = "Tutorials Point";  //Defining static variable

int main() {
   
   cout << "Welcome to " << Company::name << endl; //Accessing static variable using class 
   return 0;
}

The output of the above code is as follows:

Welcome to Tutorials Point

Accessing Static Member Using Object

The static variables are object-independent, i.e., we don't need an object to access the static variables. But we still can use objects to access the static variables.

Example

In this example, we have created an object 'c1' of class Company to access the static variable name.

#include <iostream>
using namespace std;

class Company {
   public:
   static string name;  //Declaring static variable
};

string Company::name = "Tutorials Point";  //Defining static variable

int main() {
   Company c1;  //Creating objects
   cout << "Welcome to " << c1.name << endl; //Accessing static variable using Object 
   return 0;
}

The output of the above code is as follows:

Welcome to Tutorials Point
Note: We can access static members using objects, but it is recommended to use the class.

Static Member Variables with Access Modifiers

We can use any access modifiers, i.e., 'public', 'private', and 'protected', to access static variables.

Example

In this example, we have used a static function to access the 'private' and 'protected' static variables:

#include <iostream>
using namespace std;

class Example {
public:
    static int publicVar;

protected:
    static int protectedVar;

private:
    static int privateVar;

public:
    // Static function to access private and protected members
    static void print() {
        cout << "Accessing Public using static function : " << publicVar << endl;
        cout << "Accessing Protected using static function: " << protectedVar << endl;
        cout << "Accessing Private using static function: " << privateVar << endl;
    }
};

// Defining static members 
int Example::publicVar = 10;
int Example::protectedVar = 20;
int Example::privateVar = 30;

int main() {
    cout << "Accessing Public variable directly: " << Example::publicVar << endl;
    Example::print();
    return 0;
}

The output of the above code is as follows:

Accessing Public variable directly: 10
Accessing Public using static function : 10
Accessing Protected using static function: 20
Accessing Private using static function: 30
Updated on: 2025-05-13T19:34:21+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements