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.
Frequently Asked Questions
What are access specifiers in C++?
public, private and protected — that control which parts of a program can use a class member. They are the mechanism behind encapsulation, letting a class expose some things while keeping others hidden and protected.What is the difference between public, private and protected in C++?
public members are accessible from anywhere, private members only from within the same class, and protected members from the class and any classes derived from it. Private is the most restrictive; protected sits between private and public for inheritance.What is the default access specifier in a C++ class?
class, members are private by default, so anything before the first specifier cannot be accessed from outside. In a struct, members are public by default. This default difference is the only real distinction between class and struct.Why should data members be private in C++?
What is the difference between private and protected in C++?
protected member is still accessible inside derived classes, while a private member is not accessible even to derived classes. You use protected when a base class wants subclasses to reach a member directly.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 को सुसंगत रखते हुए।