Difference between Abstraction and Encapsulation in C++

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

Abstraction & Encapsulation are two essential concepts of Object-Oriented Programming Language. In this post, we will have a glance at difference between Abstraction and Encapsulation in C++ programming language with tables.

Difference between Abstraction and Encapsulation in C++

S.NO ABSTRACTION ENCAPSULATION
1. In the abstraction, problems are resolved at the level of design or interface. While in encapsulation, problems are resolved at the level of implementation.
2. Abstraction is a means of hiding unwanted information. Whereas encapsulation is a method for hiding data in a single entity or unit along with a method for protecting information from outside.
3. We can use an abstract classes and interfaces to implement abstraction. Whereas encapsulation can be carried out by means of an access modification, i.e. private, protected and public.
4. In abstraction, the complexities of implementation are hidden by using abstract classes and interfaces. During the encapsulation process, the data is hidden using the getters and setters methods.
5. Objects that help perform abstraction are encapsulated. Whereas objects that result in encapsulation do not need to be abstracted.

Abstraction in C++

Abstraction is the method of obtaining information in OOPs. The necessary information will be taken in such a simple way that only the appropriate components are collected and also those which are considered to be less important are not found.

Example of Abstraction in C++

#include <iostream>
using namespace std;

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

Output:

Sum of the two number is 8

Encapsulation in C++

Encapsulation is the process or method used to encapsulate the information. It hides the crucial details & provides generic information.

Example of Encapsulation in C++

#include <iostream> 
using namespace std;

class Encapsulation { 
private: 
int a;
public: 

void set(int x) 
{ 
a = x; 
}

int get() 
{ 
return a; 
} 
};

int main() 
{ 
Encapsulation e1;
k
e1.set(5);

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

Output:

5

Leave a Comment

Your email address will not be published. Required fields are marked *