⬅ Previous Next ➡

Encapsulation & Abstraction

Encapsulation & Abstraction in C++
  • Encapsulation means wrapping data (variables) and methods (functions) into a single unit (class).
  • Data Hiding is achieved by keeping data members private and providing controlled access using public methods.
  • Abstraction means showing only important features and hiding internal implementation details.
  • Encapsulation + Abstraction helps in building secure, maintainable, and reusable OOP programs.

1) Data Hiding (Private Data Members)

  • Keep sensitive data private.
  • Outside code cannot access private members directly.
#include <iostream>
using namespace std;

class Account {
private:
    double balance; // hidden data

public:
    void setBalance(double b) {
        if (b >= 0) balance = b;
    }

    double getBalance() {
        return balance;
    }
};

int main() {
    Account a;
    a.setBalance(5000);
    cout << "Balance: " << a.getBalance() << endl;
    return 0;
}

2) Getters and Setters

  • Getter: returns value of private data.
  • Setter: updates private data with validation (security).
#include <iostream>
using namespace std;

class Student {
private:
    int age;

public:
    void setAge(int a) {
        if (a > 0 && a <= 120) age = a;
    }

    int getAge() {
        return age;
    }
};

int main() {
    Student s;
    s.setAge(21);
    cout << "Age: " << s.getAge() << endl;
    return 0;
}

3) Abstraction (Hide Implementation)

  • User interacts with public methods, internal logic stays hidden.
  • Example: user calls deposit() and does not care how it updates balance.
#include <iostream>
using namespace std;

class Bank {
private:
    double balance;

public:
    Bank() : balance(0) {}

    void deposit(double amt) {
        if (amt > 0) balance += amt;
    }

    void withdraw(double amt) {
        if (amt > 0 && amt <= balance) balance -= amt;
    }

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

int main() {
    Bank b;
    b.deposit(2000);
    b.withdraw(500);
    b.showBalance();
    return 0;
}

4) Abstract Data Type (ADT) Concept

  • ADT defines what operations can be performed, not how they are implemented.
  • Example ADTs: Stack, Queue, List.
  • In OOP, ADTs are designed using classes with well-defined methods.
// Example ADT idea (Stack):
// Operations: push(), pop(), top(), isEmpty()
// Implementation can be array-based or linked-list-based.

5) Access Modifiers (public / private / protected)

  • public: accessible from anywhere.
  • private: accessible only inside the class (best for data hiding).
  • protected: accessible in class and derived classes.

6) Class Design Principles (OOP)

  • Single Responsibility: one class should have one main job.
  • Encapsulate data: keep variables private and expose only necessary methods.
  • Meaningful names: class and methods should clearly represent purpose.
  • Low coupling: classes should not depend too much on each other.
  • High cohesion: related functionalities should stay together in one class.

7) Quick Notes

  • Encapsulation = binding data + functions inside class.
  • Data hiding is achieved using private members.
  • Getters/Setters provide controlled access and validation.
  • Abstraction hides internal details and shows only essential methods.
⬅ Previous Next ➡