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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …