Encapsulation in C++ in Hindi – एनकैप्सुलेशन क्या है?

Hello दोस्तों! आज हम इस post में Encapsulation in C++ in Hindi (सी.प्लस.प्लस में एनकैप्सुलेशन क्या है?) के बारें में पढेंगे और इसके example को भी देखेंगे. आप इसे पूरा पढ़िए आपको यह आसानी से समझ में आ जायेगा. तो चलिए शुरू करते है.

Encapsulation in C++ in Hindi

C++ में, encapsulation एक प्रक्रिया है जो data members और functions को एक single unit में एक साथ combine करके रखता है.

दूसरे शब्दों में कहें तो, “Data और functions को एक साथ एक class के अंदर रखना encapsulation कहलाता है.”

Encapsulation के कारण हम data को direct access नहीं कर सकते और इसे class के functions के द्वारा ही access किया जा सकता है. यह OOPs (object-oriented programming) का सबसे महत्वपूर्ण feature है.

Encapsulation की वजह से data सुरक्षित रहता है और उसका गलत इस्तेमाल नहीं होता.

उदाहरण के लिए – यहाँ पर हम एक real life example को देखते हैं. माना कि एक company में दो teams हैं. और वो एक software का निर्माण कर रहे हैं. पहली team को दूसरे team के data की जरूरत है. लेकिन पहली team उस data को direct access नहीं कर सकती. क्योंकि उनके पास data को access करने की permission नहीं है. इसके लिए पहली team को दूसरी team के leader से बात करके permission लेनी होगी. तभी जाके वो data को access कर सकते हैं. इसी को Encapsulation कहते है.

एक Class में Encapsulation को कैसे achieve (प्राप्त) करते हैं?

इसे निम्नलिखित प्रकार से प्राप्त कर सकते हैं:-

  • सभी data members को private करके.
  • सभी data members के लिए public setter और getter functions को create करके.

Types of Encapsulation in C++

C++ में, encapsulation तीन प्रकार का होता है:-

  1. Member Variable Encapsulation – इस encapsulation में, सभी data members को private के रूप में declare किया जाता है.
  2. Function encapsulation – इसमें, कुछ member functions को private किया जाता है. Constructor को public रखा जाता है.
  3. Class encapsulation  – इसमें, सभी class को private रखा जाता है. इसे ज्यादातर nested classes के दौरान किया जाता है.

Encapsulation का सरल program –

#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
test(int a)
{
x =a;
}
int get()
{
return x;
}
};
int main()
{
test a(8);
cout<<"The Number is: "<<a.get();
return 0;
}

इसका आउटपुट:-
The Number is: 8

ऊपर दिए गये program में, हमारे पास एक private member X है. इसे हम main() फंक्शन से access नहीं कर सकते है और इसे access करने के लिए हमने class test के object को create किया है.

Advantage of Encapsulation – इसके लाभ

इसके लाभ निम्नलिखित हैं:-

  • C++ में, encapsulation की मदद से हम सम्बन्धित data और functions को एक साथ रख सकते हैं. जिससे हमारा कोड clean रहता है और code को पढने में आसानी होती है.
  • Getter और Setter functions के द्वारा हम अपने class members को read-only या write-only एक्सेस प्रदान कर सकते है.
  • इसके द्वारा data hiding और data abstraction को प्राप्त किया जा सकता है.

references:-
https://beginnersbook.com/2017/09/cpp-encapsulation/

Encapsulation in C++ in Hindi

निवेदन:- अगर आपके लिए यह पोस्ट उपयोगी रही हो तो इसे अपने friends और classmates के साथ अवश्य share कीजिये. और आपके इस article से सम्बन्धित कोई सवाल हो तो उसे नीचे comment करके बताइए.

मैं आपके लिए ऐसी ही पोस्ट लाते रहता हूँ. अगर आपका कोई suggestion है तो आप उसे भी बता सकते है. जिससे कि इस वेबसाइट में improvement हो सकें. Thanks.

5 thoughts on “Encapsulation in C++ in Hindi – एनकैप्सुलेशन क्या है?”

Leave a Comment