Home >>C++ Tutorial >C++ Data Abstraction

Data Abstraction in cpp

Data Abstraction in C++

The procedure that involves delivering of only the essential details to the external world and all the internal details are hidden, in other words the representation of the essential details in the program only is known as the Data Abstraction in C++.

Another definition of the data abstraction in C++ can be that it is a programming technique which generally depends on the interference separation and details of the program implementation.

Here is a real life example that will help you understand it from a layman’s view: ordering a product online is very easy, you just select the item, add the address and pay for it. Then the item gets delivered to you within a promised time. Here is the catch, we know how the product will get to us but we are not aware of the fact that how the gets shipped to us or how is the pricing gets decided or how the payment gets to the merchant. Hence, it can be said that E-commerce website separates the implementation details from the external interface.

An eminent level of abstraction is provided by the C++ programming language. For instance, the pow() function is generally used to calculate the power of a number despite the algorithm being unknown, the function follows this.

Generally, if the users implement the class with public and private members in the C++ program then it is understood that it is an example of the data abstraction in C++.

Ways to achieve Data Abstraction

There are generally two ways by which data abstraction can be achieved:

  • Abstraction using classes
  • Abstraction in header files

Abstraction using classes

As we already known that classes are generally used to gather all the data members and member functions in to a single unit just by using the access specifiers. And from this fact you can conclude that the classes are used to achieve abstraction. The responsibility to determine that which data member will be visible outside and which will not, generally belongs to the class.

Abstraction in header files

Header file is generally known as another type of the abstraction. For instance, we have already discussed that the pow()function is used to calculate the power of a number despite the algorithm being unknown that is used in calculating the power.

Access Specifiers Implement Abstraction:

  • Public specifier : The members can be accessed from anywhere in the program provided that the members are publicly declared.
  • Private Specifier : The members can be accessed only by the function of the class provided that the members are privately declared.

Here are the examples of the data abstraction in C++ that will help you in understanding the topic from a better point of view:

#include <iostream>    
using namespace std;    
class add    
{    
private: int a, b, c; // private members  
public:    
void Sum()    
{    
cout<<"Enter Your first and Second  number  ";    
cin>>a>>b;    
c= a+b;    
cout<<"Sum of two number = "<<c<<endl;    
}    
};    
int main()    
{    
add obj;    
obj.Sum();    
return 0;    
}
Output :
Enter Your first and Second number 10 20
Sum of two number = 30

Advantages Of Abstraction

Here are some of the advantages of the abstraction that are listed below:

  • There is no need to write the low-level code by the programmer.
  • The details of the implementation of the class are generally protected from the inadvertent user level errors.
  • Data Abstraction avoids the situation where a programmer has to undergo the same task every time in order to perform the similar observation. It is also known as the code duplication in C++.
  • Users can change the internal implementation without even affecting the user-level code.
  • The main agenda of the data abstraction is to reusability of the code and to ensure that a proper partitioning of the code has been done across the classes.

No Sidebar ads