Namespace in C++

    Namespace से आशय Classes के Group से होता है । एक Program में एक से अधिक namespace हो सकते हैं। इस प्रकार एक namespace के माध्यम से एक Program में समान नाम के Identifies(आइडेंटीफायर) का प्रयोग संभव है। यह समान नाम के Identifies(आइडेंटीफायर ) अलग-अलग namespace में हो सकते हैं। एक pre-defined namespace std का प्रयोग निम्नांकित प्रोग्राम में दर्शाया गया है और इस का use हमने C++ के First article से ही करते आ रहे हैं । ध्यान दें कि std के प्रयोग के कारण से ही <iostream.h > की जगह <iostream> का प्रयोग किया गया है।


        #include<iostream>
        #include<conio.h>
        using namespace std;
        int main()
        {
            cout<<"Namste Bharat"<<endl;
            cout<<" C++ course ";
            getch();
            return 0;
        }

    Output:-

    Namste Bharat
    C++ course

    Example #02:-

    निम्नलिखित सी ++ प्रोग्राम पर विचार करें।


        // A program to demonstrate need of namespace
        int main()
        {
            int value;
            value = 0;
            double value; // Error here
            value = 0.0;
        }

    Output:-

    Compiler Error:
    'value' has a previous declaration as 'int value'

    प्रत्येक scope में, एक name केवल एक entity को represent कर सकता है। एक ही name के साथ एक ही scope में दो variables नहीं हो सकते हैं। namespaces का उपयोग करके, हम एक ही नाम वाले दो variables या Member Function (मेंबर फंक्शन) बना सकते हैं।

    Example:-


        // Here we can see that more than one variables
        // are being used without reporting any error.
        // That is because they are declared in the
        // different namespaces and scopes.
        #include <iostream>
        using namespace std;

        // Variable created inside namespace
        namespace first
        {
            int val = 500;
        }

        // Global variable
        int val = 100;

        int main()
        {
            // Local variable
            int val = 200;

            // These variables can be accessed from
            // outside the namespace using the scope
            // operator ::
            cout << first::val << '\n';

            return 0;
        }

    Output:-

    500

    Definition and Creation

    Namespaces(नेमस्पेस) हमें named entities को Group करने की अनुमति देता है जो global scope को narrower scopes में रखते हैं, जिससे उन्हें namespace scope दिया जाता है । यह organize के elements को नामों से referred विभिन्न logical scopes में व्यवस्थित करने की अनुमति देता है। Namespace वह स्थान प्रदान करता है जहां हम identifier को define या declare कर सकते हैं अर्थात variable, method, classes

    • Namespace C++ में जोड़ा गया एक feature है जो कि C language में मौजूद नहीं है।
    • एक namespace एक declarative region है जो इसके अंदर identifiers (names of the types, function, variables) को एक scope प्रदान करता है।
    • समान नाम वाले Multiple namespace blocks की अनुमति है। उन blocks के भीतर सभी declarations named scope में declared की जाती हैं।

    Namespace की definition keyword namespace से शुरू होती है और उसके बाद namespace नाम निम्नानुसार है:

    namespace namespace_name 

            int x, y;         // code declarations where 
                                 // x and y are declared in
                                 // namespace_name's scope 
    }

    • Namespace की declarations केवल global scope में की जाती हैं हैं।
    • Namespace declarations को किसी अन्य namespace में nested किया जा सकता है।
    • Namespace declarations में access specifiers नहीं हैं। (Public or private)
    • Namespace की definition के closing brace के बाद semicolon देने की आवश्यकता नहीं है।
    • हम namespace की definition को कई units में split (विभाजित) कर सकते हैं।

    Defining a Namespace:

    एक namespace definition namespace keyword से शुरू होती है और उसके बाद namespace का नाम इस प्रकार है:


        namespace  namespace_name{
            // code declarations i.e. variable  (int a;)    
            method (void add();)
            classes ( class student{};)
        }

    यहाँ ध्यान दीजिए कि closing brace के बाद कोई semicolon (;) नहीं है। किसी Function या variable के namespace-enabled version को Call करने के लिए, namespace को निम्नानुसार call किया जाता हैं।


        namespace_name: :code;  // code could be variable , function or class.  

    Example:-


        // Let us see how namespace scope the entities including variable and functions:    

        #include <iostream>
        using namespace std;

        // first name space
        namespace first_space
        {
            void func()
            {
                cout << "Inside first_space" << endl;
            }
        }

        // second name space
        namespace second_space
        {
            void func()
            {
                cout << "Inside second_space" << endl;
            }
        }

        int main()
        {
            // Calls function from first name space.
            first_space::func();
            // Calls function from second name space.
            second_space::func();
            return 0;
        }

        // If we compile and run above code, this would produce the following result:
        // Inside first_space
        // Inside second_space

    Output:-

    Inside first_space
    Inside second_space

    Post a Comment

    0 Comments