Encapsulation and Abstraction in C++ – Complete Tutorial
Last Updated on: 17th Dec 2025 17:32:57 PM
Encapsulation – Concept Explanation
Encapsulation is the process of wrapping data (variables) and code (functions) together into a single unit, called a class.
It also involves restricting direct access to data and allowing access only through controlled methods.
Encapsulation helps in:
-
Protecting data from misuse
-
Improving security
-
Maintaining control over data modification
-
Making code easier to manage
Encapsulation is achieved in C++ using:
-
Classes
-
Access specifiers (
private,public,protected)
Encapsulation – Definition
Encapsulation is a mechanism that binds data members and member functions together and hides the internal details of an object from the outside world.
Real-life example (Encapsulation)
A bank account:
-
Balance is hidden
-
You cannot change balance directly
-
You must use deposit or withdraw methods
Example
#include <iostream>
using namespace std;
class BankAccount {
private:
int balance;
public:
BankAccount(int b) {
balance = b;
}
void deposit(int amount) {
balance += amount;
}
void withdraw(int amount) {
if (amount <= balance)
balance -= amount;
else
cout << "Insufficient balance" << endl;
}
void showBalance() {
cout << "Current Balance: ₹" << balance << endl;
}
};
int main() {
BankAccount acc(5000);
acc.deposit(2000);
acc.withdraw(1000);
acc.showBalance();
return 0;
}
Output
Current Balance: ₹6000
Encapsulation – Short Explanation of Output
The balance cannot be accessed directly.
It is safely modified using public methods, ensuring data protection.
Advantages of Encapsulation
-
Data security
-
Controlled access
-
Reduced complexity
-
Easy maintenance
PART 2: Abstraction in C++
Abstraction – Concept Explanation
Abstraction means showing only essential details and hiding implementation details.
It focuses on what an object does, not how it does it.
In C++, abstraction is implemented using:
-
Abstract classes
-
Pure virtual functions
Abstraction – Definition
Abstraction is a concept of hiding unnecessary implementation details and exposing only the relevant functionality to the user.
Real-life example (Abstraction)
Using a TV remote:
-
You press buttons
-
You don’t know internal circuits
Abstraction Using Abstract Class
An abstract class contains at least one pure virtual function.
Abstraction – Complete Program
#include <iostream>
using namespace std;
class Payment {
public:
virtual void pay() = 0; // pure virtual function
};
class CardPayment : public Payment {
public:
void pay() {
cout << "Payment done using Debit/Credit Card" << endl;
}
};
class UPIPayment : public Payment {
public:
void pay() {
cout << "Payment done using UPI" << endl;
}
};
int main() {
Payment* p;
CardPayment card;
UPIPayment upi;
p = &card;
p->pay();
p = &upi;
p->pay();
return 0;
}
Output
Payment done using Debit/Credit Card
Payment done using UPI
Abstraction – Short Explanation of Output
The user interacts with the Payment interface.
Actual payment logic depends on the object type at runtime.
Difference Between Encapsulation and Abstraction
| Feature | Encapsulation | Abstraction |
|---|---|---|
| Purpose | Protect data | Hide complexity |
| Focus | How data is accessed | What functionality is provided |
| Achieved using | Access specifiers | Abstract classes |
| Level | Implementation level | Design level |
Encapsulation vs Abstraction – Real-life View
-
Encapsulation: ATM machine protects internal cash logic
-
Abstraction: ATM shows only required operations like withdraw, check balance
Advantages of Abstraction
-
Reduces complexity
-
Improves flexibility
-
Supports scalability
-
Enhances code reusability
Summary
-
Encapsulation protects data by restricting access
-
Abstraction hides internal logic and shows only essential features
-
Both are core principles of Object Oriented Programming
-
Together, they help build secure, maintainable, and scalable software
Keep practicing — you're doing amazing!
Happy Coding! ![]()