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:

  1. Data Organization: Combines related data and functions into a single class.

  2. Controlled Access: Controls how data members are accessed and modified.

  3. Data Protection: Helps prevent unauthorized or accidental changes to internal data.

  4. Maintainability: Makes programs easier to modify and maintain.

  5. Modularity: Divides a program into independent classes.

Encapsulation के मुख्य उद्देश्य:

  1. Data Organization: Related data और functions को एक class में combine करता है।

  2. Controlled Access: Data members को access और modify करने के तरीके को control करता है।

  3. Data Protection: Internal data में unauthorized या accidental changes को रोकने में मदद करता है।

  4. Maintainability: Programs को modify और maintain करना आसान बनाता है।

  5. 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 Members
public:
// Member Functions
};

In this structure:

  • private members cannot normally be accessed directly from outside the class.

  • public member functions provide the interface to interact with the object's data.

इस structure में:

  • private members को class के बाहर से normally directly access नहीं किया जा सकता।

  • public member 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: 101
Marks: 85.5

Explanation:

  • Student is a class that combines data and functions.

  • rollNo and marks are private data members.

  • setData() assigns values to the data members.

  • displayData() displays the stored values.

  • The object s1 interacts with the private data through public member functions.

व्याख्या:

  • Student एक class है जिसमें data और functions को combine किया गया है।

  • rollNo और marks private data members हैं।

  • setData() data members को values assign करता है।

  • displayData() stored values को display करता है।

  • s1 object 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: 5000
Amount Withdrawn: 1500
Available Balance: 3500

Explanation:

  • balance is 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 के कई फायदे हैं।

  1. Data Protection: Internal data can be protected from direct access.

    Data Protection: Internal data को direct access से सुरक्षित रखा जा सकता है।

  2. Controlled Data Access: Data can be accessed or modified through public member functions.

    Controlled Data Access: Public member functions के माध्यम से data को access या modify किया जा सकता है।

  3. Improved Maintainability: Changes in internal implementation can be made without changing the public interface.

    Improved Maintainability: Public interface को बदले बिना internal implementation में changes किए जा सकते हैं।

  4. Code Reusability: Classes can be reused in different programs.

    Code Reusability: Classes को अलग-अलग programs में reuse किया जा सकता है।

  5. 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 हैं, लेकिन दोनों बिल्कुल समान नहीं हैं।

BasisEncapsulationData Hiding
MeaningWrapping data and related functions into a single unitRestricting direct access to internal data
Main purposeOrganizes data and functionsProtects internal data
FocusCombining data and methodsControlling data accessibility
ImplementationAchieved using classesCommonly achieved using private and protected
RelationshipA broader OOP conceptAn 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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)