DownloadClassSessionFile 12
DownloadClassSessionFile 12
ABSTRACTION IN C++
16/10/2023
ENCAPSULATION IN C++
10/26/2023 2
UNDERSTANDING ENCAPSULATION IN C++
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
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.
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
(classes) that represent real-world entities in a way that is intuitive and easy to work
10/26/2023 9
CREATING ABSTRACT DATA TYPES IN C++
10/26/2023 10
BENEFITS OF DATA ABSTRACTION
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++
10/26/2023 12