Access Specifiers: public, private, protected
Written and reviewed by Gagan Bhardwaj · 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:
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
}Rex says Woof!
private
private members can be used only inside the class. Outside code cannot touch them directly:
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
}500
protected
protected is like private, but derived classes can still reach the member. It matters only with inheritance:
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:
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
| class | struct | |
|---|---|---|
| Default access | private | public |
That default is the only real difference between class and struct.
Common mistakes
- Forgetting that class members are
privateby default, then wondering why access fails. - Accessing a
privatemember from outside the class. - Using
protectedwhenprivatewould do — expose to subclasses only when needed. - Making all data
public, throwing away encapsulation.
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 से:
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
}Rex says Woof!
private
private members केवल class के अंदर इस्तेमाल हो सकते हैं। बाहरी code उन्हें सीधे नहीं छू सकता:
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
}500
protected
protected private जैसा है, पर derived classes फिर भी member तक पहुँच सकती हैं। यह केवल inheritance के साथ मायने रखता है:
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 कहलाते हैं:
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
| class | struct | |
|---|---|---|
| Default access | private | public |
वह default class और struct के बीच एकमात्र असली अंतर है।
आम गलतियाँ
- यह भूलना कि class members default रूप से
privateहैं, फिर सोचना कि access क्यों विफल होता है। privatemember को 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 क्या हैं?
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 क्यों होने चाहिए?
C++ में private और protected में क्या अंतर है?
protected member derived classes के अंदर फिर भी पहुँच योग्य है, जबकि private member derived classes को भी नहीं पहुँच योग्य। आप protected तब इस्तेमाल करते हैं जब base class चाहती है कि subclasses किसी member तक सीधे पहुँचें।