C++ Abstraction
Abstraction means showing only the important information and hiding the unnecessary details. Data abstraction is when we only provide the necessary information about data, keeping the complex details ( how it works) hidden from the user.
Abstraction in Real Life: ATM ExampleLet’s use the example of an ATM (Automated Teller Machine) to explain the concept of Abstraction in real life. How Abstraction Works in an ATM:What the User Sees (Abstraction):
What Happens Behind the Scenes (Complexity Hidden):
|
Abstraction using access specifiers
Abstraction in C++ can be easily achieved using access specifiers, which control how class members (variables and functions) can be accessed. Here’s how each one works:
- Public members: These can be accessed from anywhere in the program.
- Private members: These can only be accessed within the class itself and are hidden from the outside.
- Protected members: These can be accessed within the class and by derived classes (subclasses), but not by code outside the class or subclass.
To implement abstraction, you can mark the internal details of a class (like variables) as private so that they are hidden from the outside world. Then, you provide public functions to interact with the important information. These public functions can access the private data inside the class. If a derived class needs access to some information, you can mark certain members as protected, allowing the derived class to use them while still hiding them from the outside world.
C++ Abstraction Code Example
Here’s the code , followed by an explanation of the key points of abstraction:
#include <iostream> using namespace std; class ImplementAbstraction { public: // Public method to display the values of private members int main() { return 0; |
Output:
a = 10
b = 20
Key Points of Abstraction
1. Hiding Internal Data (Data Hiding)
- The class
ImplementAbstraction
has private data members (a
andb
). These private members are hidden from outside access, which means other parts of the program cannot directly access or modify them. This is a key aspect of abstraction, as it hides the internal details of how the data is stored or managed. - Private members protect the data from accidental modification by external code, ensuring that only the class’s public methods control how the data is used.
2. Providing a Simplified Interface
- The public methods
set()
anddisplay()
provide a simplified interface to interact with the private data. These methods allow the user to set values and display values without needing to know how the data is stored or manipulated inside the class. - This is the abstraction part: the user interacts with high-level operations (like setting and displaying values), without knowing the underlying implementation details (like how
a
andb
are stored).
Advantages of Data Abstraction
- – Assists users in avoiding the need to write low-level code.
- – Reduces code duplication and enhances reusability.
- – Allows changes to the internal implementation of a class without impacting the user.
- – Increases the security of an application or program by only providing users with essential information.