C++ Encapsulation In Hindi

📔 : C++ 🔗

data members को single Object में bind करना ही encapsulation कहलाता है। Encapsulation class में defined properties & methods को protect करने की protection mechanism होती है। Encapsulation mechanism की help से हम class members पर अपनी need के according access restrictions define करते हैं , जिससे data को बाहर से access नहीं किया जा सके।


Encapsulation का simply मतलब होता है , End User से Program की implementation details को hide करना। Encapsulation के द्वारा ही data को External access से protect किया जाता है। जिसमे class properties को private define करते हैं और public methods के through उन properties को update / modify करते हैं।

C++ Encapsulation Example

Generally encapsulation को हम class में setter और getter method define करके achieve करते हैं।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define a Employee class.
class Employee {
  // define salery as private.
  private:
    int salary;

  public:
    // define setter method to set salery.
    void setSalary(int sal) {
      salary = sal;
    }
    // define getter method to get salery.
    int getSalary() {
      return salary;
    }
};

int main() {
  // now firstly set salery then get that.
  Employee empObj;
  empObj.setSalary(100000);
  cout << empObj.getSalary();
  cout << endl;
  empObj.setSalary(200000);
  cout << empObj.getSalary();
  
  return 0;
} 
Output
100000
200000

C++ Advantages Of Encapsulation

  • Data Hiding And Abstraction

    Encapsulation use करके Unnecessary details, internal representation and implementation details को End user से hide किया जाता है , जिससे data structure को protect किया जा सके। किसी भी किसी class के members को private बनाकर इसके child class के access से रोका जा सकता है।

  • Reduces Code Complexity

    अब चूंकि class members को access modifiers का use करके एक single unit में bind करते हैं , जिससे code complexity भी reduce होती है।

  • Data Security

    Class properties/Methods को access modifiers का use करके need के according ही members का scope decide कर सकते हैं।

  • Code Reusability

    Code Reusability भी रहती है , same operation perform करने के लिए आपको code rewrite नहीं करना पड़ता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers