Abstraction in C++

abstraction in c plus plus

Data Abstraction is a process of providing only the essential details to the outside world and hiding the internal details, i.e., representing only the essential details in the program.

Data Abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program.

Let’s take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.

C++ provides a great level of abstraction. For example, pow() function is used to calculate the power of a number without knowing the algorithm the function follows.

In C++ program if we implement class with private and public members then it is an example of data abstraction.

Data Abstraction can be achieved in two ways:

  • Abstraction using classes
  • Abstraction in header files.

Abstraction using classes:

An abstraction can be achieved using classes. A class is used to group all the data members and member functions into a single unit by using the access specifiers. A class has the responsibility to determine which data member is to be visible outside and which is not.

Abstraction in header files:

Another type of abstraction is the header file. For example, the pow() function available is used to calculate the power of a number without actually knowing which algorithm function uses to calculate the power. Thus, we can say that header files hides all the implementation details from the user.

Access Specifiers Implement Abstraction:

Public specifier: When the members are declared as public, members can be accessed anywhere from the program.

Private specifier: When the members are declared as private, members can only be accessed only by the member functions of the class.

Example program for data abstraction using header file.

#include <iostream>  
using namespace std;  
int main()  
 int n = 4;  
   int power = 3;  
   int result = pow(n,power);         // pow(n,power) is the  power function  
   std::cout << "Cube of n is : " <<result<< std::endl;  
   return 0;  


Cube of n is : 64

Example program for data abstraction using classes.

#include <iostream>    
using namespace std;    
 class Sum    
private: int x, y, z; // private variables  
void add()    
cout<<"Enter two numbers: ";    
z= x+y;    
cout<<"Sum of two number is: "<<z<<endl;    
int main()    
Sum sm;    
return 0;    


Enter two numbers:
Sum of two number is: 9

In the above example, abstraction is achieved using classes. A class ‘Sum’ contains the private members x, y and z are only accessible by the member functions of the class.

Advantages Of Abstraction:

  • Helps the user to avoid writing the low level code
  • Avoids code duplication and increases reusability.
  • Can change internal implementation of class independently without affecting the user.
  • Helps to increase security of an application or program as only important details are provided to the user.

Disadvantages of Abstraction:

  • Simple speed. For executing an abstraction, the code implementing must handle cases and situations which is not always necessary- or often aren’t needed – by many usage scenarios. This will make the code slow in comparison to the code which is directly implemented in the operation without using the abstraction.
  • Size of the code. This does not matter on larger systems in today’s world, but it may create problems in small devices or constrained environments. The extra code must be able to fully implement an abstraction adds line count and ultimately code size, and if the code fails to be implemented carefully, it can end up “pulling in” lots of extra code, which will lead to overlarge runtime executables and making the device more expensive.
0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x