C++ Abstraction

C++ Abstraction is one of the most important features of C++ object oriented programming. Abstraction means to hide some features of the code and display only necessary information to user. Data abstraction provides the capability to hide implementation details of the data and show only needed information for user interaction to the outside world. Data abstraction has two separate layers. One is the interface layer that is visible and the other is implementation layer that is hidden.

For instance, in real life car is the example of abstraction. Driver has ability to apply brakes or accelerator. Driver can also steer left and right using the steering wheel. The driver just applies the brakes or accelerator. But the mechanism of how brake works or how car moves is not known to the driver.

Another real life example is ATM machine. When user requests a transaction, he gets cash and a success message. But he doesn’t know how the ATM machine processes its request.

In the same way, when a person turns on or turns off a light, he sees the only the action. The light turns on. The person does not know the circuit or the connections. These are all real life examples of abstraction.

C++ programming language provides abstraction in two ways:

  • Abstraction in Classes
  • Abstraction in Header files

Abstraction in Classes

In object oriented programming OOP, classes can help achieve C++ abstraction. Classes can have some public, private or protected members. By using access specifiers, classes can decide which members will be available to the outside world and which will be hidden.

Access Specifiers

Access specifiers define the scope of class members. A class can have public and private members (variables and functions). If a programmer wants to keep some member accessible to the class only, then those members should be declared under private. The code outside the class cannot access the private members. The members accessible from anywhere in the program have public access type.

In the code there is a class Addition. It has a public constructor Addition and two public functions. It has a private variable sum. If programmer tries to access private members of a class from outside the class, error arises.

#include <iostream>
using namespace std;
class Addition {
   public:
      Addition(int i = 0) {
         sum = i;
      }
      void sumNum(int num) {
         sum += num;
      }
      int getSum() {
         return sum;
      };
    private:
      int sum;
};
int main() {
   Addition a;
   a.sumNum(5);
   a.sumNum(10);
   a.sumNum(15);
   cout << "Sum " << a.getSum() <<endl;
   return 0;
}

The output of the above code after compilation is:

Abstraction in Header files

Using header files also provides C++ abstraction. For example, math.h is a header file that has a function abs(). Programmer can include the class in the code to use the function abs(). Instead of writing the logic of abs() function from scratch, whenever there is a need to calculate the absolute value of any number, programmer can call this function with one argument, by including the math.h header file in the program.

Similarly the frequently used keywords cout and cin are public members of iostream class. By including the iostream class as header file, user can call these functions any time for input output function.

For example, the code below includes header file cmath. The code calculates and displays the absolute value of double type variable using the built in function abs().

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   double i = -98.9;
   double a = abs(i);
   cout << a << endl;
   return 0;
}

The output of the above code after compilation is:

Advantages of C++ Abstraction

There are multiple advantages of using abstraction in C++ Programming language. Some of them are:

  • C++ Abstraction helps user achieve simplicity by concealing the irrelevant and complex details.
  • Programmer can make the internal working of code secure by giving no access of it to the front end user.
  • Abstraction provides the flexibility to change the internal implementation of code without having to change the public data. For example, if a programmer needs to change the private member of a class, he can do it by simply changing it at one place only. Rather than changing it from all the classes those were accessing that public member.
  • By using abstraction, programmer does not have to write low level code.
  • Abstraction provides code reusability and follows the coding principle DRY (don’t repeat yourself).
  • Programmer can avoid code repetition or duplication by using C++ abstraction.