You are on page 1of 3

Difference between Abstraction and Encapsulation in

C++
Difficulty Level : Easy ● Last Updated : 16 Oct, 2019

Abstraction:

In OOPs, Abstraction is that the method of getting info. the information needed will be taken in

such the simplest way that solely the required components are extracted, and also the ones

that are considered less significant are unnoticed. Or the unessential info will be drained dead

set keep solely the sensitive information intact. during this case, most of the classes don’t have

any sor t of implementation, and most of the problem-solving method is completed at the

inter face stage. one thing that doesn’t exist and is just a concept is named Abstraction. In

abstraction, implementation complexities are hidden using abstract classes and inter faces.

Example of Abstraction:

#include <iostream>
using namespace std;

class Summation {
private:
// private variables
int a, b, c;
public:
void sum(int x, int y)
{
a = x;
b = y;
c = a + b;
cout<<"Sum of the two number is : "<<c<<endl;
}
};
int main()
{
Summation s;
s.sum(5, 4);
return 0;
}

Output :

Sum of the two number is: 9

In the this example, we can see that abstraction has achieved by using class. The class

'Summation' holds the private members a, b and c, which are only accessible by the member


functions of that class.

Encapsulation:

Encapsulation is the process or method to contain the information. The info, it provides is that

the solely the one that is critical and ever y one the opposite data that is unsuitable is hidden

already. during this case, the matter determination is completed at the stage of

implementation. Encapsulation is a method to hide the data in a single entity or unit along with

a method to protect information from outside. Encapsulation keeps something in an

exceedingly capsule and showing of solely the essential options of a product. for example,

once someone is mistreatment software package they are doing not recognize what the secret

writing is, they solely use the mandator y functions that result from the secret writing that is

unbroken safe within the files.

Example of Encapsulation:

#include <iostream>
using namespace std;

class EncapsulationExample {
private:
// we declare a as private to hide it from outside
int a;

public:
// set() function to set the value of a
void set(int x)
{
a = x;
}

// get() function to return the value of a


int get()
{
return a;
}
};

// main function
int main()
{
EncapsulationExample e1;

e1.set(10);

cout<<e1.get();
return 0;
}

Output :

10


In the this program, the variable a is made private so that this variable can be accessed and

manipulated only by using the methods get() and set() that are present within the class.

Therefore we can say that, the variable a and the methods set() as well as get() have binded

together that is nothing but encapsulation.

Difference between Abstraction and Encapsulation:

S.NO Abstraction Encapsulation

1. A bstraction is the process or While encapsulation is the process or method to

method of gaining the contain the information.

information.

Related Articles Save for later

2. In abstraction, problems are While in encapsulation, problems are solved at

solved at the design or inter face the implementation level.

level.

3. A bstraction is the method of Whereas encapsulation is a method to hide the

hiding the unwanted data in a single entity or unit along with a

information. method to protect information from outside.

4. We can implement abstraction Whereas encapsulation can be implemented

using abstract class and using by access modifier i.e. private, protected

inter faces. and public.

5. In abstraction, implementation While in encapsulation, the data is hidden using

complexities are hidden using methods of getters and setters.

abstract classes and inter faces.

6. The objects that help to per form Whereas the objects that result in

abstraction are encapsulated. encapsulation need not be abstracted.

You might also like