Access control concepts (Public, Private, Protected) and difference among them

public, private और protected तय करते हैं कि एक class member को कौन छू सकता है: सब, सिर्फ़ class ख़ुद, या class plus इसके children, और class members default से private होते हैं।

9 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

कोई भी drawer खोल सकता था

CanteenTill class का Version 1 दिन का cash एक plain variable में रखता था। कहीं भी कोई भी code यह लिख सकता था:

till.cash = 0;

एक intern के buggy loop ने शाम 4 बजे बिल्कुल यही किया। कोई error नहीं, कोई warning नहीं, Rs 18,000 के records ग़ायब।

Fix "careful रहिए" नहीं है। Fix एक language feature है जो compiler को ऐसी line refuse करवाता है। C++ आपको तीन locks देता है: public, private, protected।

Theory

Counter, kitchen, cash drawer

Canteen में चलिए:

  • Counter public है: कोई भी वहाँ खड़ा होकर order दे सकता है।
  • Kitchen protected है: staff और family की franchise branches अंदर आ सकते हैं, customers नहीं।
  • Cash drawer private है: सिर्फ़ owner इसे खोलता है, staff सहित बाक़ी सब बाहर रहते हैं।

एक class का हर member इन तीन zones में से एक में रहता है, और compiler वह guard है जो कभी नहीं सोता।

Theory

तीनों specifiers, formally

एक access specifier तय करता है कि इसके बाद declare हुए members को कौन इस्तेमाल कर सकता है:

  • public: कहीं से भी accessible जहाँ object visible है, main() समेत।
  • private: सिर्फ़ class के अपने member functions के अंदर accessible। बाहर कोई नहीं, यहाँ तक कि एक derived class भी नहीं।
  • protected: बाहरी दुनिया के लिए private जैसा, पर derived classes access कर सकती हैं (यह inheritance में matter करता है, दो lessons आगे)।

एक specifier अगले specifier आने तक इसके बाद के हर member पर लागू होता है।

Practical

Drawer, ठीक से locked

#include <iostream>
using namespace std;

class CanteenTill {
private:
    double cash;          // the drawer: owner only

protected:
    double costPrice;     // the kitchen: this class + child classes

public:
    int ordersToday;      // the counter: anyone

    void addSale(double amount) {   // public gate to the private drawer
        cash = cash + amount;
    }
    double report() { return cash; }
};

int main() {
    CanteenTill till;
    till.ordersToday = 1;   // ok: public
    till.addSale(30.0);     // ok: public function touches private cash
    // till.cash = 0;       // ERROR: 'cash' is private
    return 0;
}

At a glance

कौन क्या access कर सकता है

से Access किया गयाpublicprotectedprivate
वही class के functionsहाँहाँहाँ
Derived class के functionsहाँहाँनहीं
बाहर (main, other code)हाँनहींनहीं

Think first

Compiler का अंदाज़ा लगाइए

ऊपर वाली class इस्तेमाल करते हुए, main() यह run करता है:

cout << till.cash;

tap करने से पहले: compile error या output? और अगर यह एक error है, वह एक-line fix क्या है जो cash को private रखता है?

Show the answer

Compile error: कुछ ऐसा जैसे 'cash' is private within this context। Fix है उस public gate से गुज़रना जो class पहले से देती है: cout << till.report();। यही encapsulation का पूरा pattern है: private data, public functions जो access mediate करते हैं। Drawer बंद रहता है; counter इसके बारे में सवालों के जवाब देता है।

Quiz

`class CanteenTill { double cash; ... }` के top पर एक member बिना किसी specifier के declare किया गया है। cash को कौन access कर सकता है?

  1. सिर्फ़ class के अपने member functions: class members default से private होते हैं
  2. सब: members default से public होते हैं
  3. Class और इसके children: members default से protected होते हैं
  4. कोई भी नहीं जब तक एक specifier न लिखा जाए
Show the answer

सिर्फ़ class के अपने member functions: class members default से private होते हैं

एक class में, पहले specifier से पहले सब कुछ private होता है। मशहूर twin fact: एक struct में, default public होता है। वह class-बनाम-struct default एक pyara 2-mark सवाल है। Option B struct का व्यवहार describe करता है, option C का default C++ में exist ही नहीं करता, और option D imaginary है: members को हमेशा कोई access level मिलता है।

Watch out

Default trap

Exams को पसंद है: "C++ में class और struct का फ़र्क़ क्या है?" Expected answer बिल्कुल एक line है: class members default से private होते हैं, struct members default से public होते हैं। बाक़ी सब कुछ उनके बारे में एक जैसा है।

दूसरा trap: protected तब तक बेकार लगता है जब तक inheritance नहीं आती। इसे किसी answer में "private जैसा" मत कहिए: एक derived class के लिए वे अलग व्यवहार करते हैं, और वही फ़र्क़ मुद्दा है।

Theory

यह encapsulation की muscle है

अगले lessons concept को नाम देते हैं: data hiding और encapsulation। नोटिस कीजिए कि वे सिर्फ़ इसलिए possible हैं क्योंकि ये specifiers exist करते हैं: private के बिना, "hiding" एक compiler-enforced rule की जगह एक polite request होती। बाद में जब आप किसी job interview में getters और setters लिखेंगे, आप बिल्कुल यही table apply कर रहे होंगे।

Summary

Key takeaways

  • public: कहीं से भी accessible; private: सिर्फ़ class के अपने functions; protected: class plus derived classes।
  • एक specifier अगले specifier तक इसके बाद के सभी members को govern करता है।
  • Class default private है; struct default public है (classic 2-marker)।
  • बाहरी code private data को सिर्फ़ public member functions (getters/setters) के through छूता है।
  • protected inheritance में अपना काम कमाता है: children इसे इस्तेमाल कर सकते हैं, बाहरी नहीं।
  • Memory hook: counter, kitchen, cash drawer।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Encapsulation and inheritance

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati