Home >>C++ Tutorial >C++ Data Abstraction
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++.
There are generally two ways by which data abstraction can be achieved:
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.
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.
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; }
Here are some of the advantages of the abstraction that are listed below: