Summary: In this tutorial, we will learn what abstraction is, how it works, and also see an example of it in C++.

Abstraction is one of the properties of the object-oriented programming language. C++ being an OOP language allows us to take advantage of it to make our program more secure.

Abstraction in context to programming means hiding background or implementation details and showing only necessary or important information.

Real-life example: Consider our smartphone. We run a variety of apps and games on our phone, click photos with it, make calls and etc. We certainly know what a smartphone can do, but how it is all done is hidden from us.

Similarly, abstraction in C++ is a way to hide some details that we want them to hide from the outside world (user).

So being a programmer it’s our responsibility to show only relevant information to our user.

How to Implement Abstraction in C++?

Abstraction in C++ can be implemented in two ways:

1. Using Class and Function

When we define a class we also define its characteristics (data members) and its behavior (member methods), which can lead to two cases:

  • If we invoke any of the class methods using its object then we only get the result as the return value. We don’t get to know how the returned value has been calculated.
  • Similarly, when any of the data members are declared as private using the private access specifier, then it cannot be accessed outside of its class.

Notice how these two scenarios are hiding details from the end-user. Hence by using class and function, we hide the implementation code from the user and only let them get the result by invoking the functions.

Here is an example demonstrating the same in C++:

#include <iostream>
using namespace std;
 
class Math{
private:
    int sum; //Can't be accessed in main
public:
    Math(){}
 
    void add(int a, int b){
        //Hidden Implementation
        sum = a + b;
    }
 
    int getSum(){
        return sum;
    }
};
 
int main()
{
    int result;
 
    Math math;
    math.add(5,4);
 
    // cout << "Sum: "<< math.sum << endl;  //Error
    cout << "Sum: "<< math.getSum() << endl;
    return 0;
}

Output:

Output of abstraction program in C++

In the above program, it is unknown for the main function how the amount is being calculated and how many members are in the Math class.

2. Using Header File

Header files also implement abstraction in a way. Consider math.h as an example. we include it to use its functions such as pow(), sqrt(), etc but we don’t actually know how the results are being calculated?

Example:

#include <iostream>
#include <math.h>
using namespace std;
 
int main()
{
    int square;
    square = pow(5,2);
 
    cout << "Square of 5: "<< square << endl;
    return 0;
}

Output:

C++ abstraction example

Note: We can also make our own header file and use it in another program file in order to implement abstraction.

Advantages of Abstraction in C++

  • Hides low-level code implementation or details in the design level.
  • Allows changing the implementation details at any time without letting the user know.
  • Promotes code re-usability.
  • Increases the security of the program by showing only important details to the user.

In this tutorial, we learned about Abstraction and its significance in the C++ programming language.

Leave a Reply