Abstraction in C++

 


1️⃣ What is Abstraction?

Abstraction is a fundamental concept in Object-Oriented Programming (OOP) that hides unnecessary details and shows only the essential features of an object.

🔹 Key Features:
✅ Hides implementation details from the user.
✅ Only relevant information is exposed.
✅ Increases security by restricting direct access to data.
✅ Implemented using classes, access specifiers (private, protected, public), and abstract classes.


2️⃣ Example: Abstraction using Classes

In this example, the BankAccount class hides the balance details and provides public methods for interaction.

#include <iostream>
using namespace std;

class BankAccount {
private:
    double balance;  // Private data (Hidden from user)

public:
    BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    void deposit(double amount) {
        balance += amount;
        cout << "Deposited: " << amount << endl;
    }

    void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            cout << "Withdrawn: " << amount << endl;
        } else {
            cout << "Insufficient balance!" << endl;
        }
    }

    void showBalance() {
        cout << "Current Balance: " << balance << endl;
    }
};

int main() {
    BankAccount acc(1000);  // Create account with initial balance

    acc.deposit(500);
    acc.withdraw(300);
    acc.showBalance();

    // acc.balance = 5000;  // ❌ Not allowed (balance is private)

    return 0;
}

🔹 Output

Deposited: 500
Withdrawn: 300
Current Balance: 1200

✅ Why use abstraction?
✔ Users can only interact through deposit(), withdraw(), and showBalance().
✔ The balance variable remains hidden and cannot be modified directly.


3️⃣ Example: Abstraction Using Abstract Classes

Abstract classes enforce only the essential behavior while hiding implementation details.

#include <iostream>
using namespace std;

class Shape {  // Abstract class
public:
    virtual void draw() = 0;  // Pure virtual function (abstraction)
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Drawing Rectangle" << endl;
    }
};

int main() {
    Shape* s1 = new Circle();
    Shape* s2 = new Rectangle();

    s1->draw();  // Calls Circle's draw() function
    s2->draw();  // Calls Rectangle's draw() function

    delete s1;
    delete s2;

    return 0;
}

🔹 Output

Drawing Circle
Drawing Rectangle

✅ Why use abstraction here?
✔ Shape class provides an interface for all derived classes.
✔ The internal details of how each shape is drawn are hidden from the user.


4️⃣ Key Points About Abstraction in C++

✔ Hides unnecessary details and shows only the required functionality.
✔ Implemented using classes and access specifiers (private, protected, public).
✔ Abstract classes enforce abstraction by requiring derived classes to implement certain functions.
✔ Improves security, modularity, and maintainability of code.

Would you like an example with file handling abstraction? 🚀

Post a Comment

0 Comments