0% found this document useful (0 votes)
17 views

Information Hiding and Encapsulation

Hiding Info

Uploaded by

k13913703
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Information Hiding and Encapsulation

Hiding Info

Uploaded by

k13913703
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

In Object-Oriented Programming (OOP), hiding information and encapsulation are foundational concepts

designed to improve code structure and security, as explained by Bjarne Stroustrup in *The C++
Programming Language*. Here's a breakdown of both:

Information Hiding:
Information hiding is a principle where the internal details of a class (like its data and internal methods)
are hidden from the outside world. It restricts access to certain parts of an object, exposing only what is
necessary through a well-defined interface.

Benefits of Information Hiding:


1. Security and Integrity: By restricting access to internal data, you prevent external code from
modifying it in unintended ways, which helps maintain data integrity.

2. Modularity: Since internal details are hidden, you can change the internal workings of a class without
affecting the code that uses it, leading to more maintainable and modular code.

3. Simplicity: Users of a class only need to know about its public interface, making it easier to
understand and use without knowing the complexities behind it.

Banking System (Information Hiding)

 Imagine an online banking application where users can check their


account balances, deposit money, and withdraw funds.
 In such a system, the user’s bank account balance is sensitive
information that should not be directly accessible or modifiable by
external entities.
 The BankAccount class could make the balance private. It would only
allow access to this balance through controlled methods ( deposit,
withdraw, getBalance) that handle the logic for modifying the balance and
checking conditions.

Encapsulation

Encapsulation is when you put data (attributes) and the functions


(methods) that work with that data into one unit, called a class. This helps
keep things organized. Usually, the data is kept private so that it can’t be
changed directly from outside the class. Instead, public methods are used
to control how others can access or change the data.

Advantages of Encapsulation:
1. Control over Data: By using private access specifiers, encapsulation provides control over which parts
of the data can be accessed or modified externally, improving data security.

2. Code Maintenance: Encapsulation simplifies maintenance by keeping data and functions together,
reducing code duplication and dependencies.

3. Flexibility: Internal implementation changes can be made without affecting the interface provided to
the user, which promotes flexibility and reusability.

E-commerce Shopping Cart (Encapsulation)

 In an e-commerce application, a shopping cart is a perfect example of


encapsulation. The ShoppingCart class can contain a list of items that a
user wants to purchase.
 The items themselves, along with methods for adding, removing, or
updating items, are encapsulated within the ShoppingCart class.
 The class might provide methods like addItem, removeItem,
calculateTotal, and applyDiscount, but it won’t allow direct access to the
list of items or the total cost itself.

In C++, encapsulation is typically implemented using access specifiers like `public`, `private`, and
`protected` to control access to class members.

You might also like