Scope resolution operator in C++

    Scope Resolution Operator

    Scope Resolution Operator(स्कोप रिज़ॉल्यूशन ऑपरेटर) का उपयोग global variable या Member Function को reference करने के लिए किया जाता है । इसलिए, हम किसी प्रोग्राम के hidden variable(हिडन वेरिएबल) या फंक्शन को एक्सेस करने के लिए Scope Resolution Operator(स्कोप रिज़ॉल्यूशन ऑपरेटर) का उपयोग करते हैं। ऑपरेटर को double colon (::) प्रतीक के रूप में दर्शाया गया है।

    उदाहरण के लिए, जब किसी प्रोग्राम में global variable (ग्लोबल वेरिएबल ) और Local variable(लोकल वेरिएबल) या function(फंक्शन) का एक ही नाम होता है, और जब हम variable(वेरिएबल) को कॉल करते हैं, तो डिफ़ॉल्ट रूप से यह global variable(ग्लोबल वेरिएबल) को कॉल किए बिना केवल इनर या लोकल वेरिएबल को एक्सेस करता है। इस तरह, यह global variable(ग्लोबल वेरिएबल) या फ़ंक्शन को छुपाता है। इस स्थिति को दूर करने के लिए, हम प्रोग्राम के hidden variable या फ़ंक्शन को use के लिए Scope Resolution Operator(स्कोप रिज़ॉल्यूशन ऑपरेटर) का उपयोग करते हैं।

    Uses of scope resolution operator 

    1. इसका उपयोग किसी प्रोग्राम के Hidden Variable या Member Function तक पहुँचने के लिए किया जाता है।
    2. यह Scope Resolution Operator का उपयोग करके Class के बाहर Member Function को परिभाषित करता है।
    3. इसका उपयोग किसी class के static variable और static function तक पहुँचने के लिए किया जाता है।
    4. Scope Resolution Operator(स्कोप रिज़ॉल्यूशन ऑपरेटर) का उपयोग Inheritance(इनहेरिटेंस) में फ़ंक्शन को override (ओवरराइड) करने के लिए किया जाता है।

    Example 1 Scope Resolution Operator:-


        #include <iostream>
        #include <conio.h>
        using namespace std;
        int a = 10;
        int main()
        {
            int a = 20;
            cout << "a = " << a << endl;
            cout << "::a = " << ::a;
            getch();
        }
       

    Output:-

    a = 20
    ::a = 10

    उपरोक्त उदाहरण में a नाम के दो variable Declare किए हैं। इनमे एक global Variable तथा दूसरा Local Variable हैं ध्यान दे कि Local Variable की Presidency (प्रिसिडेन्स) Global Variable से अधिक होती हैं,  अर्थात ऐसी स्थिति में global Variable छुप जाते हैं, तथा Local Variable प्रयोग में लिए जा सकते हैं। इसी कारण से जब  cout << "a = " << a << endl;  के माध्यम से a की वैल्यू print करवाई गई तो Local variable की वैल्यू print हुई। ऐसी स्थिति में यह हमें Global variable की value प्रयोग में लेनी हो तो Scope Resolution Operator (::)  का use किया जाता हैं। यहीं कार्य cout << "::a = " << ::a;  के माध्यम से किया गया हैं।

    Example 2 Scope Resolution Operator:-

    Scope Resolution Operator (::)  का उपयोग करके hidden value को access करना। 


        #include <iostream>  
        using namespace std;  
        // declare global variable  
        int num = 50;  
        int main ()  
        {  
        // declare local variable  
        int num = 100;  
       
        // print the value of the variables  
        cout << " The value of the local variable num: " << num;  
       
        // use scope resolution operator (::) to access the global variable  
        cout << "\n The value of the global variable num: " << ::num;  
        return 0;  
        }  
       

    Output:-

     The value of the local variable num: 100
     The value of the global variable num: 50

    Example 3: Scope Resolution Operator:-

    Scope Resolution Operator (::)  का उपयोग करके Class के बाहर Member Function को Define करने का Program


        #include <iostream>
        using namespace std;
        class Operate
        {
        public:
            // declaration of the member function
            void fun();
        };
        // define the member function outside the class.
        void Operate::fun() /* return_type Class_Name::function_name */
        {
            cout << " It is the member function of the class. ";
        }
        int main()
        {
            // create an object of the class Operate
            Operate op;
            op.fun();
            return 0;
        }
       

    Output:-

     It is the member function of the class. 

    Example 4: Scope Resolution Operator:-

    Scope Resolution Operator (::)  का उपयोग करके   Member Function को ओवरराइड करने का Program


        #include <iostream>
        using namespace std;
        class ABC
        {
            // declare access specifier
        public:
            void test()
            {
                cout << " \n It is the test() function of the ABC class. ";
            }
        };
        // derive the functionality or member function of the base class
        class child : public ABC
        {
        public:
            void test()
            {
                ABC::test();
                cout << " \n It is the test() function of the child class. ";
            }
        };
        int main()
        {
            // create object of the derived class
            child ch;
            ch.test();
            return 0;
        }
       

    Output:-

     It is the test() function of the ABC class.  
     It is the test() function of the child class.

    Post a Comment

    0 Comments