
Static Data Members and Functions in C++
In daily life, some things are shared by everyone rather than belonging to just one person. For example, in a classroom the notice board is the same for all students. If one student puts a notice, every student can see it. Similarly, in a society, the water tank is shared by all houses. In C++, such shared values or functions are handled using the static
keyword.
A static member in C++ belongs to the class itself rather than to individual objects, and its value is shared among all objects of that class.
Why Do We Need Static?
Contents
- Shared Data: Some values are common for all objects. For example, the total number of students in a class is the same information for all students.
- Memory Efficiency: Instead of each object having its own copy, only one shared copy is created.
- Persistence: A static variable keeps its value throughout the program execution, instead of being created and destroyed repeatedly.
Static Variables Inside Functions
Normally, when a variable is declared inside a function, it is created every time the function is called and destroyed when the function ends. But if it is declared as static
, it is created only once and remembers its value between function calls.
#include <iostream>
using namespace std;
void demo() {
static int x = 0; // created only once
x++;
cout << "x = " << x << endl;
}
int main() {
demo();
demo();
demo();
return 0;
}
Output:
x = 1
x = 2
x = 3
Static Data Members in Classes
When a variable is declared as static
inside a class, it is shared by all objects of that class. Only one copy exists in memory.
Program Example: Counter
#include <iostream>
using namespace std;
class counter {
public:
static int count; // Declaration of static variable
counter() {
count++; // Every new object increases the same shared count
}
static void show() { // static function
cout << "The value of counter is: " << count << endl;
}
};
// Definition of static variable outside the class
int counter::count = 0;
int main() {
counter C1;
C1.show();
counter C2;
C2.show();
counter C3;
counter::show(); // Can also be called using class name
cout << "Try programiz.pro" << endl;
return 0;
}
Explanation
static int count;
→ declared inside the class.int counter::count = 0;
→ defined outside the class.- Every time a new object (
C1
,C2
,C3
) is created, the constructor incrementscount
. - The function
show()
is declared static, so it can be called without creating an object (e.g.,counter::show();
). - All objects share the same
count
.
Daily Life Example
Think of class attendance:
Each student can be thought of as an object, while the attendance register in the classroom is a single shared record. No matter how many students there are, they all refer to and update the same register when marked present. In the same way, the static variable count
is like that register—every object (student) updates the same shared value.
Static Member Functions
A static function inside a class can be called without creating an object. It only works with static data members, since those are also shared.
class MyClass {
public:
static int value;
static void display() {
cout << value << endl;
}
};
Comparison: Normal vs Static Members
Aspect | Normal Member | Static Member |
---|---|---|
Storage | Each object has a copy | Shared by all objects |
Lifetime | Created/destroyed with obj | Exists till program ends |
Access | Through objects only | Through objects or class |
Memory Efficiency | Less efficient (duplicates) | More efficient (single copy) |
Practice Exercises
- Write a program to count how many objects of a class are created using a static variable.
- Create a static function in a class that displays the total number of books in a library.
- Demonstrate a static variable inside a function that keeps its value across multiple calls.