Data abstraction is the process of hiding the details but only displaying the relevant information to the users. This is one of the main advantages of using abstraction.
It is a programming way or a technique through which we can separate the interface with the implementation details.
Take a real-life example:
Suppose a person is typing with the help of a keyboard on his/her computer. That person knows that pressing any alphabet on the keyboard, displays that alphabet on the screen of the monitor but he/she doesn’t know how the internal mechanism is working to display the alphabet.
Therefore, hiding the internal mechanism and showing only its output is an abstraction in this example.
C++ also provides a great level of abstraction such as the function pow() we use in a program to calculate the power of some number is internally implemented. Although we do not know how it is implemented. We just use it by calling pow() without knowing its algorithm.
There is another example such as private and public modifiers or sort()function that we use in a program.
In C++, abstraction can be achieved in two ways.
- Abstraction using classes
- Abstraction in header files.
Abstraction using Classes:
Classes are used o achieve abstraction in C++. A class grouped the class members that is data members and member function into a single unit using access specifiers. A class can decide which members are relevant to show to the outside world and which to hide.
Abstraction in Header files:
Another way to achieve abstraction is through header files. For example, consider the use of function pow() in a program to calculate the power of some number that is present in math.h
header file, and this function is internally implemented. Although we do not know how it is implemented. We just use it by calling pow() without knowing its algorithm.
Access Specifiers for abstraction:
Access specifiers are the main way to implement abstraction and enforce the restriction on class members in C++.
- private: The members declared private are only accessible to the class They are not allowed to access outside the class, hence making it secure.
- public: The members declared public can be accessed from anywhere in the program.
Let us understand through a C++ example for abstraction using the above two specifiers.
Example of C++ data abstraction using classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //C++ program to demonstrate the abstraction #include <iostream> using namespace std; class Average { // delaring private members private: int num1, num2, avg; // public members public_colon void set(int x, int y) { num1 = x; num2 = y; } void display() { avg = (num1 + num2) / 2; cout << "Average: " << avg << endl; } }; int main() { Average ag; ag.set(10, 10); ag.display(); return 0; } |
Output:
Average: 10
As you can see in the above program the private variables num1, num2 and avg cannot be accessed outside the class. Only the public member functions are allowed to access.
Advantage of data abstraction
- When you need to modify the code in the future, you only need to modify the higher-level class and no need to modify the lower-level class.
- It also avoids code duplication increases the reusability of the code.
- Since the abstraction separates the interface and implementation, the internal changes can be easily made without affecting the user-level code.
- The abstracting of the data itself increases the security as no user can access the internal hidden mechanism.