One of the most fundamental and significant components of C++ object-oriented programming is data abstraction. Abstraction refers to hiding the details and presenting simply the most important facts. Data abstraction is the process of exposing to the outside world just the information that is absolutely necessary while hiding implementation or background information.
Types of Abstraction:
Data abstraction – This type only shows the required information about the data and hides the unnecessary data.
Control Abstraction – This type only shows the required information about the implementation and hides unnecessary information.
Abstraction Using Classes
Classes may be used to implement abstraction in C++. Using the available access specifiers, the class enables us to group data members and member functions. Which data member is visible to the public and which is not can be determined by a class.
Abstraction in Header Files
Header files are another sort of abstraction in C++. Take the pow() function from the math.h header file as a example. Without understanding the underlying technique used by the function to compute the power of numbers, we simply use the function pow() found in the math.h header file whenever we need to determine the power of a number.
Abstraction using Access Specifiers
Access specifiers are the main pillar of implementing abstraction in C++. We can use access specifiers to enforce restrictions on class members. For example:
Members declared as public in a class can be accessed from anywhere in the program.
Members declared as private in a class, can be accessed only from within the class. They are not allowed to be accessed from any part of the code outside the class.
The two qualities mentioned above that access specifiers offer allow us to construct abstraction with ease. Let's say that a class has the ability to mark the elements that specify the internal implementation as private. Additionally, important information that must be disclosed to the public can be marked as such. And because they are inside the class, these public members have access to the private members.
#include <iostream>
using namespace std;
class implementAbstraction {
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Output
a = 10
b = 20
You can see in the above program we are not allowed to access the variables a and b directly, however, one can call the function set() to set the values in a and b and the function display() to display the values of a and b.
Advantages of Data Abstraction
Helps the user to avoid writing the low-level code
Avoids code duplication and increases reusability.
Can change the internal implementation of the class independently without affecting the user.
Helps to increase the security of an application or program as only important details are provided to the user.
It reduces the complexity as well as the redundancy of the code, therefore increasing the readability.
We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.