Tutorial Study Image

Abstraction in C++


June 9, 2023, Learn eTutorial
269

Object-oriented programming provides numerous capabilities for writing programs with different concepts, which serve to minimize difficulties and boost program flexibility. Data abstraction is one of the characteristics of object-oriented programming. You will discover how the C++ software implements the data abstraction concept in this chapter.

One of the key principles of Object-Oriented Programming is abstraction. Even though this subject is not difficult, many people are not knowledgeable about it. With the help of LearneTutorials, you can learn the fundamentals of abstraction.

What do you mean by abstraction in C++?

Abstraction implies revealing only the most important information and masking the details. The technique of exposing to the outside world only the information that is absolutely necessary and hiding implementation or background information is known as data abstraction.

For example, you don't see what actually happens when you click "send" when you send an email to someone; instead, you merely get the success message and the receiver receives the data via the network. You are unaware of how data is transferred through a network to the recipient (because it is unimportant to you).

Different Types of Abstraction in C++

In C++, there are mainly 2 types of abstraction:

  1. Data Abstraction: It conceals/hides data-related information.
  2. Control Abstraction: It conceals/hides the implementation's details/information.

Abstraction implementation in C++

Two methods exist for putting the idea of data abstraction into practice:

  1. Using classes (Abstraction using classes)

    Classes can be used to implement abstraction in C++. With the use of the available access specifiers, classes assist us in grouping data members and member functions. A class can choose which members of the data set are accessible to the public and which are not. With the use of access specifiers, a class can be used to assemble all data members and functions into a single unit. A class can choose which of its data might be available to the public.

  2. Through header files ( abstraction done through header files)

    Header files are an additional abstraction type in C++. Take the pow() method from the math.h header file as an example. When we need to calculate the power of a number, we simply use the function pow() found in the math.h header file and supply the relevant integers as arguments without being aware of the underlying algorithm that the function uses to do so.

    For example, the sqrt() the function can be used to determine the square root of any given number if the mathematical operations header file, such as <cmath> or </math.h>, is included at the beginning of the program. In this approach, we understand the function of sqrt() but not the specifics of how it calculates the square root.

A Program that finds the square root of a number entered by the user



#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  double num, answer;
  cout<<"Please enter the number: ";
  cin>>num;
  answer = sqrt(num);
  cout << "\n The square root of " << num << " is " << answer ;
}

 

Output:



Please enter the number: 49 
The square root of 49 is 7

So, even though we don't know the background information, we can see that we can directly use the sqrt() function just from the <cmath> library to determine the square root.

Access specifiers in C++

The primary feature of C++ that maintains the implementation data abstraction is access specifiers. They can be used to decide which class members are allowed to be seen by the public and which are not. With the use of public, private, and protected keywords, such limitations can be imposed.

  • Members in a class who have been designated as the public can be accessed from any part of the program.
  • Private members in a class can only be accessible from within the class. They cannot be accessible from any other parts of the code than the class.

We can easily implement abstraction by utilizing the above two access specifier characteristics. In a class, for example, the members that specify the internal implementation can be designated as private. Additionally, it is possible to mark as public any information that must be made available to the general public. The private members are accessible to these public members as well because they are a part of the class.

Example program for abstraction


#include <iostream>
using namespace std;

class implementAbstraction
{
 private:
  int a, b;

 public:
 
  // method in order in order to set values of
  // private members
  void set(int x, int y)
  {
   a = x;
   b = y;
  }
  
  void display()
  {
   cout<<"a = " <<a << endl;
   cout<<"b = " << b << endl;
  }
};

int main()
{
 implementAbstraction obj;
 obj.set(40, 50);
 obj.display();
 return 0;
}

 

Output:


Output
a = 40
b = 50

You will notice in the following program that we are not permitted to directly access the variables a and b, but we can use the functions set() and display() in order to set as well as display the values of a and b.

The advantages of abstraction in C++

  1. Because of the appropriate partitioning, abstraction improves the reusability of the code.
  2. It improves the readability of the code by reducing its complexity and redundancy.
  3. The security of the code is increased by the use of classes and objects. To keep the portions of the code secure, we can mark them as private.
  4. The key components of the code are secure because of abstraction because the user only sees the vital features and is unaware of the background information.
  5. Since the programmer can reuse the same code repeatedly, it helps us carry out identical tasks for related operations.
  6. Abstraction enables internal implementations to be changed without affecting user-level code.
  7. No one else but the programmer is allowed to modify the programmer's data and techniques.
  8. Low-level code does not require to be written by the programmer.

Design Strategy

The code was divided into two sections, implementation, and interface, by data abstraction. As a result, we must leave the implementation alone when developing the code. In other words, the interface should remain independent of implementation. Even the prevention of code duplication is impacted by it.

As a result, we may reuse the code and change the implementation, which helps protect the data mainly from the outside world.

Summary

This tutorial taught us the definition of data abstraction and two alternative ways to use it. Even the access specifiers employed for data abstraction were explained to us.

Along with the real-world example, we also discussed some code features of abstraction. Hopefully, this course helped you understand the idea of data abstraction.