Encapsulation in C++

Encapsulation in C++

With C++ we achieve the functionality to wrap data and functions inside as a single form and keep them safe from outside world and achieve data hiding from and make the code more secure from unauthorised access.

Encapsulation in C++

People sometimes get highly confused between data abstraction and Encapsulation. Both of them are different –

  • Data Abstraction – Is essentially hiding the implementation details and only giving methods to interact with the system without knowing what exactly is happening in the backend code
    • Example – Consider a Truck which is running, you only get accelorator, brakes and gears etc as interface but you don’t know how the mechanical system in the engine works
  • Encapsulation – Is however, different. You wrap and bind all related data together in a single form. Sort of like capsuling it together.
    • Example – All the Engine has separate space in the front of the car, the breaking and clutching system are far from the engine and are clubbed together below the car with axels and wheels. The emission systems are clubbed together and are at the end of the car.
    • Imagine the engine, breaks and emission system all being scattered all over the car. The car is very likely to break down and it will be complex to understand for the mechanic, what is where.
    • Similarly, for a coder to give code more understandable and readable format he groups and encapsulates all the related data together inside a single unit.

Note – With the help of encapsulation in a way we also achieve data abstraction, which is why people think that they are one and the same thing. Yes, while doing encapsulation i.e. grouping into single units. We simultaneously use access modifiers which directly help is hiding the data and managing it good architecture at the same time.

Competitive Coding Techniques

Example

  • Consider that in a Bank for example there is Finance department, marketing, HR and Payroll departments.
    • Payroll department would have all the information related to employee salaries, payments etc
    • The HR would have details of the employees, appraisals and performance information.
    • Similarly, Finance department would have all the information about the finances of the company.

Now, let’s say the Payroll department wants to access information about the employee salaries so that they can credit salaries in their account

  • For this they would need information from Payroll department and can’t access the information directly.
  • So they would send a request to HR department for the details.

Lets see an example on how this is achieved –

#include <iostream>
using namespace std;

class myClass
{
private:
// This data hidden from outside world
int salary;

public:
// function to set salary value
void setSalary(int s)
{
salary = s;
}

// function to return value of
// variable x
int getSalary()
{
return salary;
}
};

// main function
int main()
{
myClass myObj;
myObj.setSalary(80000);

cout<< "The salary is: " << myObj.getSalary();
return 0;
}

Observations –

  1. salary is made private and is not accessible by the outside world.
  2. salary details can only be manipulated, by getSalary and setSalary functions thus access is only restricted with predefined methods and there is no other way to manipulate them, these methods are binding data together.
  3. We have wrapped setting salary value in one code using function and getting salary using another function thus, achieving encapsulation that is wrapping and binding related data together and separating them with one another

To implement this –

  1. Data should always be in private mode
  2. The helper functions should be declared public using public access modifications.

To understand this in detail we suggest you should read further –