Absraction & Encapsulation
Absraction & Encapsulation
Bundling similar data members and functions inside a class together also
helps in data hiding.
C++ Encapsulation
In general, encapsulation is a process of wrapping similar code in one place.
In C++, we can bundle data members and functions that operate together
inside a single class. For example,
class Rectangle {
public:
int length;
int breadth;
int getArea() {
return length * breadth;
}
};
class Rectangle {
public:
// Variables required for area calculation
int length;
int breadth;
int main() {
// Create object of Rectangle class
Rectangle rect(8, 6);
return 0;
}
Run Code
Output
Area = 48
Here, the variables and functions can be accessed from other classes as well.
Hence, this is not data hiding.
This is only encapsulation. We are just keeping similar codes together.
Note: People often consider encapsulation as data hiding, but that's not
entirely true.
Encapsulation refers to the bundling of related fields and methods together.
This can be used to achieve data hiding. Encapsulation in itself is not data
hiding.
Why Encapsulation?
In C++, encapsulation helps us keep related data and functions together,
which makes our code cleaner and easy to read.
class Rectangle {
private:
int age;
public:
void setLength(int len) {
if (len >= 0)
length = len;
}
};
The getter and setter functions provide read-only or write-only access to our
class members. For example,
And, they are kept hidden from outer classes. This is called data hiding.
Data Hiding
Data hiding is a way of restricting the access of our data members by hiding
the implementation details. Encapsulation also provides a way for data hiding.
We can use access modifiers to achieve data hiding in C++. For example,
Example 2: C++ Data Hiding Using the private Specifier
#include <iostream>
using namespace std;
class Rectangle {
private:
public:
int main() {
// Create object of Rectangle class
Rectangle rectangle1;
return 0;
}
Run Code
Output
Length = 8
Breadth = 6
Area = 48
Data abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and
hiding the details. Data abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or implementation.
Consider a real-life example of a man driving a car. The man only knows that pressing
the accelerator will increase the speed of the car or applying brakes will stop the car but
he does not know how on pressing the accelerator the speed is actually increasing, he
does not know about the inner mechanism of the car or the implementation of the
accelerator, brakes, etc in the car. This is what abstraction is.
Types of Abstraction:
1. Data abstraction – This type only shows the required information about the data and
hides the unnecessary data.
2. Control Abstraction – This type only shows the required information about the
implementation and hides unnecessary information.
Abstraction using Classes
We can implement Abstraction in C++ using classes. The class helps us to group data
members and member functions using available access specifiers. A Class can decide
which data member will be visible to the outside world and which is not.