×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

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!    yes

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo