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.

Frequently Asked Questions

What are access specifiers in C++?
Access specifiers are keywords — 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?
In a 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++?
Making data members private hides the internal state and forces outside code to go through public functions, which can validate input and keep the object consistent. This encapsulation protects the data and lets you change the internals later without breaking code that uses the class.
What is the difference between private and protected in C++?
Both hide members from outside code, but a 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 से:

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 को सुसंगत रखते हुए।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.