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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …