Abstraction in C++ In Simple Words with Examples


Data Abstraction offers sufficient public methods to set users to explore the object’s functionality and change its data.

For instance, abstraction is a state without understanding the class’ background details.

C++ Abstraction in Simple Words

Abstraction in C++ refers to the process of exposing only the most essential information about data to the outside world while hiding its implementation details.

What is an Abstraction with Example?

Any C++ program that implies public and private members for a class is an example of data abstraction.

Consider the following illustration:

#include <iostream>
using namespace std;

class Adder {
      Adder(int i = 0) {
         total = i;
      void addNum(int number) {
         total += number;
      int getTotal() {
         return total;
      int total;

int main() {
   Adder a;

   cout << "Total " << a.getTotal() <<endl;
   return 0;


Total 60

The class above does addition and returns the result.

The public members addNum and getTotal are the class’s interfaces to the outside world, and a user must be familiar with them to utilize the class.

This private member total is not something the user needs to know about, but it is important for the class to work.

Learn more about C++ classes through our Classes in C++ Language with Examples tutorial.

What are Abstraction and Encapsulation in C++?

Abstraction is the strategy that hides undesirable information.

Encapsulation, on the other hand, is a way to hide data inside of a single entity or unit, as well as a way to keep the data safe from the outside world.

You can also explore more about C++ data and method encapsulation.

What is an Abstraction in OOP?

Abstraction is an important feature of Object-Oriented Programming ideas.

Its primary function is to conceal unnecessary details from users.

It involves picking data from a bigger pool and reduces programming effort and complexity.

What are the 2 types of abstraction?

  • Control Abstraction
  • Data Abstraction

Control Abstraction

Control abstraction shows the details of implementing abstraction where methods are always not visible.

Data Abstraction

Data Abstraction in C++ is the process of delivering only the necessary details to the outside world and hiding the internal details.

Example Programs of Abstraction in C++

Look at how to help abstract data in the C++ programming language by using classes, header files, and specifiers.

Example 1

Data abstraction in C++ is attainable using classes, and it permits the grouping of data members and member functions using the available permission labels.

Example abstraction using classes:

#include <iostream>
#define PI 3.142

using namespace std;

class circle
private :
float radius;
public :

void get_radius(void)
cout<<"Enter radius of circle"<<endl;

float area(void)

float circumference(void)
//implementation of the class
int main ()
// creating object of declared class
circle c;
cout << " AREA = " << c.area() << endl ;
cout << " CIRCUMFERENCE = " << c.circumference() << endl ;
return 0 ;


AREA  =  50.272

In the above code, the class circle is declared. In this class, the variable radius stores the value of the user-supplied radius.

Then two functions in the class’s public section declare area and circumference.

Example 2

Header files could be another type of abstraction in C++. Consider, for instance, the pow() method provided in the math.h header file.


#include <iostream>
#include <math.h>

using namespace std ;
int main ()
int a = 5 ;
int b = 2 ;
int result = pow ( a , b);
cout << " Square of a = " << result << std::endl ;
return 0 ;


The square of a is : 25

Note that pow(a,b) is the power function to calculate power.

Using the math.h header file, the predefined math functions given by the C++ programming language in the program above are included.

The std::cout figures out the square of the given digit and stores it in the declared integer result.

Example 3

Abstraction in C++ is also using permission labels to show how to access program data.

Using permission labels, the application can limit the data members of a class in the following ways:

  • Public: The data and functions are declared as public and can be used anywhere as the class is visible.

  • Private: implies that the data members and member functions are private and can only be accessed within the class.

  • Protected: Only their related classes and derived classes can access data members and member functions.


#include <iostream>
using namespace std;

class abstraction
    int x, y;

    void set(int m, int n)
      x = m;
      y = n;
    void display()
      cout<<"x = " <<x << endl;
      cout<<"y = " << y << endl;

int main()
  abstraction sp;
  sp.set(20, 30);
  return 0;


Sum =  95

This example shows evidence that direct access to the variables x and y is prohibited.

However, the set() function can be used to set the values x and y. The display() function is invoked to display the x and y values.

Advantages of Data Abstraction

  • The proper division promotes the code’s reusability as a result of abstraction.

  • It decreases the code’s complexity and redundancy, thereby boosting its readability.

  • Because a programmer can use the same code again and again, we can do similar jobs and activities.

  • Abstraction allows internal implementations to be modified without affecting user-level code.

  • Nobody else but the coder can modify his data and methods.

  • The developer is not required to write low-level code.


The data abstraction principle enables developers to deliver only relevant data.

This article clarifies everything essential to understanding abstraction in C++.

This will be helpful for your future programs.

Leave a Comment