C++ Static Members In Hindi

📔 : C++ 🔗

पिछले Topic में आपने समझा कि C++ में Class क्या होती है और इसे कैसे define करते हैं। किसी भी Class में दो तरह के members (Variables / Methods ) हो सकते हैं , Static & Non Static . इस Topic में आप पढ़ेंगे कि Class में Static Members क्या होता है , कैसे इन्हे define करते हैं और किस - किस तरह से इन्हे Access कर सकते हैं।

C++ Defining Static Members

C++ में Static Property / Methods को define करने के लिए simply predefined keyword static का use किया जाता है। किसी भी Property / Method से पहले static लिख देने से वह Property As a static define हो जाती है।
For Example :

// define static property.
static int age;

//define static method.
static void print_age(){
  // code of block.
}

C++ Accessing Static Members

C++ में Static Property / Methods Access करने के लिए हमें उस class का Object नहीं बनाना पड़ता है , simply ClassName और Double Colon (::) का use करके हम directly Class की Static Property / Methods Access कर सकते हैं।

C++ Static Member Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define a Test class.
class Test{
  // define static function.
  public: 
  static void print_age(){
    cout << "Age : " << age;
  }
  
  // define static property.
  static int age;
};

// now initialize age value.
int Test::age = 25;

int main() {
  // you can access static method using :: colon.
  Test::print_age();
  
  // you can also change the value.
  Test::age = 30;
  cout << endl;
  Test::print_age();
  return 0;
}
Output
Age : 25
Age : 30

Static members की एक single copy ही बनती है , नो matter हम कितने भी class object बनाएं। सभी Objects के लिए static members की value same ही रहती है। हालाँकि इसे आप आगे अच्छे से समझेंगे।

Important

ध्यान रहे कि static properties को आप define करते समय initialize नहीं कर सकते हैं। आपको सिर्फ static properties को define करना है , न कि initialize करना , अगर आप ऐसा करते हैं तो error generate होगी।

static int age = 90;
error: ISO C++ forbids in-class initialization of non-const static member 'Test::age'

चूंकि static members को आप बिना class object बनाये access करते हैं , इसलिए इन्हे access करने पर constructor भी run नहीं होता है। और न ही static method के अंदर आप non - static method को call कर सकते हैं , क्योंकि static method call करने के लिए class object की जरूरत होती है , और static method object के context में नहीं होते और न ही इनके अंदर आप class object बना नहीं सकते हैं।

what is constructor ?

constructor , class name का ही एक member method होता है जो , हर बार object create होने पर ही run होता है। हालाँकि इसे आप आगे पड़ेंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers