हिंदी माध्यम नोट्स
Categories: c++ language in hindi
c++ : scope and linkage , what is scope and linkage in c++ language in hindi , External , internal , Global scope
External , internal , Global scope , c++ : scope and linkage , what is scope and linkage in c++ language in hindi :-
इससे पहले के article मे memory space को discuss किया है | storage class मे data variable के memory access को define किया जाता है | c++ language मे निन्म storage class को मुख्य है :
1. Automatic
जब किसी variable को function definition मे define किया जाता है तब इस variable का storage class automatic होता है | ये variable तब execute होता जब function को execute किया जाता है | variable के लिए allocate memory free हो जाती जब function का execution नहीं होता है | c++ language मे निन्म दो प्रकार के variable होते है :
Static :
जब किसी variable को function के बहार declare किये जाता है पर इस variable को static keyword के साथ declare किया जाता है | तब इस variable को static variable कहते है | इसकी variable की value पुरे प्रोग्राम मे constant रहती है | c++ language मे , तीन प्रकार के static variable होते है |
dynamic :
जब किस variable को new operator से allocate किया जाता है तब इस variable को dynamic variable कहते है | इस variable को free तब किया जाता है जब तक इसकी delete() function को declare नहीं किया जाता है |
Scope And Linkage
scope का मतलब है life time of variable in file |जब किसी variable को function की अनादर use किया जाता है | इस variable को केवल function मे use किया जाता है | लेकिन जब variable को को function के बहार declare किया जाता है तब इस variable को सभी functions मे use किया जा सकता है ||
linkage का मतलब है किसी name को अलग अलग functions मे use कैसे किया जा सकता है | ये दो प्रकार होता है :
External Linkage
जब किसी name को external linkage के साथ use किया जा सकता है तब इस name को अलग अलग files मे use किया जा सकता है |
Internal Linkage
जब किसी name को internal linkage के साथ use किया जाता है तब इस name को केवल एक file के functions को use किया जा सकता है |
c++ मे कई प्रकार के scope को use किया जाता है जैसे
local scope : local scope को केवल एक block मे ही declare किया जाता है | जैसे अगर for()loop मे control variable ‘i’ को केवल फोए loop की body मे ही use किया जा सकता है |इसे block scope भी कहते है | क्योकि इसे block मे ही use किया जा सकता है |
Global scope
global scope का मतलब होता हगे variable की lifetime पुरे प्रोग्राम के समय होता है |
automatic variable का scope local होता है लेकिन static variable का scope global और local दोनों हो सकता है |
name को किसी function के argument मे pass किया जाता है तब इसका scope function prototype scope है | किसी class के member function का scope class scope होता है |जब किसी variable को namespace मे declare किया जाता है तब इस variable का scope namespace होता है |
c++ function का scope class scope और namespace scope दोनों होते है | अतः इसका scope global होता है लेकिन इसका scope local नहीं होता है |
Automatic Storage Duration
function parameter और variables को किसी function के अन्दर declare किया जाता है तब उसका scope आटोमेटिक होता है | इसका scope local होता है लेकिन इसमें linkage नहीं होता है | उदाहरन के लिए ज variable tax को main () function मे use किया जा सकता है या यूजर द्वारा किसी दुसरे function मे equal name का use किया जा सकता है |
जब किसिस प्रोग्राम मे variable को execute किया जाता है तब प्रोग्राम innermost block को allocate करता है | जो की function definition को hold करता है | जब function terminate हो जाता है जब ये block भी free हो जाता है |
#include<iostream.h>
#include<conio.h>
void oil(int a);
using namespace std;
int main()
{
int tax= 234;
int year = 2001;
cout<<“IN main(), tax = “<<tax <<“, &tax : “<<&tax;
cout<<“IN main(), year = “<<year <<“, &year : “<<&year;
oil(tax);
cout<<“IN main(), tax = “<<tax <<“, &tax : “<<&tax;
cout<<“IN main(), year = “<<year <<“, &year : “<<&year;
getch();
}
void oil(int a)
{
int tax = 5;
cout<<“IN oil(), tax = “<<tax <<“, &tax : “<<&tax;
cout<<“IN oil(), a = “<<a <<“, &a : “<<&a;
{
int tax=8;
cout<<“IN block , tax = “<<tax <<“, &tax : “<<&tax;
cout<<“IN block , a = “<<a <<“, &a : “<<&a;
}
इस प्रोग्राम मे variable tax को तीन बार declare किया जाता है | इस function मे variable tax को main() function मे declare किया जाता है | सबसे पहले tax की value print किया जाता है |
इसके बाद tax की value को oil() function मे पास किया जाता है | इसके बाद tax की value को print किया जाता है
oil() मे ,
इस function मे tax को भी declare किया जाता है और इसकी value ‘5’ होती है | इसके बाद tax की value को print किया जाता है |
किसी block मे tax को declare किया जाता है | इसमें tax की value ‘8’ को initial किया जाता है | इसके बाद tax की value को print किया जाता है |
इसका आउटपुट होगा :
IN main(), tax = 234 , &tax : 4032
IN main(), year = 2001 , &tax : 4034
IN main(), tax = 5 , &tax : 4032
IN main(), year = 2001 , &tax : 4034
IN main(), tax = 234 , &tax : 4032
IN main(), year = 2001 , &tax : 4034
IN main(), tax = 8 , &tax : 4036
IN main(), year = 2001 , &tax : 4038
इस article मे , scope को discuss किया जाता है | अब आगे article मे , namespace को discuss करेगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago