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 Example

Let’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):

  • When you approach an ATM, you are presented with a simple interface where you can:

    • Insert your card.
    • Enter your PIN.
    • Choose options like Check Balance, Withdraw Money, or Deposit Funds.
  • Abstraction hides all the complex operations happening inside the ATM. You don’t need to know how the ATM processes your card, how it communicates with your bank, or how it dispenses cash. You only see a high-level interface with clear options to interact with.

What Happens Behind the Scenes (Complexity Hidden):

  • The ATM internally connects to your bank’s server, verifies your identity, checks available funds, and interacts with other systems to process your request. The entire backend operation (like database queries, transaction processing, security checks, etc.) is hidden from the user.

    You don’t need to know how the ATM is performing these tasks. You are only concerned with the final outcome: receiving the requested service, whether it’s a balance inquiry or withdrawing money.

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 {
private:
int a, b; // Private data members

public:
// Public method to set values for private members
void set(int x, int y) {
a = x; // Set value of a
b = y; // Set value of b
}

// Public method to display the values of private members
void display() {
cout << “a = ” << a << endl; // Display value of a
cout << “b = ” << b << endl; // Display value of b
}
};

int main() {
ImplementAbstraction obj; // Create object of ImplementAbstraction class
obj.set(10, 20); // Set values of a and b using set method
obj.display(); // Display values of a and b using display method

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 and b). 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() and display() 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 and b 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.