Encapsulation in C++ - Definition, Advantages and Examples
1. Introduction to Encapsulation in C++
Encapsulation is one of the fundamental concepts of Object-Oriented Programming (OOP). It refers to wrapping data members and the member functions that operate on that data into a single unit called a class. Encapsulation also allows us to control access to data using access specifiers such as private, public and protected. For example, a Bank Account class can contain account details and functions such as deposit, withdraw and check balance in one unit.
Encapsulation, Object-Oriented Programming (OOP) का एक fundamental concept है। इसका अर्थ data members और उन पर कार्य करने वाले member functions को एक single unit अर्थात class में combine करना है। Encapsulation हमें private, public और protected जैसे access specifiers की सहायता से data की accessibility को control करने की सुविधा भी देता है। उदाहरण के लिए, एक Bank Account class में account details और deposit, withdraw तथा check balance जैसे functions को एक ही unit में रखा जा सकता है।
2. Need for Encapsulation in C++
Encapsulation helps organize program components and provides controlled access to an object's data.
C++ में Encapsulation program components को व्यवस्थित करने और object के data तक controlled access provide करने में मदद करता है।
The main purposes of encapsulation are:
Data Organization: Combines related data and functions into a single class.
Controlled Access: Controls how data members are accessed and modified.
Data Protection: Helps prevent unauthorized or accidental changes to internal data.
Maintainability: Makes programs easier to modify and maintain.
Modularity: Divides a program into independent classes.
Encapsulation के मुख्य उद्देश्य:
Data Organization: Related data और functions को एक class में combine करता है।
Controlled Access: Data members को access और modify करने के तरीके को control करता है।
Data Protection: Internal data में unauthorized या accidental changes को रोकने में मदद करता है।
Maintainability: Programs को modify और maintain करना आसान बनाता है।
Modularity: Program को independent classes में divide करता है।
3. How to Achieve Encapsulation in C++
In C++, encapsulation is achieved using classes. Data members are often declared as private, while public member functions are provided to access or modify the data in a controlled manner.
C++ में Encapsulation को classes की सहायता से achieve किया जाता है। Data members को अक्सर private declare किया जाता है, जबकि data को controlled तरीके से access या modify करने के लिए public member functions provide किए जाते हैं।
Syntax
class ClassName{private:// Data Memberspublic:// Member Functions};
In this structure:
privatemembers cannot normally be accessed directly from outside the class.publicmember functions provide the interface to interact with the object's data.
इस structure में:
privatemembers को class के बाहर से normally directly access नहीं किया जा सकता।publicmember functions object के data के साथ interact करने के लिए interface provide करते हैं।
4. Example of Encapsulation in C++
The following example demonstrates encapsulation by combining data members and member functions in a class.
निम्न example में data members और member functions को एक class में combine करके Encapsulation को demonstrate किया गया है।
C++ Program
#include <iostream>using namespace std;class Student{private:int rollNo;float marks;public:void setData(int r, float m){rollNo = r;marks = m;}void displayData(){cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks;}};int main(){Student s1;s1.setData(101, 85.5);s1.displayData();return 0;}
Output:
Roll Number: 101Marks: 85.5
Explanation:
Studentis a class that combines data and functions.rollNoandmarksare private data members.setData()assigns values to the data members.displayData()displays the stored values.The object
s1interacts with the private data through public member functions.
व्याख्या:
Studentएक class है जिसमें data और functions को combine किया गया है।rollNoऔरmarksprivate data members हैं।setData()data members को values assign करता है।displayData()stored values को display करता है।s1object public member functions के माध्यम से private data के साथ interact करता है।
5. Real-Life Example of Encapsulation – Bank Account
A bank account is a common real-life example of encapsulation. Account balance is kept private, and operations such as deposit, withdrawal and balance checking are provided through public functions.
Bank Account, Encapsulation का एक सामान्य real-life example है। Account balance को private रखा जाता है और deposit, withdrawal तथा balance checking जैसे operations public functions के माध्यम से provide किए जाते हैं।
C++ Program
#include <iostream>using namespace std;class BankAccount{private:double balance = 0;public:void deposit(double amount){if (amount > 0){balance += amount;cout << "Amount Deposited: " << amount << endl;}else{cout << "Invalid Deposit Amount!" << endl;}}void withdraw(double amount){if (amount <= 0){cout << "Invalid Withdrawal Amount!" << endl;}else if (amount <= balance){balance -= amount;cout << "Amount Withdrawn: " << amount << endl;}else{cout << "Insufficient Balance!" << endl;}}void displayBalance(){cout << "Available Balance: " << balance << endl;}};int main(){BankAccount account;account.deposit(5000);account.withdraw(1500);account.displayBalance();return 0;}
Output:
Amount Deposited: 5000Amount Withdrawn: 1500Available Balance: 3500
Explanation:
balanceis a private data member.deposit()adds money after checking that the amount is positive.withdraw()checks the amount and available balance before withdrawal.displayBalance()displays the current balance.The balance cannot be modified directly through the object.
व्याख्या:
balanceएक private data member है।deposit()amount positive होने पर balance में money add करता है।withdraw()पैसे निकालने से पहले amount और available balance check करता है।displayBalance()current balance display करता है।Object की सहायता से balance को directly modify नहीं किया जा सकता।
6. Advantages of Encapsulation in C++
Encapsulation provides several advantages in software development.
Software development में Encapsulation के कई फायदे हैं।
Data Protection: Internal data can be protected from direct access.
Data Protection: Internal data को direct access से सुरक्षित रखा जा सकता है।
Controlled Data Access: Data can be accessed or modified through public member functions.
Controlled Data Access: Public member functions के माध्यम से data को access या modify किया जा सकता है।
Improved Maintainability: Changes in internal implementation can be made without changing the public interface.
Improved Maintainability: Public interface को बदले बिना internal implementation में changes किए जा सकते हैं।
Code Reusability: Classes can be reused in different programs.
Code Reusability: Classes को अलग-अलग programs में reuse किया जा सकता है।
Better Modularity: Related data and functions are grouped together.
Better Modularity: Related data और functions को एक साथ group किया जाता है।
7. Difference Between Encapsulation and Data Hiding
Encapsulation and Data Hiding are related concepts in OOP, but they are not exactly the same.
Encapsulation और Data Hiding, OOP के related concepts हैं, लेकिन दोनों बिल्कुल समान नहीं हैं।
| Basis | Encapsulation | Data Hiding |
|---|---|---|
| Meaning | Wrapping data and related functions into a single unit | Restricting direct access to internal data |
| Main purpose | Organizes data and functions | Protects internal data |
| Focus | Combining data and methods | Controlling data accessibility |
| Implementation | Achieved using classes | Commonly achieved using private and protected |
| Relationship | A broader OOP concept | An important aspect supported by encapsulation |
Encapsulation is an important principle of Object-Oriented Programming in C++. It combines data members and member functions into a single unit called a class. By using access specifiers and public member functions, encapsulation provides controlled access to internal data and helps develop modular, maintainable and reliable programs.
C++ में Encapsulation Object-Oriented Programming का एक महत्वपूर्ण principle है। यह data members और member functions को एक single unit अर्थात class में combine करता है। Access Specifiers और public member functions का उपयोग करके Encapsulation internal data तक controlled access provide करता है तथा modular, maintainable और reliable programs develop करने में मदद करता है।
Comments
Post a Comment