Difference Between Abstraction and Encapsulation in C++
What is Abstraction?
By concentrating on the most crucial elements and obscuring unimportant features, abstraction helps to make complicated systems simpler. It enables programmers to ignore certain implementation problems and operate at a higher level of abstraction.
“Abstraction creates a common structure and behaviour for objects without revealing implementation specifics by defining abstract classes, interfaces, or protocols.” This encourages modularity and code reuse. Complexity can be managed by abstraction, which also makes code easier to comprehend and makes maintenance simpler.
It offers a broad perspective on systems, enabling adaptable and flexible solutions. Programmers can produce software that is more adaptable and scalable by abstracting away specific specifics.
Applications
- Machine simplification
- code reuse
- flexible design
- easier library creation
What is Encapsulation?
“Encapsulation is a programming technique that entails grouping together into a single object the methods or functions that operate on the data.” By assuring data integrity and restricting access through clearly defined interfaces, it tries to safeguard data from unauthorised access and external code alteration.
We hide the internal implementation details and grant regulated access to the data through public methods or properties by enclosing the data inside an object.
Data protection, code organisation, and reusability are all facilitated by encapsulation. It enables the construction of independent objects that are simple to use and control inside of a programming. robust and maintainable code.
Applications
- conceals implementation details
- protects data
- assures data integrity
Difference between Encapsulation and Abstraction
Although they are connected, neither of the basic ideas of encapsulation and abstraction in the programming language object-oriented serve distinct operations:
Encapsulation:
- The primary objective of encapsulation is to shield an object's data from direct access while concealing its internal workings. It entails combining information and operations into a single item.
- Through limiting access to an object's data through methods (getters and setters), encapsulation aims to secure data and guarantee that the item's internal state remains consistent.
- Access modifiers (such private, protected, and public) in classes, which specify the visibility as well as accessibility of data and methods, are employed to accomplish encapsulated.
- The primary emphasis is on data hiding and access control, ensuring an independent distinction between an object's internal implementation and the external initiatives consuming it.
Abstraction:
- Through highlighting and highlighting crucial elements while hiding non-essential ones, abstraction aims to simplify complicated systems.
- It entails building a model or representation of something that only contains the elements that are absolutely necessary in the given situation.
- By creating abstract classes, interfaces, or protocols that describe the common structure and behaviour of objects without revealing implementation specifics, abstraction is achieved.
- Abstraction is used to enable code reuse, modularity, and easier maintenance by presenting a higher-level understanding and more broad view of the system.
- It enables programmers to deal with an object's fundamental properties without worrying about its particular implementation or low-level specifics.
Example
Following is a quick C++ instance showing how abstraction and encapsulation interact together:
Encapsulation Example:
#include <iostream>
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance) {
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
std::cout << "Insufficient funds!" << std::endl;
}
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount account(1000.0);
account.deposit(500.0);
account.withdraw(200.0);
double currentBalance = account.getBalance();
std::cout << "Current Balance: " << currentBalance << std::endl;
return 0;
}
Output
![Difference Between Abstraction and Encapsulation in C++](https://static.tutorialandexample.com/cpp/difference-between-abstraction-and-encapsulation-in-cpp1.png)
Abstraction Example
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle." << std::endl;
}
};
class Square : public Shape {
public:
void draw() override {
std::cout << "Drawing a square." << std::endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
shape1->draw();
shape2->draw();
delete shape1;
delete shape2;
return 0;
}
Output
![Difference Between Abstraction and Encapsulation in C++](https://static.tutorialandexample.com/cpp/difference-between-abstraction-and-encapsulation-in-cpp2.png)
Conclusion
Encapsulation is the process of organising and protecting data by combining it with related techniques into objects. On the other side, abstraction provides a generalised view and hides unneeded specifics to simplify complex systems. Abstraction makes systems simpler and encourages code reuse, whereas encapsulation focuses on data protection and code organisation.