0% found this document useful (0 votes)
6 views3 pages

Encapsulation

Encapsulation is a key principle of object-oriented programming that combines data and methods within a class, enhancing code organization and data protection. It enforces data hiding and access control through public methods, allowing safe data manipulation. Additionally, encapsulation promotes code reusability, making it easier to integrate into various projects.

Uploaded by

aarchie077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Encapsulation

Encapsulation is a key principle of object-oriented programming that combines data and methods within a class, enhancing code organization and data protection. It enforces data hiding and access control through public methods, allowing safe data manipulation. Additionally, encapsulation promotes code reusability, making it easier to integrate into various projects.

Uploaded by

aarchie077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Encapsulation

Encapsulation is a fundamental pillar of object-


oriented programming (OOP). It's a powerful tool for
structuring and organizing code, improving
maintainability, and protecting data integrity.
>> It's a fundamental concept of object-oriented
programming (OOP) that bundles data and the
methods that operate on that data within a single
unit, known as a class.
>> This allows for better organization, control over
access, and increased flexibility in code design.
CODE
#include <iostream>
using namespace std;

class BankAccount {
OUTPU
T
private:
int accountNumber;
double balance;
public:
Account Number: 123
BankAccount(int accNum, double bal) : accountNumber(accNum), balance(bal) {}
int getAccountNumber() { return accountNumber; } Initial Balance: 1000
double getBalance() { return balance; } Updated Balance: 150
void setBalance(double bal) { balance = bal; }
};

int main() {
BankAccount account(12345, 1000.0);
cout << "Account Number: " << account.getAccountNumber() << endl;
cout << "Initial Balance: " << account.getBalance() << endl;
account.setBalance(1500.0);
cout << "Updated Balance: " << account.getBalance() << endl;
return 0;
}
DEMONSTRATION OF
ENCAPSULATION
Data Hiding
Encapsulation hides the internal implementation details of the class. The
private members are not directly accessible from outside the class, enforcing
data protection and preventing unauthorized access.

Data Access Control


Public methods provide controlled access to the encapsulated data. They act as
gatekeepers, ensuring that data modifications are performed in a safe and
controlled manner.
Code Reusability
The EncapsulatedObject class can be easily reused in other parts of
the program or in different projects. This reduces development time
and effort, leading to more efficient software development.

You might also like