Introduction to Abstraction in C++
Abstraction is one of the most important pillars of object-oriented C++ programming language. Data abstraction concept in C++ helps programmers to provide only essential information to the outside world while hiding background details. It’s the most widely used technique that relies on the separation of implementation and interface of the code. Data Abstraction helps the user to increase the flexibility of the code while minimizing the problems and issues.
Code:
#include <iostream>
using namespace std ;
class Addition
{
private : int p, q, r ; // declaring private variables p, q, and r
public : // public function add() can be used by any outside class
void add ()
{
cout << " Please enter the two numbers : " ;
cin >> q >> r ;
p = q + r ; // performing addition and storing total in integer variable p
cout << " The Sum of two number is : " << p << endl ;
}
} ;
int main ()
{
Addition ad ; // creating declared class object “ad”
ad.add () ; // calling add () method
return 0 ;
}
Output:
Types of Abstraction
Given below are the two types of abstraction:
1. Control Abstraction: In control, abstraction implementation details will always be hidden and won’t be visible.
2. Data Abstraction: In data abstraction, information about the data in the code will always be hidden.
For implementing our own Abstract Data Type (ADT) we can use classes. We use cout object of output stream class for streaming the data. We can use access specifiers like public, private, and protected to hide declaring data members as private-public, or protected and hide them from outside the world separately.
Examples of Abstraction in C++
Here we will see how we can achieve data abstraction in C++ programming through classes, header files, and specifiers.
Example #1
Data Abstraction using classes.
Code:
#include <iostream>
using namespace std ;
class abstractionClass // declaring class
{
private :
int x , y ; // private variables x and y
public :
// method to set values of all the declared private members
void set ( int a , int b ) // declaring public function set
{
x = a ;
y = b ;
}
void display () // declaring display() function
{
cout << " x = " << x << endl ;
cout << " y = " << y << endl ;
}
} ;
int main ()
{
abstractionClass obj ; // creating object of declared class
obj.set ( 110 , 250 ) ;
obj.display () ;
return 0 ;
}
Output:
In the above code, you can see we have declared a class knows as abstraction class. In this class, we have declared two private integer variables x, and y. After that we have declared set function in public access specifier and also created one display() function in public mode. Finally, in the main class, we created an object of the class abstract class called “obj”. We used this object to call the set and display function to set and display the results.

4.5 (8,442 ratings)
View Course
Example #2
Data Abstraction using header files.
Code:
#include <iostream>
#include <math.h>
using namespace std ;
int main ()
{
int x = 8 ;
int power = 4 ;
int result = pow ( x , power ) ; // pow(n,power) is the power function to calculate power
std :: cout << " The square of x is : " << result << std :: endl ;
return 0 ;
}
Output:
In the above code, you can see we have used a header file math.h so that we can use predefined math functions in our code. In this code, we have declared three public integer variables x, power, and result. After that, we have used std:: out to calculate the square of the given number and then store them in result integer which we have declared at the starting of the code.
Example #3
Abstraction using specifiers.
Code:
#include <iostream>
using namespace std ;
class Addition { // declaring class for the program
public :
// constructor
Addition ( int i = 0 ) { // constructor for class with the same name
total = i ;
}
// interface to outside world
void numAdd ( int number ) { // add function to add values
total += number ;
}
// interface to outside world
int getTotal () {
return total ;
} ;
private :
// hidden data from outside world
int total ;
} ;
int main () {
Addition ad ;
ad.numAdd ( 25 ) ;
ad.numAdd ( 35 ) ;
ad.numAdd ( 44) ;
cout << " The Total of the number is " << ad.getTotal() << endl ;
return 0 ;
}
Output:
In the above code, you can see we have declared a class knows as Addition. In this class, we have declared one constructor named ”Addition” and inside that constructor, we have initialized the value to zero and the total is set to integer value “i”. then we have created on the function “named” to keep adding the number to the total. Finally, in the main class, we created an object of the class Addition called “ad”. We used this object to call the named function to pass the value in that and start adding.
Conclusion
Data abstraction is used to reuse the written code and change the internal implementation without affecting the source code which helps coder in protecting the data from outside the world. Data abstraction plays a crucial role in avoiding code duplication to perform the same operation again and again.
Recommended Articles
This is a guide to Abstraction in C++. Here we discuss the introduction to abstraction along with types and respective examples. You may also have a look at the following articles to learn more –