Data encapsulation can be thinkable like one another type of data abstraction.
As you already know, programs are composed of the two fundamental elements. First one is “data”, which you hold all your variables and datas to make calculations or etc, and the second one is program statements, which you can say the “methods” or “functions”, that you are allowed to make calculations with your datas.
Encapsulation is an OOP (Object Oriented Programming) concept that binds together the data and the functions that manipulate the data, and that keeps both safe from outside interface and misuse. Data encapsulation led to the important OOP concept of data hiding.
Next phrase would be the most important thing for you to understand the difference between Data Encapsulation and Data Abstraction. Read it carefully and please have it somewhere to see anytime 🙂
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private.
return std::cout << "We have : " << ragnar << ", " << rollo << " and " << bjorn << std::endl;
std::string ragnar = "Ragnar";
std::string rollo = "Rollo";
std::string bjorn = "Bjorn";
Now, we have ragnar, rollo and bjorn as variables and they are private as you see in the above code. This means they can be only accessed by the members of Valhalla class, which is whoWeHave() function for now. Otherwise, no any other function can touch them even for seeing the values of them. This is one way encapsulation is achieved. Do not forget that, If I was declare my variables with no any access specifier, they would be private, instead of public.
Below sentence is very important for you to understand how to implement the encapsulation mechanism in your code. Read it carefully please:
Making one class a friend of another exposes the implementation details and reduces encapsulation. The ideal is to keep as many of the details of each class hidden from all other classes as possible.
I beleive we are ready to have an example now.
Data Encapsulation Example:
Below example is very basic one, and I beleive it would help you to understand the concept easily.
// this will work when we generate our first object from this class
// it will have the value as "0"
MyMath(int i = 0)
total = i;
// interface to outside world
void addNum(int number)
total += number;
// another interface to outside world
void subtractNum(int number)
total -= number;
// interface to outside world
// hidden data from outside world
int main( )
std::cout << "Total " << result.getTotal() <<std::endl;
When the above code is compiled and executed, it produces the following result:
Above class has the “add” and “subtract” functions, and it is doing adding and subtraction operations, and returns the total number. The public members addNum, subtractNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from outside world, but is needed for the class to operate properly.
Designing Strategy for Data Encapsulation in C++:
Most of us have learned through bitter experience to make class members private by default unless we really need to expose them. That’s just good encapsulation.