Difference Between Abstraction And Encapsulation In C++

Share Your Love

Here in this post, I am going to discuss the difference between abstraction and encapsulation in C++. Here also understand the difference and also many students are confused about abstraction and encapsulation.

What is Abstraction?

Abstraction is a concept in object-oriented programming that “shows” only the most important features while “hiding” the rest. The basic goal of abstraction is to keep consumers away from unnecessary details. Abstraction is the process of picking data from a bigger pool in order to present the user only the relevant details of an item. It aids in the reduction of programming effort and complexity. It is one of the most crucial OOP ideas.

Example Of Abstraction:

#include <iostream> 
using namespace std; 

class Multiplication { 
   private: 
      // private variables 
      int Num1, Num2, Num3 
   public: 
      void mul(int num1, int num2) 
      { 
          Num1 = Num1; 
          Num2 = num2; 
          Num3 = num1 * num2; 
          cout << "Multiplication of the two number is : " << Num3< <endl; 
      } 
}; 
int main() 
{ 
    Summation myMul; 
    myMul.mul(5, 4); 
    return 0; 
} 

Output:

Multiplication of the two number is: 9 

The variables Num1, Num2, and Num3 are private in this case, and so inaccessible to any code outside of the class Multiplication. The variables in this example are set to values supplied as arguments to the sum method. This isn’t a perfect example because the values aren’t always set right before being used in this way, but it illustrates the reality of the implementation.

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

What is Encapsulation?

Encapsulation is a technique for making a complex system more user-friendly. The user does not need to be concerned about the system’s internal details or complications. Encapsulation is a method of combining data and the code that manipulates it into a single entity. It can be thought of as a protective wrapper that prevents random access to code declared outside of it.

Example Of Encapsulation:

#include <iostream> 
using namespace std; 

class EncapsulationExample { 
    private: 
        // we declare a as private to hide it from outside 
        int numeric; 
        
        public: 
        // set() function to set the value of a 
        void set(int input1) 
        { 
            numberic = input1; 
        } 
        
        // get() function to return the value of a 
        int get() 
        { 
            return numeric; 
        } 
}; 

// main function 
int main() 
{ 
    EncapsulationExample myInstance; 
    myInstance.set(10);
    cout << myInstance.get() << endl; 
    return 0; 
} 

Output:

10

The variable numeric is made private in this program so that it can only be read and edited using the methods get() and set() that are available within the class. As a result, we can conclude that the variable a, as well as the methods, set() and get(), have been bound together, resulting in encapsulation. There’s nothing remarkable about the method names “get()” or “set()” – there might be other ways that manipulate the variable numeric…this is what encapsulation is all about.

Encapsulation Example-2:

//Account And Sales Using Encapsulation

#include <iostream>

using namespace std;

class Account
{
private:
    int account_number;
    string account_holder_name;

public:
    void set_account(int ac_num, string ac_name)
    {
        account_number = ac_num;
        account_holder_name = ac_name;
    }

    void get_account()
    {
        cout << "Account Holder Name: " << account_holder_name << " Account_number: " << account_number;
    }
};

class Sales : public Account
{
private:
    int person_id;
    string person_name;

public:
    void set_sales(int p_id, string p_name)
    {
        person_id = p_id;
        person_name = p_name;
    }

    void get_sales()
    {
        cout << "\nSales Person Name: " << person_name << " Sales Person Id: " << person_id;
    }
};

class Organizaation : public Sales
{
public:
    Organizaation()
    {
        set_account(42325, "QWERTY");
        set_sales(5235, "Gopal");
    }
};

int main()
{
    Organizaation O;
    O.get_account();
    O.get_sales();
    return 0;
}

Output:

Account Holder Name: QWERTY Account_number: 42325
Sales Person Name: Gopal Sales Person Id: 5235 

Difference between Abstraction and Encapsulation:

AbstractionEncapsulation
The process or method of obtaining information is known as abstraction.Encapsulation, on the other hand, is the process or method of containing information.
Problems are tackled at the design or interface level in abstraction.Encapsulation helps to solve the problems are at the implementation level.
Abstraction is a technique for hiding undesired information.Encapsulation, on the other hand, is a way of hiding data in a single entity or unit as well as a method of protecting information from the outside.
Abstract classes and interfaces can be used to implement abstraction.Encapsulation, on the other hand, can be implemented utilising access modifiers such as private, protected, and public.
Implementation complexities are hidden in abstraction by using abstract classes and interfaces.The data is hidden using getter and setter methods when in encapsulation.
Encapsulated are the items that aid in abstraction.Encapsulated objects, on the other hand, do not need to be abstracted.
Difference Between Encapsulation And Abstraction

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter