Static data member in C++ in Hindi
Static data member
किसी class में static data member को class variable भी कहा जाता है क्योंकि एक ही class के सभी objects के लिए सिर्फ एक ही मान होता है। इसके contents एक class के सभी objects के लिए एक ही होते हैं।
जब class में पहला objects बनाया जाता है तब static data member zero (0) से initialize होता है दूसरा कोई भी initialization करने की permission नहीं होती है। पर class के लिए उस member की सिर्फ एक ही copy बनायी जाती है तथा उस class के सारे objects के द्वारा share की जाती है। यह सिर्फ class के अंदर दिखता है लेकिन इसका जीवन काल पूरा program है अर्थात यह program के खत्म होने तक अस्तित्व में रहता है। static data member class में declare किये जाते हैं तथा class के बाहर define किये जाते हैं।
Syntax
Class_class name
{
Data type
Static variable
.........................
.........................
};
Data type class name :: variable initialization
Example
Int A :: variable =20;
यह परिभाषा variable को 20 से initialize करती है।
Program
#include<iostream.h>
#include<conio.h>
class sample
{
private : static int y;
public :
void set()
{
++y;
}
static void showy()
{
cout<<"y is ="<<y<<endl;
}
};
int sample : y=10;
void main()
{
clrscr();
sample s1,s2;
cout<<"call static member function first time"<<endl;
sample : showy();
s1.set y();
s2.set y();
cout<<"call static member function second time"<<endl;
sample :: showy();
getch();
}
लेख पसंद आया हो तो इसे जरूर शेयर करें।
Hi please, do not spam in comments