Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 43

Static Data Members and Static Functions

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

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.

Static member क्या है?

सामान्यतः हर object के पास हर member की अपनी copy होती है। Static member अलग है: यह class के लिए ही होता है, तो केवल एक copy होती है जो सभी objects साझा करते हैं। यह हर instance के लिए साझा किसी चीज़ के लिए एकदम सही है — जैसे कितने objects मौजूद हैं इसकी चलती गिनती।

Static data members

किसी data member को static declare करें और हर object इसे साझा करता है:

C++
class Counter {
public:
    static int count;   // sabhi objects dwara shared
};

Static member परिभाषित करना

Static data member को storage देने के लिए class के बाहर एक बार परिभाषित भी करना होगा:

C++
int Counter::count = 0;   // class ke bahar definition

इस class-बाहर परिभाषा को भूलना linker error पैदा करता है।

Static member functions

static member function बिना किसी object के class पर call हो सकता है। यह static members इस्तेमाल कर सकता है, पर per-object data नहीं:

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

उदाहरण: object counter

एक classic इस्तेमाल — गिनें कि कितने objects बनाए गए:

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

बिना object access करना

Static members class नाम और scope operator से पहुँचे जाते हैं: Widget::count, Math::square(5)। आप object भी इस्तेमाल कर सकते हैं (a.count), पर class-नाम रूप "साझा" प्रकृति स्पष्ट करता है।

आम गलतियाँ

  • Static data member की class-बाहर परिभाषा भूलना (linker error)।
  • Static function के अंदर non-static members इस्तेमाल करने की कोशिश।
  • यह उम्मीद करना कि हर object की static member की अपनी copy हो।
  • कई files में शामिल header के अंदर static member को फिर से परिभाषित करना।
🏋️ अभ्यास

किसी class में एक static count जोड़ें जो constructor में बढ़े और destructor में घटे। अलग scopes में objects बनाएँ और नष्ट करें और live count print करके इसे बढ़ते-घटते देखें।

सारांश

  • Static member class के लिए होता है, सभी objects द्वारा साझा।
  • Static data members को class के बाहर एक परिभाषा चाहिए।
  • Static member functions बिना object के call हो सकते हैं।
  • Static function के पास this नहीं होता और per-object data इस्तेमाल नहीं कर सकता।
  • सामान्य इस्तेमाल में साझा counters और class-wide utilities शामिल हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में static member क्या है?
Static member किसी एकल object के बजाय class के लिए ही होता है, तो सभी objects एक copy साझा करते हैं। static keyword से घोषित, static data member हर instance के लिए एक साझा value रखता है, और static member function बिना object के call हो सकता है।
C++ में static और non-static member में क्या अंतर है?
Non-static member की हर object में अलग copy होती है, जबकि static member की केवल एक copy होती है जो class के सभी objects साझा करते हैं। Non-static members व्यक्तिगत objects का वर्णन करते हैं; static members class के बारे में कुछ समग्र वर्णन करते हैं, जैसे एक साझा गिनती।
C++ में static data member कैसे परिभाषित करते हैं?
आप इसे class के अंदर static से declare करते हैं, फिर storage आवंटित करने को class के बाहर एक बार परिभाषित करते हैं, जैसे int Counter::count = 0;। उस class-बाहर परिभाषा के बिना program link नहीं होता, क्योंकि member को कहीं वास्तविक storage चाहिए।
C++ में static member function क्या है?
Static member function class के लिए होता है और बिना किसी object के class नाम से call हो सकता है, जैसे Math::square(5)। चूँकि यह किसी object से बँधा नहीं, इसके पास this pointer नहीं होता और यह केवल static data members सीधे इस्तेमाल कर सकता है।
क्या C++ में static member function non-static members access कर सकता है?
नहीं, सीधे नहीं। Static member function के पास this pointer नहीं होता, तो यह non-static (per-object) data को refer या अपने आप non-static functions call नहीं कर सकता। यह केवल static members के साथ काम कर सकता है जब तक आप इसे इस्तेमाल को कोई object न दें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.