C++ में Static Members(Static Data Members और Static Member Functions) की संपूर्ण जानकारी
What is Static Members(Static Class members) in C++ in Hindi
C++ में Static members, Class के level का Variables और Methods होता है जिसे Class के सभी Objects द्वारा share किया जाता है।
ये "static" keyword के साथ Declare किए जाते हैं और Class का Object बनाए बिना इसे Access किया जा सकता हैं।
Static members को Program के शुरू होने से पहले एक बार Initialize किया जाता है।
ये Code को संगठित करने, दक्षता में सुधार करने मे मदद करते हैं और वे Singleton या Factory जैसे Programming patterns में महत्वपूर्ण भूमिका निभाते हैं।
एक Class में Data(Variables) और Functions में कोई भी Static members हो सकते हैं
1) Static Data member/Variables
2) Static member functions
Static Data Members in C++ in Hindi
किसी Class के Static Data members को 'Class variables' भी कहा जाता है क्योंकि यह एक Class level का Variable है जिसे उस Class के सभी Objects के द्वारा Share किया जाता है। अर्थात एक ही Class के सभी Objects के लिए सिर्फ एक ही मान होता है
Regular variable के विपरीत जो प्रत्येक Instance(Object) के लिए Unique होते है, Static variable अपने Value को Objects में बनाए रखते है अतः इसका Use अक्सर Constants और Counters के लिए किया जाता है।
Static Data Members(Class member variables) की विशेषताएं
1) जब भी Class के अंदर Static Data members बनाना चाहते हैं तो हमें उसे ' static' keywords के साथ Define
करना होता है।
2) यह Class के अंदर Declare होता है।
3) इसे Class के बाहर अवश्य ही Define होना होता है।
4) पूरे Class के लिए, Static Data members की केवल एक ही Copy होती है।
5) एक Static Data members , Class के सभी Objects द्वारा Share किए जाते हैं अर्थात सभी Objects को एक ही Static member को share करना होता है।
6) Static Data members, 0 से Initialize होता है। यह केवल एक बार ही Initialize होता है।
Syntax:
static data-type variableName;
ऊपर Syntax में static एक keyword है, data-type, Variable का प्रकार है कि वह किस प्रकार की Value Store करेगी तथा variableName, Static data member को दिया गया नाम है
Example:
static int balance;
Declaration and Definition of Static Data members in C++ in Hindi
एक Static Data members को निम्न प्रकार से Declare और Define करते है
1) Class Definition के अंदर Declare करना
2) Class Definition के बाहर Define करना
Class के अंदर Static Data members को declare करने के लिए ' static' keyword का Use किया जाता है और Class के बाहर Define करने के लिए Class का नाम तथा Scope resolution Operator(::) का use किया जाता है
Accessing Static Data Members in C++ in Hindi
1) Scope resolution Operator(::) का Use करके
2) Class objects का Use करके(Not recommended)
1) Scope resolution Operator का Use करके:
C++, में Static data members, Class के Specific Objects के बजाय Class के साथ सम्बन्धित रहता है।
इसलिए Static data members को Specific Objects के द्वारा Dot (`.`) operator का Use करके Access करने की जरुरत नहीं है।
इसके बदले आपको Class नाम के साथ Scope resolution operator (`::`) का Use करना होता है।
Example:
class Demo{
public:
static int x;
};
// Accessing the static member using scope resolution operator
int Demo::x = 12;
2) एक Object का Use करके Access करना (not recommended for static members):
Static data members को Access करने के लिए Object का Use करना भी संभव है।
और यह आमतौर पर recommended नहीं है और इसका Use करना भ्रमित करने वाला हो सकता है क्योंकि Static members का संबंध Class ही से होता है।
Example:
class Demo{
public:
static int x;
};
Demo obj;
obj.x = 24;
अतः Static members को Access करने का सही तरीका Class के नाम का Use करके ही करना है
नीचे हम एक Program का निर्माण किए है जिसमें निम्न बाते शामिल है
1) Static Data Members को Class Definition के अंदर Declare किया गया है।
2) Static Data Members को Class Definition के बाहर Define किया गया है।
3) Static Data Members को Access करने के लिए Scope resolution operator(::) का Use किया गया है
Program for Static Data(Variable )Members
#include<iostream>
using namespace std;
// Declaration of static member variable 'count' in class 'myClass'
class myClass {
public:
static int count; // Declaration of static member variable
};
// Definition and initialization of static member variable 'count' outside the class definition
int myClass::count = 0;
int main() {
// Increment 'count' directly by the class name
myClass::count++; // Increment count
myClass::count++; // Increment count again
myClass::count++; // Increment count again
// Print the current value of 'count' directly by using class name
cout << "Count: " << myClass::count << endl;
return 0;
}
Output:
Count: 3
Explaination:
1) Static Member Variable:
`static int count;
`myClass` नामक Class में एक Static member variable `count`, नाम से
Declare किया गया है
2) Static member variable(count )को Initialize करना:
int myClass::count = 0;
`count` को Class definition के बाहर Define तथा initialize किया गया है तथा उसकी Value को 0 set किया गया है
3) `count` को Increment करना:
myClass::count++;
`main()`, में `myClass::count` को एक Static members variable के रूप में इसके उपयोग को प्रदर्शित करने के लिए, तीन बार Increment किया गया है।
4)`count` को Access करना :
cout << "Count: " << myClass::count << endl;
`count` की Current value को Access करने के लिए Class का नाम और Scope resolution operator(::) का Use किया गया है। तथा उसे `cout` का Use करके Print किया गया है।
Static Member Functions in C++ in Hindi
C++ में, Static member functions, Static data member की तरह ही एक Class के, Instance(Objects) के बजाय सीधे ही Class से संबंधित होता है।
इन्हे Class name, का Use करके Call किया जाता है अर्थात Static member functions को Call करने के लिए Object Instance की जरूरत नहीं होती।
इन्हे केवल Static data members और अन्य Static member functions के द्वारा ही सीधे Access किया जाता है।
Static member functions ऐसे Operations जैसे Utility functions, Counters, या Factory methods, के लिए Use किया जाता है जो विशिष्ट Instance (Object)data पर निर्भर नहीं हैं बल्कि संपूर्ण Class से संबंधित हैं।
ये Object के निर्माण के बिना Class के सभी Instances में Data share और Methods तक Efficient Access प्रदान करते हैं
Property of Static Members Functions in C++ in Hindi
1) Static member functions भी Class के Object पर निर्भर नहीं करता हैं।
2) Static member functions का निर्माण करते समय हम static keyword का Use करके उसे Declare करते हैं।
3) एक Static member function केवल अन्य Static variable या उसी Class में उपस्थित Function द्वारा Access किए जा सकते है।
4) Static member functions को Class name और Scope resolution operator(::) का Use करके Call किया जा सकता है। आपको इसके लिए पहले Objects का निर्माण करने की जरूरत नहीं है
5) Regular functions की तरह ही एक ही Class में Static member functions के कई versions बना सकते हैं।
6) आप Static member function का Use सीधे ही Non static variables या function के साथ नही कर सकते है।
Program for Static Member Functions
#include<iostream>
using namespace std;
// Declaration of static member variable 'count' in class 'myClass'
class myClass {
public:
static int count; // Declaration of static member variable
// Static method 'increment' to increase the value of 'count'
static void increment()
{
count++; // Increment the static variable 'count'
}
};
int myClass::count = 0;
int main() {
// Using static method 'increment' to increase 'count'
myClass::increment(); // Increment count by static method
myClass::increment(); // Increment count again
myClass::increment(); // Increment count again
0 टिप्पणियाँ