You are on page 1of 13

Encapsulation

Abstraction
Key Factors of OOP
The 3 key factors in OO programming
1. Inheritance
2. Encapsulation (data hiding)
3. Dynamic method binding
WHAT IS ENCAPSULATION?
Encapsulation is one of the fundamental concepts in object-
oriented programming (OOP).
In normal terms Encapsulation is defined as wrapping up of data
and information under a single unit.
In OOP, bundling or binding of data members and functions that
operate together inside a single class is called encapsulation.
 Encapsulation ensures better control of your data, because you can
change one part of the code without affecting other parts.
Characteristics of Encapsulation:
 Encapsulation keeps data and functions safe from outside interference and
misuse. Data encapsulation led to the important OOP concept of data hiding.
 OOP supports the properties of encapsulation and data hiding through the
creation of user-defined types, called classes.
 In encapsulation, all items defined in that specific class are kept private which
means they can be accessed only by that particular class, and not by any other
part of your program.
 To achieve encapsulation, access specifiers(private, protected, public) plays
vital role.
How Encapsulation is achieved in a class?

To do this:
 Make all the data members private.
 Create public setter and getter functions for each data
member in such a way that:
the set function set the value of data member and get function
get the value of data member.
EXAMPLE: int main() {

#include <iostream>   Employee myObj;


using namespace std;   myObj.setSalary(50000);
  cout << myObj.getSalary();
class Employee {
  private:   return 0;
    // Private attribute }
    int salary;

  public:
    // Setter
    void setSalary(int s) {
      salary = s;
    }
    // Getter
    int getSalary() {
      return salary;
    }
};
UNDERSTANDING THE CONCEPT…!
• The salary attribute is private, which have restricted access.

• The public setSalary() method takes a parameter (s) and assigns it to the
salary attribute (salary = s).

• The public getSalary() method returns the value of the private salary
attribute.

• Inside main(), we create an object of the Employee class. Now we can use
the setSalary() method to set the value of the private attribute to 50000.
Then we call the getSalary() method on the object to return the value.
(GOT IT?)
ENCAPSULATION?
 SUMMARY
The meaning of Encapsulation, is to make sure that "sensitive" data is
hidden from users.
To achieve this, you must declare class variables/attributes as private
(cannot be accessed from outside the class).
If you want others to read or modify the value of a private member,
you can provide public get and set methods.
WHAT IS ABSTRACTION?
Abstraction is one of the feature of OOP, where you show only
relevant details to the user and hide irrelevant details.
Which means displaying only essential information and hiding the
details.
For example, when you send an email to someone you just click send
and you get the message status only (sent, not sent, or draft etc),
what actually happens when you click send, how data is transmitted
over network to the recipient is hidden from you (because it is
irrelevant to you).
Types of Abstraction:
In the C++ language, there are two types of abstraction:

Control abstraction - In this type of abstraction implementation,


the process is hidden from the user.
Data abstraction - Data abstraction always hides the information
about the data in the program.
Data abstraction is further caterorized into 2 types:
(i) Abstraction using Classes:
A ‘Class’ can decide which data member will be visible to the outside
world and which is not. A class is used to group all the data members and
member functions into a single unit by using the access specifiers.
(ii) Abstraction in Header files:
One more type of abstraction in C++ can be header files. For example,
consider the pow() method present in math.h header file. Whenever we
need to calculate the power of a number, we simply call the function
pow() present in the math.h header file and pass the numbers as
arguments without knowing the underlying algorithm according to which
the function is actually calculating the power of numbers.
Program to illustrate data abstraction using header files:

#include <iostream>
#include <math.h>

using namespace std ;


int main ()
{
int a = 5 ;
int power = 2 ;
int result = pow ( a , power ) ;
cout << " Square of a = " << result << std::endl ;
return 0 ;
}

You might also like