0% found this document useful (0 votes)
13 views9 pages

Chap 0201

Uploaded by

udyadav430
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)
13 views9 pages

Chap 0201

Uploaded by

udyadav430
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/ 9

Lecture – 6

Encapsulation
Benefits of encapsulation
Abstraction
Benefits of abstraction
Encapsulation
• It 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.

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 and so most member functions are made public to allow the users to access the
data members through those functions.
For example, we operate a washing machine through its power button. We switch on the
power button, the machine starts and when we switch it off, the machine stops. We don't
know what mechanism is going on inside it. That is encapsulation.

Benefits of Encapsulation
There are various benefits of encapsulated classes.
• Encapsulated classes reduce complexity.
• Help protect our data. A client cannot change an Account's balance if we encapsulate it.
• Encapsulated classes are easier to change.
• We can change the privacy of the data according to the requirement without changing the
whole program by using access modifiers (public, private, protected).
• For example, if a data member is declared private and we wish to make it directly
accessible from anywhere outside the class, we just need to replace the specifier private by
public.
Let's see an example of Encapsulation.
#include<iostream>
usingnamespacestd;
classRectangle };
{
I int length , breadth; int main()
public: {
voidsetDimension(intl,intb) Rectangle.rt;
{ rt.setDimension(7,4);
length=l; cout<<rt.getArea()<<endl;
breadth=b; return0;
} }

intgetArea() Output :
{ 28
return length*breadth;
}
Data Abstraction
• Data abstraction is one of the most essential and important feature of object oriented
programming in C++.
• Abstraction means displaying only essential information and hiding the details.

• Data abstraction refers to providing only essential information about the data to the outside
world, hiding the background details or implementation.

• Consider a real life example of a man driving a car. The man only knows that pressing the
accelerators will increase the speed of car or applying brakes will stop the car but he does not
know about how on pressing accelerator the speed is actually increasing, he does not know
about the inner mechanism of the car or the implementation of accelerator, brakes etc in the
car.
Advantages of Data Abstraction:
• Helps the user to avoid writing the low level code
• Avoids code duplication and increases reusability.
• Can change internal implementation of class independently without affecting the user.
• Helps to increase security of an application or program as only important details are
provided to the user.

Types of data abstraction

There are two types of data abstraction.

•Abstraction using classes: A class organizes the data into categories. With access specifiers,
the classes determine which functions users can see and which functions remain hidden.

•Abstraction in header files: Header files obscure all the inner functions from the user.
Example :
}
};
#include <iostream>
using namespace std;
int main()
class implementAbstraction {
{ implementAbstraction obj;
int a, b; obj.set(10, 20);
public: obj.display();
return 0;
void set(int x, int y) }
{
a = x;
b = y; Output :
}
a=10
void display()
{ b=20
cout << "a = " << a << endl;
cout << "b = " << b << endl;
Abstraction Encapsulation
Abstraction is the process or method of gaining the While encapsulation is the process or method to contain
information. the information.
In abstraction, problems are solved at the design or While in encapsulation, problems are solved at the
interface level. implementation level.
Whereas encapsulation is a method to hide the data in a
Abstraction is the method of hiding the unwanted single entity or unit along with a method to protect
information. information from outside.

We can implement abstraction using abstract class and Whereas encapsulation can be implemented using by
interfaces. access modifier i.e. private, protected and public.

In abstraction, implementation complexities are hidden While in encapsulation, the data is hidden using methods
using abstract classes and interfaces. of getters and setters.

The objects that help to perform abstraction are Whereas the objects that result in encapsulation need
encapsulated. not be abstracted.

Encapsulation hides data and the user can not access


Abstraction provides access to specific part of data. same directly (data hiding.
Do Subscribe

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy