✅ Definition:

Access Specifiers decide karte hain ki class ke members (variables/functions) ko kahaan se access kar sakte ho – class ke andar ya bahar se.


🔑 3 Main Access Specifiers:

Specifier Access Location Description (Hinglish)
public Har jagah access allowed Kahin se bhi access kar sakte ho
private Sirf class ke andar Bahar se access nahi kar sakte
protected Class + Inherited classes Inheritance mein use hota hai (next level)

🔧 Syntax:

cpp
CopyEdit
class MyClass {
  public:
    int a;         // Public member

  private:
    int b;         // Private member

  protected:
    int c;         // Protected member
};


🧪 Example:

cpp
CopyEdit
#include<iostream>
using namespace std;

class Student {
  public:
    string name;

  private:
    int roll;

  public:
    void setRoll(int r) {
        roll = r;
    }

    void display() {
        cout << "Name: " << name << ", Roll: " << roll << endl;
    }
};

int main() {
    Student s1;
    s1.name = "Nikhil";    // ✅ public, allowed
    // s1.roll = 101;      ❌ private, not allowed directly
    s1.setRoll(101);       // ✅ indirectly through function
    s1.display();

    return 0;
}

🟢 Output:

yaml
CopyEdit
Name: Nikhil, Roll: 101


🛡️ Access Specifiers Summary Table:

Specifier Class ke andar Bahar se (object) Inheritance mein
public ✅ Yes ✅ Yes ✅ Yes
private ✅ Yes ❌ No ❌ No
protected ✅ Yes ❌ No ✅ Yes (child class)

🎯 Use Case Guide:

Situation Use this
Function ya variable sabko dikhana ho public
Sirf class ke andar use karna ho private
Sirf class aur uske child classes ke liye protected