class Counter
{
private:
    static int count;   // ONE shared copy across all objects
public:
    Counter() { count++; }
    static int getCount() { return count; }
};
int Counter::count = 0;   // static members must be defined outside the class too

MCQ Trap: static members belong to the class (one shared copy); non-static (instance) members belong to each object separately.

Full worked example — static vs instance data members together

#include <iostream>
using namespace std;

class Counter
{
private:
    int id;              // instance member -- each object has its own
    static int count;    // static member -- ONE shared copy for the whole class

public:
    Counter()
    {
        count++;          // increments the SHARED copy
        id = count;        // this object's own personal id
    }

    void show()
    {
        cout << "My ID: " << id << ", Total objects created: " << count << endl;
    }
};

int Counter::count = 0;   // static members MUST be defined outside the class (once, at file scope)

int main()
{
    Counter a, b, c;
    a.show();   // My ID: 1, Total objects created: 3
    b.show();   // My ID: 2, Total objects created: 3
    c.show();   // My ID: 3, Total objects created: 3
    return 0;
}

Notice count is the same value (3) across all three objects — because there's only one shared copy — while id is different for each, since it's an instance member.

Why the initializer list matters

class Point
{
private:
    const int x;   // const member -- MUST be set at construction, can't be assigned later
public:
    Point(int val) : x(val) {}   // initializer list -- the ONLY way to set a const member
    // Point(int val) { x = val; }  -- ERROR: cannot assign to a const member in the constructor body
};

Important MCQs

  1. A static data member is:
  2. Static data members must be:
  3. A const data member can only be initialized via:
  4. In the Counter example, after creating 5 Counter objects, the static count will equal:

Self-Test Questions

  1. Why must static data members be defined a second time outside the class?
  2. Why does a const data member require an initializer list instead of body assignment?
  3. In the Counter example, what would count be after creating 5 objects?

📌 Sample & Repeated FPSC Questions (2016–2026)