Abstraction in C++
Data abstraction, a crucial feature in C++ object-oriented programming (OOP), involves displaying only essential information while concealing implementation details. This concept simplifies program structure, enhancing flexibility by organizing code into reusable classes for creating individual objects. In a real-life analogy, like a person driving a car, abstraction lets users interact with essential functionalities like accelerator and brakes without delving into internal mechanisms. As a core aspect of C++ features, data abstraction contributes to program modularity and readability. This article explores how C++ implements data abstraction, emphasizing its significance and benefits in optimizing code structure and promoting object-oriented programming principles.
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:
Output:
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. Read more about Abstract Class in C++.
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.