C++ Data Abstraction3 min read

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

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.

MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More