Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between Abstraction and Encapsulation in C++

  • Difficulty Level : Medium
  • Last Updated : 06 Oct, 2021

Abstraction
In OOPs, Abstraction is the method of getting information where the information needed will be taken in such a simplest way that solely the required components are extracted, and also the ones that are considered less significant are unnoticed. The concept of abstraction only shows necessary information to the users. It reduces the complexity of the program by hiding the implementation complexities of programs. 
Example of Abstraction:
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

CPP




#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. Encapsulation is a method to hide the data in a single entity or unit along with a method to protect information from outside world. This method encapsulates the data and function together inside a class which also results in data abstraction. 
Example of Encapsulation:
 



CPP




#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.NOAbstractionEncapsulation
1.Abstraction is the process or method of gaining the information.While encapsulation is the process or method to contain the information.
2.In abstraction, problems are solved at the design or interface level.While in encapsulation, problems are solved at the implementation level.
3.Abstraction is the method of hiding the unwanted information.Whereas encapsulation is a method to hide the data in a single entity or unit along with a method to protect information from outside.
4.We can implement abstraction using abstract class and interfaces.Whereas encapsulation can be implemented using by access modifier i.e. private, protected and public.
5.In abstraction, implementation complexities are hidden using abstract classes and interfaces.While in encapsulation, the data is hidden using methods of getters and setters.
6.The objects that help to perform abstraction are encapsulated.Whereas the objects that result in encapsulation need not be abstracted.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!