0% found this document useful (0 votes)
21 views12 pages

DownloadClassSessionFile 12

This document discusses encapsulation and data abstraction in C++. It explains that encapsulation is achieved through classes and access specifiers, which allow hiding data members and controlling access through member functions. This ensures data integrity and security. Data abstraction simplifies usage by focusing on essential features and hiding implementation details, promoting code simplicity and maintainability. Both concepts are demonstrated through examples of classes that abstract real-world entities like bank accounts and points.

Uploaded by

snn8hddmmx
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)
21 views12 pages

DownloadClassSessionFile 12

This document discusses encapsulation and data abstraction in C++. It explains that encapsulation is achieved through classes and access specifiers, which allow hiding data members and controlling access through member functions. This ensures data integrity and security. Data abstraction simplifies usage by focusing on essential features and hiding implementation details, promoting code simplicity and maintainability. Both concepts are demonstrated through examples of classes that abstract real-world entities like bank accounts and points.

Uploaded by

snn8hddmmx
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/ 12

ENCAPSULATION, DATA

ABSTRACTION IN C++
16/10/2023
ENCAPSULATION IN C++

Encapsulation is one of the four fundamental principles of Object-Oriented


Programming (OOP), along with inheritance, polymorphism, and
abstraction. It is a key concept that helps in creating robust and
maintainable software by hiding the internal details of an object and
exposing a controlled interface to interact with it. In C++, encapsulation can
be achieved using classes and access specifiers.

10/26/2023 2
UNDERSTANDING ENCAPSULATION IN C++

Encapsulation is often described as "data hiding." It means that the internal


representation of an object is hidden from the outside world, and access to
that data is restricted to well-defined methods (member functions) of the
class. This ensures that the data remains in a consistent state and is not
directly manipulated by external code, which can lead to unexpected side
effects and errors.

10/26/2023 3
CLASSES IN C++
In C++, classes are the primary means of implementing encapsulation. A class is a
user-defined data type that combines data members (attributes or variables) and
member functions (methods) to operate on those attributes. Let's take a look at a
simple example:

10/26/2023 4
CLASSES IN C++

In this example, we have defined a Rectangle class with two private data members,
length and width. These data members are hidden from external access because
they are marked as private.

10/26/2023 5
ACCESS SPECIFIERS

Access specifiers in C++98 control the visibility of class members. There are three access
specifiers in C++:

public: Members declared as public are accessible from anywhere in the program. These are
the methods that provide the public interface to the class.

private: Members declared as private are not accessible from outside the class. They are
used to store the internal state of the object and should only be accessed or modified by the
class's member functions.

protected: Members declared as protected are similar to private members but have limited
access when dealing with inheritance. We won't delve into this in detail in C++98.
10/26/2023 6
ENCAPSULATION BENEFITS

Encapsulation provides several benefits:

Data Integrity: By encapsulating data and providing controlled access through methods, we can
ensure that data is in a valid and consistent state at all times.

Abstraction: Encapsulation allows us to abstract away the implementation details, making it easier
to understand and use classes.

Code Maintenance: It simplifies code maintenance by localizing changes within the class and not
affecting external code that uses the class.

Security: It enhances security by restricting access to sensitive data.


10/26/2023 7
DATA ABSTRACTION

Data abstraction is a crucial concept in Object-Oriented Programming (OOP) that

allows us to hide the complex implementation details of a class while providing a

simplified and clear interface for users of the class. In C++, data abstraction is

primarily achieved through the use of classes, access specifiers, and member

functions.

10/26/2023 8
THE ESSENCE OF DATA ABSTRACTION

Data abstraction is all about focusing on the essential features of an object while

hiding the non-essential details. It allows us to create user-defined data types

(classes) that represent real-world entities in a way that is intuitive and easy to work

with. By abstracting away the complexities of the internal implementation, data

abstraction promotes code simplicity and maintainability.

10/26/2023 9
CREATING ABSTRACT DATA TYPES IN C++

In this example, the BankAccount class encapsulates the data


members accountHolder and balance as private members.
Users of the class interact with the data through well-defined
member functions, such as deposit, withdraw, getBalance,
and getAccountHolder. The internal representation of the
bank account is abstracted away from the users.

10/26/2023 10
BENEFITS OF DATA ABSTRACTION

Data abstraction offers several advantages:

Simplicity: It simplifies the usage of classes by providing a clear and intuitive interface,
making it easier for programmers to work with complex data structures.

Maintenance: Abstracted data types are easier to maintain because changes to the
internal implementation do not affect the code that uses the class, as long as the
public interface remains unchanged.

Encapsulation: Data abstraction often goes hand in hand with encapsulation, which
ensures data integrity and controlled access to data.
10/26/2023 11
EXAMPLE OF DATA ABSTRACTION IN C++

Consider a simple example of a Point class that represents a


point in a 2D coordinate system:

In this example, the Point class abstracts the concept of a point


in a 2D space. It encapsulates the x and y coordinates as
private data members and provides public methods for
accessing and modifying these coordinates. External code can
create and manipulate points using this clear and controlled
interface.

10/26/2023 12

You might also like