✅ Definition:

Encapsulation ka matlab hai:

✅ Data (variables) aur ✅ Functions (methods) ko ek single unit (class) mein bind (jodna).

📦 Yeh data hiding ka bhi kaam karta hai, jisme object direct data ko access nahi kar pata, sirf functions ke through hi access hota hai.


🧠 Real-Life Example:

Socho tumhara mobile phone:

👉 That's Encapsulation!


🔧 C++ Example:

cpp
CopyEdit
#include<iostream>
using namespace std;

class Student {
  private:
    string name;
    int roll;

  public:
    void setData(string n, int r) {
        name = n;
        roll = r;
    }

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

int main() {
    Student s1;

    // s1.name = "Nikhil";  ❌ Not allowed (private)
    s1.setData("Nikhil", 101);  // ✅ allowed via public function
    s1.getData();               // Output: Name: Nikhil, Roll: 101

    return 0;
}


🔍 Key Features of Encapsulation:

Feature Explanation
✅ Data hiding Variables ko private banake direct access se bachana
✅ Control access public functions ke through hi data change karna
✅ Secure code Unauthorized access se data protect hota hai
✅ Easy maintenance Class ke andar logic fix ho to bahar kuch badalne ki jarurat nahi

🧱 How to Implement Encapsulation in C++:

Step Description
private All data members ko private bana do
public Sirf setters & getters (functions) ko public rakho

🎯 Benefits of Encapsulation: