Site icon Afzal Badshah, PhD

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?

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

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

AspectNormal MemberStatic Member
StorageEach object has a copyShared by all objects
LifetimeCreated/destroyed with objExists till program ends
AccessThrough objects onlyThrough objects or class
Memory EfficiencyLess efficient (duplicates)More efficient (single copy)

Practice Exercises

  1. Write a program to count how many objects of a class are created using a static variable.
  2. Create a static function in a class that displays the total number of books in a library.
  3. Demonstrate a static variable inside a function that keeps its value across multiple calls.

Exit mobile version