Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 42

Access Specifiers: public, private, protected

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What are access specifiers?

Access specifiers decide who is allowed to use each member of a class. There are three: public, private and protected. They are how C++ enforces encapsulation — showing a safe interface while hiding the internals.

public

public members can be used from anywhere — inside the class, and by outside code holding an object:

C++
class Dog {
public:
    string name;
    void bark() { cout << name << " says Woof!\n"; }
};
int main() {
    Dog d;
    d.name = "Rex";     // OK: public
    d.bark();           // OK: public
}
Output:
Rex says Woof!

private

private members can be used only inside the class. Outside code cannot touch them directly:

C++
class Account {
private:
    double balance = 0;      // hidden
public:
    void deposit(double a) { balance += a; }
    double getBalance() { return balance; }
};
int main() {
    Account acc;
    acc.deposit(500);
    // acc.balance = 999;    // ERROR: private
    cout << acc.getBalance();  // 500
}
Output:
500

protected

protected is like private, but derived classes can still reach the member. It matters only with inheritance:

C++
class Animal {
protected:
    int legs = 4;      // hidden from outside, seen by subclasses
};
class Dog : public Animal {
public:
    void show() { cout << "Legs: " << legs; }  // OK
};

Getters and setters

The usual pattern is private data with public functions to read and change it safely — often called getters and setters:

C++
class Temperature {
    double celsius = 0;        // private
public:
    void setC(double c) { if (c >= -273.15) celsius = c; }  // validate
    double getC() { return celsius; }
};

The setter can reject invalid values — impossible if the data were public.

Default access

classstruct
Default accessprivatepublic

That default is the only real difference between class and struct.

Common mistakes

  • Forgetting that class members are private by default, then wondering why access fails.
  • Accessing a private member from outside the class.
  • Using protected when private would do — expose to subclasses only when needed.
  • Making all data public, throwing away encapsulation.
🏋️ Practice

Write a BankAccount class with a private balance, a deposit that rejects negative amounts, a withdraw that refuses to overdraw, and a getter. Try to change the balance directly and see the compiler stop you.

Summary

  • Access specifiers control who can use each member.
  • public: everywhere; private: same class only; protected: class + derived.
  • Class members are private by default; struct members public.
  • Private data with public getters/setters is the encapsulation pattern.
  • Setters can validate input, keeping the object consistent.

Access specifiers क्या हैं?

Access specifiers तय करते हैं कि class के हर member को कौन इस्तेमाल कर सकता है। तीन हैं: public, private और protected। ये वह तरीका हैं जिससे C++ encapsulation लागू करता है — एक सुरक्षित interface दिखाना जबकि internals छिपाना।

public

public members कहीं से भी इस्तेमाल हो सकते हैं — class के अंदर, और object रखने वाले बाहरी code से:

C++
class Dog {
public:
    string name;
    void bark() { cout << name << " says Woof!\n"; }
};
int main() {
    Dog d;
    d.name = "Rex";     // OK: public
    d.bark();           // OK: public
}
Output:
Rex says Woof!

private

private members केवल class के अंदर इस्तेमाल हो सकते हैं। बाहरी code उन्हें सीधे नहीं छू सकता:

C++
class Account {
private:
    double balance = 0;      // chhupa hua
public:
    void deposit(double a) { balance += a; }
    double getBalance() { return balance; }
};
int main() {
    Account acc;
    acc.deposit(500);
    // acc.balance = 999;    // ERROR: private
    cout << acc.getBalance();  // 500
}
Output:
500

protected

protected private जैसा है, पर derived classes फिर भी member तक पहुँच सकती हैं। यह केवल inheritance के साथ मायने रखता है:

C++
class Animal {
protected:
    int legs = 4;      // bahar se chhupa, subclasses ko dikhta
};
class Dog : public Animal {
public:
    void show() { cout << "Legs: " << legs; }  // OK
};

Getters और setters

सामान्य pattern है private data के साथ उसे सुरक्षित रूप से पढ़ने और बदलने को public functions — अक्सर getters और setters कहलाते हैं:

C++
class Temperature {
    double celsius = 0;        // private
public:
    void setC(double c) { if (c >= -273.15) celsius = c; }  // validate
    double getC() { return celsius; }
};

Setter अमान्य values अस्वीकार कर सकता है — असंभव अगर data public होता।

Default access

classstruct
Default accessprivatepublic

वह default class और struct के बीच एकमात्र असली अंतर है।

आम गलतियाँ

  • यह भूलना कि class members default रूप से private हैं, फिर सोचना कि access क्यों विफल होता है।
  • private member को class के बाहर से access करना।
  • private काफ़ी होने पर protected इस्तेमाल करना — ज़रूरत होने पर ही subclasses को उजागर करें।
  • सारे data को public बनाना, encapsulation फेंक देना।
🏋️ अभ्यास

एक BankAccount class लिखें जिसमें private balance, एक deposit जो ऋणात्मक राशि अस्वीकार करे, एक withdraw जो overdraw से मना करे, और एक getter हो। Balance को सीधे बदलने की कोशिश करें और देखें compiler आपको रोकता है।

सारांश

  • Access specifiers नियंत्रित करते हैं कि हर member को कौन इस्तेमाल कर सकता है।
  • public: हर जगह; private: केवल वही class; protected: class + derived।
  • Class members default रूप से private हैं; struct members public।
  • Private data के साथ public getters/setters encapsulation pattern है।
  • Setters input validate कर सकते हैं, object को सुसंगत रखते हुए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में access specifiers क्या हैं?
Access specifiers keywords हैं — public, private और protected — जो नियंत्रित करते हैं कि program के कौन-से हिस्से class के किसी member को इस्तेमाल कर सकते हैं। ये encapsulation के पीछे का तंत्र हैं, class को कुछ चीज़ें उजागर करने देते हुए जबकि दूसरी छिपी और सुरक्षित रखते हुए।
C++ में public, private और protected में क्या अंतर है?
public members कहीं से भी पहुँच योग्य हैं, private members केवल उसी class के भीतर से, और protected members class तथा उससे derived किसी भी class से। Private सबसे प्रतिबंधात्मक है; protected inheritance के लिए private और public के बीच बैठता है।
C++ class में default access specifier क्या है?
class में, members default रूप से private होते हैं, तो पहले specifier से पहले जो कुछ हो वह बाहर से access नहीं हो सकता। struct में, members default रूप से public होते हैं। यह default अंतर class और struct के बीच एकमात्र असली भेद है।
C++ में data members private क्यों होने चाहिए?
Data members को private बनाना आंतरिक अवस्था छिपाता है और बाहरी code को public functions से गुज़रने पर मजबूर करता है, जो input validate कर सकते और object को सुसंगत रख सकते हैं। यह encapsulation data की रक्षा करता है और आपको बाद में internals बदलने देता है बिना class इस्तेमाल करने वाले code को तोड़े।
C++ में private और protected में क्या अंतर है?
दोनों members को बाहरी code से छिपाते हैं, पर protected member derived classes के अंदर फिर भी पहुँच योग्य है, जबकि private member derived classes को भी नहीं पहुँच योग्य। आप protected तब इस्तेमाल करते हैं जब base class चाहती है कि subclasses किसी member तक सीधे पहुँचें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.