Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 43

Static Members in C++

In this lesson: Static variables and static member functions in C++ classes — object counter example with output. In Hindi & English.

What is a static member?

Normally each object has its own copy of every member. A static member is different: it belongs to the class itself, so there is just one copy shared by all objects. It is perfect for something common to every instance — like a running count of how many objects exist.

Static data members

Declare a data member static and every object shares it:

C++
class Counter {
public:
    static int count;   // shared by all objects
};

Defining a static member

A static data member must also be defined once outside the class to give it storage:

C++
int Counter::count = 0;   // definition outside the class

Forgetting this out-of-class definition causes a linker error.

Static member functions

A static member function can be called on the class without any object. It can use static members, but not per-object data:

C++
class Math {
public:
    static int square(int x) { return x * x; }
};
int main() {
    cout << Math::square(5);   // 25 — no object needed
}
Output:
25

Example: object counter

A classic use — count how many objects have been created:

C++
#include <iostream>
using namespace std;
class Widget {
public:
    static int count;
    Widget() { count++; }        // every object bumps the shared count
};
int Widget::count = 0;
int main() {
    Widget a, b, c;
    cout << "Widgets: " << Widget::count << "\n";
    return 0;
}
Output:
Widgets: 3

Accessing without an object

Static members are reached through the class name and the scope operator: Widget::count, Math::square(5). You can also use an object (a.count), but the class-name form makes the "shared" nature clear.

Common mistakes

  • Forgetting the out-of-class definition of a static data member (linker error).
  • Trying to use non-static members inside a static function.
  • Expecting each object to have its own copy of a static member.
  • Redefining the static member inside a header included in many files.
🏋️ Practice

Add a static count to a class that increments in the constructor and decrements in the destructor. Create and destroy objects in different scopes and print the live count to watch it rise and fall.

Summary

  • A static member belongs to the class, shared by all objects.
  • Static data members need a single definition outside the class.
  • Static member functions can be called without an object.
  • A static function has no this and cannot use per-object data.
  • Common uses include shared counters and class-wide utilities.

Frequently Asked Questions

What is a static member in C++?
A static member belongs to the class itself rather than to any single object, so all objects share one copy. Declared with the static keyword, a static data member holds one value common to every instance, and a static member function can be called without an object.
What is the difference between a static and a non-static member in C++?
A non-static member has a separate copy in every object, while a static member has just one copy shared by all objects of the class. Non-static members describe individual objects; static members describe something about the class as a whole, like a shared count.
How do you define a static data member in C++?
You declare it inside the class with static, then define it once outside the class to allocate storage, such as int Counter::count = 0;. Without that out-of-class definition the program fails to link, because the member needs actual storage somewhere.
What is a static member function in C++?
A static member function belongs to the class and can be called using the class name without any object, like Math::square(5). Because it is not tied to an object, it has no this pointer and can only use static data members directly.
Can a static member function access non-static members in C++?
No, not directly. A static member function has no this pointer, so it cannot refer to non-static (per-object) data or call non-static functions on its own. It can only work with static members unless you pass it an object to use.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFriend Function and Friend Class Your progress: 43 / 81 (53%) · Lesson 44 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free