what is scope in c++ language , C++ : SCOPE in hindi :-
इससे पहले के article मे function के type , structure , default parameter और return statement को discuss किया गया है | function के आधार variable को दो भागो मे divind किया गया है |
C++ language मे किसी variable को दो feture होता है :-
1.type : ये data variable के type को define करता है |
2.Storage class : ये data variable के visiablity , scope और usage को define करता है | storage class से variable की lifetime और scope को define करता है | lifetime का मतलब है variable की time period जब तक ये active होता है | और scope का मतलब होता है variable की activeness प्रोग्राम मे जब तक active रहता है |
c++ मे data type int , char , float और double आदि होते है | जिसे हम पहले discuss कर चुके है | अब इस article मे storage class को discuss करेगे |
c++ मे निन्म मुख्य storage class होती है :-
1.local variable
जब किसी variable को function के अन्दर declare किया जाता है | उस variable को local variable कहते है इसमें variable को {} मे लिखा जाता है | अतः scope केवल function की body मे ही होता है | अथार्त variable का activeness केवल function की body मे ही होता है | जब प्रोग्राम / function को terminate किया जाता है तब local variable का activeness भी destroyed हो जाती है |
इसका उदाहरन होता है :
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b ;
cout<<“Enter value 1 : “;
cin>>a;
cout<<“Enter value2 : “;
cin>>b;
int output = fmod(a,b);
cout<<” remainder output of v2/v1:”<<output<<endl
}
getch();
}
इसके उदाहरन मे , यूजर द्वारा दो value को input किया जाता है | इन value को a और b मे assign किया जाता है | और इस int output = fmod(a,b); इस statement से first remainder expression की value को calculate किया जाता है | यहा पर variable ‘a’ और ‘b’ दोनों local variable है क्योकि इन्हें main() function मे declare किया गया है | in variable को main() function के बहार use नहीं कर सकता है |
इसका आउटपुट होगा :
Enter value 1 : 100
Enter value 2 : 1000
remainder output of v1 : 100
1.Global variable
जब किसी variable को function के बहार declare किया जाता है | उस variable को global variable कहते है इसमें variable को {} के बहार लिखा जाता है | अतः scope पुरे function की body के साथ दुसरे main() मे ही होता है | अथार्त variable का activeness function की body और main() मे ही होता है | जब प्रोग्राम / function को terminate किया जाता है तब local variable का activeness भी destroyed नहीं होता है |
इसका उदाहरन होता है :
#include<iostream.h>
#include<conio.h>
#include<math.h>
int a,b ;
void sub(int , int );
void main()
{
cout<<“Enter value 1 : “;
cin>>a;
cout<<“Enter value2 : “;
cin>>b;
int output = fmod(a,b);
cout<<” remainder output of a/b:”<<output<<endl
}
getch();
}
void add(int ,int )
{
cout<<“Enter value 1 : “;
cin>>a;
cout<<“Enter value2 : “;
cin>>b;
int c= a+b;
cout<<” addition output of a+b :”<<c<<endl
}
इसके उदाहरन मे , यूजर द्वारा दो value को input किया जाता है | इन value को a और b मे assign किया जाता है | और इस int output = fmod(a,b); इस statement से first remainder expression की value को calculate किया जाता है | यहा पर variable ‘a’ और ‘b’ दोनों global variable है क्योकि इन्हें main() function के बहार declare किया गया है | इन variables को main() function और add() function मे use किया गया है |
इसका आउटपुट होगा :
Enter value 1 : 100
Enter value 2 : 10
remainder output of a/b : 10
Enter value 1 : 100
Enter value 2 : 1000
addition output of a+b : 1100
Static variable
static variable एसे variable को कहते है जिसका scope local variable के तरह होता है लेकिन इसकी value पुरे प्रोग्राम मे constant रहता है | इसका मतलब है की इस variable की activeness केवल प्रोग्राम / function तक रहती है | और अगर एक बार static variable को initial किया जा सकता है | उसके बाद इसकी value को change नहीं कर सकता है | इस variable को declare करने के लिए static keyword को use किया जाता है |
#include<iostream.h>
#include<conio.h>
#include<math.h>
void sub(int , int );
void main()
{
static int a,b ;
cout<<“Enter value 1 : “;
cin>>a;
cout<<“Enter value2 : “;
cin>>b;
int output = fmod(a,b);
int output2 = add(a,b);
cout<<” remainder output of a/b:”<<output<<endl;
cout<<” addition output of a+b :”<<c<<endl;
}
getch();
}
void add(int e ,int f )
{
int c= e + f ;
return c;
}
इसके उदाहरन मे , यूजर द्वारा दो static value को input किया जाता है | इन value को a और b मे assign किया जाता है | और इस int output = fmod(a,b); इस statement से first remainder expression की value को calculate किया जाता है | और इस int output = add(a,b); इस statement से addition को calculate किया जाता है यहा पर variable ‘a’ और ‘b’ दोनों static variable है क्योकि इन्हें value को एक बार initail किया जा सकता है |
इसका आउटपुट होगा :
Enter value 1 : 100
Enter value 2 : 10
remainder output of a/b : 10
addition output of a+b : 1100
Register variable
Register variable ये variable होता है जिसे computer memory मे register मे store किया जाता है |इसे declare करने के लिए register keyword को use किया जाता है | ये variable नोरमल variable की तरह होते है लेकिन इन variable नोरमल variable से faster access किया जा सकता है | c++11 मे register variable को declare नहीं किया जाता है | क्योकि register keyword को c++11 मे allow नहीं कर सकते है |
Thread Local Storage
Thread Local Storage ये वो class होती है जिसमे variable के एक उदाहरन को प्रोग्राम मे declare किया जाता है इसके लिए Thread_local keyword को use किया जा सकता है |
इस article मे storage class को अच्छी तरह से discuss किया गया है | अब आगे के article मे function recursion को discuss करेगे |