C++ Access Modifiers - W3 Coding Club

    Access Modifiers

    Access modifiers का उपयोग Object-Oriented Programming(ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग) के एक महत्वपूर्ण पहलू को लागू करने के लिए किया जाता है जिसे Data Hiding(डेटा छिपाने) के रूप में जाना जाता है ।

     Real-life example:- 

    Research और analysis wing (R&AW) के पास प्रमुख 10 members हैं, जो national security(राष्ट्रीय सुरक्षा) के संबंध में sensitive(संवेदनशील) confidential information(गोपनीय जानकारी) के regarding कार्य करते हैं।

    अब हम इन core members(कोर सदस्यों) को data members या किसी class के member functions से correlated (सहसंबंधित) कर सकते हैं जो बदले में R&A wing से संबंधित हो सकते हैं।

    ये 10 members सीधे अपने wing (class ) से confidential information को directly access कर सकते हैं, लेकिन इन 10 members के अलावा कोई भी सीधे इस information को directly access नहीं कर सकता है।

    किसी Class में Access Modifiers या Access Specifiers(स्पेसिफायर) का उपयोग Class के members को accessibility assign करने के लिए किया जाता है। Access Modifiers या Access Specifiers(स्पेसिफायर) Class के members पर outside functions द्वारा directly access न करने के लिए कुछ प्रतिबंध लगाता है।

    C++ में 3 प्रकार Access Modifiers या Access Specifiers(स्पेसिफायर) हैं:

    1. Public
    2. Private
    3. Protected

    Note:- यदि हम class के अंदर members के लिए कोई Access Modifiers specify नहीं करते हैं तो default (डिफ़ॉल्ट) रूप से members के लिए Access Modifiers Private होगा ।

    1. Public:

    public specifier के द्वारा declared class के सभी data members और member functions को अन्य class और function के द्वारा भी Access किया जा सकता है। किसी class के पब्लिक Members को उस Class के Object के साथ direct member access operator (.) का उपयोग करके Program में कहीं से भी Access किया जा सकता है।

    Example #01:-


        // C++ program to demonstrate public
        // access modifier

        #include <iostream>
        using namespace std;

        // class definition
        class Circle
        {
        public:
            double radius;

            double compute_area()
            {
                return 3.14 * radius * radius;
            }
        };

        // main function
        int main()
        {
            Circle obj;

            // accessing public datamember outside class
            obj.radius = 5.5;

            cout << "Radius is: " << obj.radius << "\n";
            cout << "Area is: " << obj.compute_area();
            return 0;
        }

    Output:-

    Radius is: 5.5 
    Area is: 94.985

    Example:-


        #include <iostream>
        using namespace std;
        class Line
        {
        public:
            double length;
            void setLength(double len);
            double getLength(void);
        };

        // Member functions definitions
        double Line::getLength(void)
        {
            return length;
        }

        void Line::setLength(double len)
        {
            length = len;
        }

        // Main function for the program
        int main()
        {
            Line line;

            // set line length
            line.setLength(6.0);
            cout << "Length of line : " << line.getLength() << endl;

            // set line length without member function
            line.length = 10.0; // OK: because length is public
            cout << "Length of line : " << line.length << endl;

            return 0;
        }

    Output:-

    Length of line : 6 
    Length of line : 10

    2. Private:-

    एक Private member variable या function का उपयोग Class बाहर से नहीं किया जा सकता है। केवल उसी class और friend functions ही private members को access कर सकते हैं। By default एक Class के सभी members private होते हैं। 
    उदाहरण के लिए निम्न class Box में width  एक private Member है, जिसका अर्थ है कि जब तक आप किसी Member को Label  नहीं करते, तब तक इसे एक private Member माना जाएगा -
    class Box {
    double width;

    public:
    double length;
    void setWidth( double wid );
    double getWidth( void );
    };

    Example #03:-

    Practically , हम private section में Data और public section में उस से related functions को define करते हैं ताकि उन्हें Class के बाहर से call किया जा सके जैसा कि निम्नलिखित कार्यक्रम में दिखाया गया है।


        #include <iostream>
        using namespace std;
        class Box
        {
        public:
            double length;
            void setWidth(double wid);
            double getWidth(void);

        private:
            double width;
        };

        // Member functions definitions
        double Box::getWidth(void)
        {
            return width;
        }

        void Box::setWidth(double wid)
        {
            width = wid;
        }

        // Main function for the program
        int main()
        {
            Box box;

            // set box length without member function
            box.length = 10.0; // OK: because length is public
            cout << "Length of box : " << box.length << endl;

            // set box width without member function
            // box.width = 10.0; // Error: because width is private
            box.setWidth(10.0); // Use member function to set it.
            cout << "Width of box : " << box.getWidth() << endl;

            return 0;
        }

    Output:-

    Length of box : 10
    Width of box : 10 

    Example #04:-


        // C++ program to demonstrate private
        // access modifier

        #include <iostream>
        using namespace std;
        class Circle
        {
            // private data member
        private:
            double radius;

            // public member function
        public:
            double compute_area()
            { // member function can access private
                // data member radius
                return 3.14 * radius * radius;
            }
        };

        // main function
        int main()
        {
            // creating object of the class
            Circle obj;

            // trying to access private data member
            // directly outside the class
            obj.radius = 1.5;

            cout << "Area is:" << obj.compute_area();
            return 0;
        }

    Output:-

    blog.cpp: In function 'int main()':
    blog.cpp:30:13: error: 'double Circle::radius' is private within this context
             obj.radius = 1.5;
                 ^~~~~~
    blog.cpp:11:16: note: declared private here
             double radius;

    Explain Example:-

    उपरोक्त Program का Outpit एक compile time error है क्योंकि हमें सीधे Class के बाहर किसी class के Private data member को Access करने की अनुमति नहीं है। फिर भी obj.radius को Access करने का प्रयास किया जाता है इसलिए , radius एक private data member होने के नाते हमें एक compile time error प्राप्त होती है।

    हालाँकि, हम Class के public member functions का उपयोग करके indirectly किसी classs के private data members को access कर सकते हैं।


        // C++ program to demonstrate private
        // access modifier

        #include <iostream>
        using namespace std;

        class Circle
        {
            // private data member
        private:
            double radius;

            // public member function
        public:
            void compute_area(double r)
            { // member function can access private
                // data member radius
                radius = r;

                double area = 3.14 * radius * radius;

                cout << "Radius is: " << radius << endl;
                cout << "Area is: " << area;
            }
        };

        // main function
        int main()
        {
            // creating object of the class
            Circle obj;

            // trying to access private data member
            // directly outside the class
            obj.compute_area(1.5);

            return 0;
        }

    Post a Comment

    0 Comments