KEMBAR78
Lecture 10 OOP Encapsulation | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
10 views12 pages

Lecture 10 OOP Encapsulation

The document discusses encapsulation and data hiding in object-oriented programming, explaining how encapsulation combines data and functions within a class to protect data from outside access. It highlights the importance of making class members private and using public functions to access them, ensuring data security and flexibility in code. Additionally, it provides examples and advantages of encapsulation, emphasizing its role in maintaining the integrity of data within software applications.
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)
10 views12 pages

Lecture 10 OOP Encapsulation

The document discusses encapsulation and data hiding in object-oriented programming, explaining how encapsulation combines data and functions within a class to protect data from outside access. It highlights the importance of making class members private and using public functions to access them, ensuring data security and flexibility in code. Additionally, it provides examples and advantages of encapsulation, emphasizing its role in maintaining the integrity of data within software applications.
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

Object Oriented programming:

Encapsulation and Data Hiding


Course Code: CSC1102 &1103 Course Title: Introduction to Programming

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 10 Week No: 8(1X1.5) Semester:


Lecturer: Name & email
What is Encapsulation

Encapsulation is the method of combining the


data and functions inside a class. Thus the data
gets hidden from being accessed directly from
outside the class. This is similar to a capsule
where several medicines are kept inside the
capsule thus hiding them from being directly
consumed from outside. All the members of a
class are private by default, thus preventing
them from being accessed from outside the
class.
Data Hiding

 Data hiding is a technique especially practiced in object-oriented programming


(OOP).Data hiding is hiding the details of internal data members of an object.
 Data hiding is also known as Information hiding.
 Sometimes Data Hiding includes Encapsulation. Thus Data Hiding is heavily related to
Abstraction and Encapsulation.
 Data Hiding is the one most important OOP mechanism. Which is hide the details of
the class from outside of the class.
 The Class used by only a limited set of variables and functions, others are hidden by
the class.
An example

A Washing Machine and It's


Power Button:

What is the function that power


button does? Switches the machine
on (obviously). But did you ever
imagined the inside mechanism.
Doesn't matter unless it's
functioning well. That's
encapsulation. The object is
wrapped and inner details are
hidden. Only thing is that object can
be called and used.
Why Encapsulation
 Encapsulation is necessary to keep the details about an object hidden from the users
of that object.
 Details of an object are stored in its data members. This is the reason we make all the
member variables of a class private and most of the member functions public.
 Member variables are made private so that these cannot be directly accessed from
outside the class (to hide the details of any object of that class like how the data
about the object is implemented)
 Most member functions are made public to allow the users to access the data
members through those functions.
Advantages
 The main advantage of using of encapsulation is to secure the data from other methods, when we make a data
private then these data only use within the class, but these data not accessible outside the class.

 The major benefit of data encapsulation is the security of the data. It protects the data from unauthorized users
that we inferred from the above stated real-real problem.

 We can apply the concept of data encapsulation in the marketing and finance sector where there is a high
demand for security and restricted access of data to various departments.

 Encapsulation helps us in binding the data(instance variables) and the member functions(that work on the
instance variables) of a class.

 Encapsulation is also useful in hiding the data(instance variables) of a class from an illegal direct access.

 Encapsulation also helps us to make a flexible code which is easy to change and maintain.
Encapsulation
Role of Access modifiers:

 Access specifiers plays an important role in


implementing encapsulation in C++. The
process of implementing encapsulation can
How Encapsulation is achieved in a class: be sub-divided into two steps:

 Make all the data members private.  The data members should be labeled as
private using the private access specifiers.
 Create public setter and getter functions for
each data member in such a way that the  The member function which manipulates
set function set the value of data member the data members should be labeled as
and get function get the value of data public using the public access specifier.
member.
No encapsulation here.
Why???
ERROR!!!!!
A program to demonstrate Encapsulation
 In this example, the variable “Num” is private. Hence this
variable can be accessed only by the members of the same
class and is not accessible anywhere else. Hence outside
the classes will be unable to access this variable Which is
called data hiding.

 At the same time, “Square” class contains two other


methods namely “Get” and “Display” which has public
members. Here “Get” method just prints the value while
“Display” method prints the square of the value in the
variable “Num”.

 Here the class “Square” implements Data Encapsulation


concept by capsulation of the value in the variable “Num”
and thus allowing the user only to perform a restricted set
of operations on the hidden variable “Num”.
#include <iostream> Another Example of Data Abstraction
using namespace std;

class Adder{
public:
Data Encapsulation
int main( ) prevents users of the
// interface to outside world { classes from unintentionally
void addNum(int number) { Adder a;
total += number;
altering a critical piece of
}
a.addNum(10); data or gaining access to a
a.addNum(20); sensitive data item. This
// interface to outside world
a.addNum(30); programming strategy is
int getTotal() {
return total;
cout << "Totalcalled
“ encapsulation or
}; << a.getTotal();
data hiding
private: return 0;
// hidden data from outside world }
int total = 2; Output
}; Total 62
Thank You

You might also like