Abstraction in C++

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

Abstraction is defined as delivering only essential information to the outer world while masking background details, i.e. representing the required information in a program without showing the specifics. Abstraction is a design and programming method that separates the interface from the implementation.

Scope

  • This article is about what is an abstraction in C++ and ways of achieving data abstraction in C++.
  • In this article, various types of abstraction are explained.
  • We will talk about various access specifiers with examples.
  • This article also includes information about the abstract class, the need for abstraction, and the advantages of abstraction.

Introduction

Let us understand abstraction with a real-life example. A mobile user uses various mobile phone functionalities, such as calls, SMS, etc. But the actual implementation details of these functions are hidden from the mobile user. This is an example of abstraction.

In C++, data abstraction is one of the most significant and crucial features of object-oriented programming. Abstraction in C++ is achieved through classes, header files, and access specifiers.

Types of Abstraction in C++

In the C++ language, there are two types of abstraction:

Control abstraction - In this type of abstraction implementation, the process is hidden from the user.

The following are the critical elements concerning control abstraction:

  • Control Abstraction adheres to the core idea of DRY coding, which stands for Don't Repeat Yourself, and the finest illustration of control abstraction is the use of functions in a program.
  • Control Abstraction is a technique for creating new features by combining control statements into a single unit.
  • It's a fundamental characteristic of all higher-level languages, not only Java.
  • Applications for control abstraction include higher-order functions, closures, and lambdas.

Data abstraction - Data abstraction always hides the information about the data in the program.

The following are the key elements concerning data abstraction:

  • Data abstraction is the process of delivering the necessary data to the outside world while concealing the necessary details within, i.e., representing only the necessary details in the program.
  • Data abstraction is a programming method that separates the program's interface and implementation details.
  • Consider an air conditioner, which can be turned on or off, the temperature, the mode, and other exterior components such as a fan and a swing. But we don't know the AC's interior workings, i.e., how it operates. As a result, we may claim that AC isolates implementation details from the external interface.

Ways of Achieving Data Abstraction in C++

There are two ways of achieving data abstraction in C++:

1. Abstraction In Header Files

Abstraction can also be achieved using header files as we hide the function's implementation in header files. We could use that same function in our program without knowing its inside workings. Sort(), for example, is used to sort an array, a list, or a collection of items, and we know that if we give a container to sort, it will sort it, but we don't know which sorting algorithm it uses to sort that container.

2. Abstraction Using Classes

Classes can be used to implement Abstraction in C++. We may group data members and member functions into classes using the available access specifiers. A Class can choose which data members are visible to the outside world and which are hidden.

Abstraction Using Access Specifiers

In this section, we will know in detail how abstraction is achieved using classes. Access specifiers of class can be used to impose limits on class members. There are the following types of specifiers:

Public Specifier

The public specifier means that members can be accessed from anywhere in the program when they are declared as public.

Private Specifier

The private specifier means that when members are declared private, they can only be accessed by the class's member functions.

Protected Specifier

The protected specifier means only their friend and derived classes can access data members and member functions.

Example

Let's look at the following example:

Code:

#include <iostream>
using namespace std;
  
class TestAbstraction
{
    private:
        string x, y;
  
    public:
      
        // method to set values of 
        // private members
        void set(string a, string b)
        {
            x = a;
            y = b;
        }
        //printing values  
        void print()
        {
            cout<<"x = " << x << endl;
            cout<<"y = " << y << endl;
        }
};
  
int main() 
{
    // object of TestAbstraction
    TestAbstraction t1;
    t1.set("Scaler", "Academy");
    t1.print();
    
    return 0;
}

Output:

x = Scaler
y = Academy

Explanation:

  • In this example, abstraction is achieved using classes and access specifiers.
  • TestAbstraction class is declared with string x, y as its private members, which means we could not access these strings outside the class.
  • void set(string a, string b) and void print() are declared public members' functions.
  • TestAbstraction t1 creates the object of TestAbstraction class.
  • t1.set("Scaler", "Academy"); sets the private member's string x as Scaler, y as Academy. These implementation details are hidden from the user.
  • t1.print() prints the members of t1 object.

What Is an Abstract Class?

An abstract class is used to provide a suitable base class from which additional classes can be derived, or we can say that we could form a blueprint to derive more classes. Abstract classes only provide a user interface; they cannot be used to construct objects. When an object of abstract classes is attempted to be instantiated, a compilation error occurs. In C++, data abstraction is also implemented using abstract classes.

Why Do We Need Abstraction?

Abstraction is one of the most fundamental concepts in OOP, and it is thoroughly implemented in C++. We can hide the program's implementation specifics behind abstraction and only reveal the details we wish to show to the outside world. This will make over program simple to use and easy to understand. It has many advantages, which have been listed below.

Advantages of Data Abstraction in C++

  • It makes it impossible for the user to write low-level code.
  • It reduces program duplication and enhances reusability.
  • The implementation of the internal class can be changed without affecting the user.
  • It aids in the improvement of an application's or program's privacy by only presenting the user with pertinent information.
  • Inadvertent user-level mistakes that could alter the object's state are protected from class internals.
  • Without requiring changes to user-level code, the class implementation may vary over time in response to new requirements or problem reports.

Design Strategy

Abstraction divides code into two categories: interface and implementation. So, when creating your component, keep the interface separate from the implementation so that if the underlying implementation changes, the interface stays the same.

In this instance, any program that uses these interfaces would remain unaffected and would require recompilation with the most recent implementation.

Conclusion

  • Abstraction is the process of only showing the necessary details to the user and hiding the other details in the background.
  • Control and data are the two types of abstraction in C++.
  • Abstraction in C++ is achieved through classes, header files, and access specifiers (public, private, protected).
  • Abstraction in C++ has many advantages, such as it provides code reusability, code duplicity is reduced, etc.
Challenge Time!
quiz
quiz
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
Free Courses by top Scaler instructors
rcbGet a Free personalized Career Roadmap from