Encapsulation & Abstraction in C++
When a program becomes larger, simply putting data and functions inside classes is not enough. You also need to control who can access the data and decide which details should be visible.
This is where encapsulation and abstraction are used. Both are important OOP concepts in C++, but they solve different problems.
Encapsulation focuses on keeping data and related functions together while controlling access to that data. Abstraction focuses on hiding unnecessary implementation details and showing only what is needed.
Encapsulation in C++
Encapsulation in C++ means combining data and the functions that work with that data inside a class.
For example:
class Student
{
private:
int marks;
public:
void setMarks(int m)
{
marks = m;
}
int getMarks()
{
return marks;
}
};
Here, marks and the functions that work with it are part of the same class.
The private keyword also prevents code outside the class from directly changing marks.
You can access it through the public functions:
Student s;
s.setMarks(85);
cout << s.getMarks();
Output
85
This gives the class control over how its data is accessed or changed.
Data Hiding in C++
Data Hiding in C++ means preventing direct access to certain data from outside the class.
The private access specifier is commonly used for this purpose.
class BankAccount
{
private:
double balance;
public:
void setBalance(double amount)
{
if (amount >= 0)
balance = amount;
}
double getBalance()
{
return balance;
}
};
Here, balance cannot be changed directly:
BankAccount account;
// account.balance = 5000; // Error
Instead, the setBalance() function controls how the value is changed.
This can also help prevent invalid values from being stored.
Abstraction in C++
Abstraction in C++ means showing the important part of something while hiding the details of how it works.
Think about using a car. You press the brake pedal to stop the car, but you don’t need to know everything happening inside the braking system.
The same idea can be used in programming.
For example:
class ATM
{
public:
void withdraw()
{
checkBalance();
verifyAccount();
processPayment();
}
private:
void checkBalance()
{
// balance checking
}
void verifyAccount()
{
// account verification
}
void processPayment()
{
// payment processing
}
};
A user only needs to call:
ATM atm;
atm.withdraw();
The internal steps remain hidden.
Abstract Classes in C++
An abstract class is a class that contains at least one pure virtual function. It is used to provide a common structure for derived classes.
class Shape
{
public:
virtual void draw() = 0;
};
The draw() function is a pure virtual function because it is followed by = 0.
You cannot create an object of Shape directly:
Shape s; // Error
A derived class must provide the implementation:
class Circle : public Shape
{
public:
void draw() override
{
cout << "Drawing Circle";
}
};
Now you can create a Circle object:
Circle c;
c.draw();
Abstract classes are useful when you want different classes to follow the same basic design while allowing each class to provide its own implementation.
Interface-like Design
C++ does not have a separate interface keyword like some other programming languages. However, you can create an interface-like design using an abstract class containing pure virtual functions.
class Payment
{
public:
virtual void pay() = 0;
virtual void refund() = 0;
};
A derived class can then implement these functions:
class UPI : public Payment
{
public:
void pay() override
{
cout << "Payment completed";
}
void refund() override
{
cout << "Refund completed";
}
};
The base class defines what the class should do, while the derived class decides how to do it.
Encapsulation vs Abstraction
Although they are related, they are not the same.
| Encapsulation | Abstraction |
|---|---|
| Protects and organizes data | Hides unnecessary implementation details |
| Uses classes and access specifiers | Commonly uses abstract classes and functions |
| Focuses on controlling access | Focuses on showing only what is needed |
In simple terms, encapsulation controls access, while abstraction hides complexity.
Both concepts help make C++ programs easier to organize, maintain, and use. Understanding Encapsulation and Abstraction in C++ also makes it easier to understand how real-world applications are designed using OOP.
Frequently Asked Questions (FAQs)
Q1. Why is encapsulation important in C++?
Encapsulation in C++ keeps data and the functions that operate on it together while controlling how that data can be accessed.
Q2. How does data hiding work in C++?
Data Hiding in C++ restricts direct access to class members, commonly by declaring data as private and providing controlled public methods.
Q3. What is the difference between encapsulation and abstraction?
Encapsulation focuses on protecting and controlling access to data, while Abstraction in C++ hides unnecessary implementation details and exposes only essential functionality.
Q4. Why are abstract classes used in C++?
Abstract Classes in C++ provide a common structure for derived classes and can define functions that derived classes are required to implement.
Q5. Can C++ create interfaces like other programming languages?
C++ does not have a dedicated interface keyword, but an interface-like design can be created using abstract classes with pure virtual functions.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
