Encapsulation is a primary aspect of object-oriented programming languages.
It entails the combination of data elements and methods within a single class.
Putting related data members and functions into the same class can assist in data hiding.
What is Encapsulation in C++?
Encapsulation in C++ includes merging similar data and functions into a single entity called a class.
By binding these functions and data, we secure them against conversion (data hiding).
In C++, it is possible to encapsulate data members and functions that run in concert within a single class. For example:
class Rectangle { public: int length; int width; int getArea() { return length * width; } };
In the preceding code, the getArea()
function estimates the area of a rectangle.
To determine the area, the program calculates the length and width.
The Rectangle class keeps the data members (length and width) and the getArea()
method together.
What is the meaning of encapsulation in OOP?
C++ is built on notions of object-oriented programming (OOPs), and encapsulation is among these ideas.
The goal of encapsulation is to avoid direct access to the data.
What is the difference between encapsulation and abstraction?
In object-oriented programming, encapsulation is a syntax that links together data and functions.
This technique prevents both outside conflict and misuse.
On the other hand, data abstraction is a way to hide operation details from the user and only show the interfaces.
What is the syntax of encapsulation?
The C++ language implies encapsulation through the use of classes and C++ access modifiers. Examine the example:
#include<iostream> using namespace std; class encapsulation { private: int a; public: void set(int b) { x = b; } int get() { return a; } }; // main function int main() { encapsulation obj; obj.set(10); cout<<obj.get(); return 0; }
This example syntax will produce the following output:
10
In the following code, the variable a is private.
This can only be read and modified with the class-specific procedures get()
and set()
.
Learn more about C++ Syntax with our complete tutorial.
What is the best example of encapsulation?
Containers are the best examples of encapsulation in programming, which encapsulates the data and methods into a single unit.
What are the benefits of encapsulation?
- It hides data from other methods.
- Keeps the data private and accessed only within the class.
- It secures data against foreign access.
- Suitable for the marketing and finance industries that require data protection and restricted access.
- Facilitates the binding of a class’s member functions and data.
- Assists in creating flexible code that is simple to modify and maintain.
What are the types of data encapsulation?
There are three ways to apply encapsulation in C++.
- Member Variable Encapsulation – All data members need to be private in this type of encapsulation.
- Function Encapsulation – Some of the member functions in this type of encapsulation are private. The constructor is accessible to the public.
- Class Encapsulation – In this sort of encapsulation, a class’s whole contents are private. This basically applies in nesting courses.
What is encapsulation in C++ with real-world examples?
Encapsulation means that a function made inside a class must use all of its member variables.
Consider a real-world example of encapsulation: in a business, there are various departments such as accounts, finance, sales, etc.
The financial sector handles all financial transactions and maintains records of all financial data.
A circumstance may arise in which a finance department executive needs all sales information for a given month.
In this instance, he cannot have direct access to the sales section’s data.
He must first contact another officer in the sales department and request that individual data be provided.
This describes encapsulation. Here, the data of the sales section and the employees with access to it are grouped together under the name “sales section”.
Example programs for C++ Encapsulation
Example program: Calculate the area of a rectangle using C++ encapsulation.
#include <iostream> using namespace std; class Rectangle { public: int length; int width; Rectangle(int len, int wid) : length(len), width(wid) {} int getArea() { return length * width; } }; int main() { Rectangle rect(5, 7); cout << "Rectangle area = " << rect.getArea(); return 0; }
Output:
Rectangle area = 35
To calculate area, we require two variables: length and width, as well as the getArea()
function.
Here, the functions that manipulate variables and methods are also accessible from other classes (not data hiding).
Why Encapsulation?
- Encapsulation in C++ lets us keep similar data and functions together, making our code easier to read. It aids in regulating the alteration of our data members.
Consider a circumstance in which the length field of a class must be non-negative. Here, we may make the length variable private and implement the logic within thesetAge()
function.
Example Program:
class Rectangle { private: int age; public: void setLength(int len) { if (len >= 0) length = len; } };
The getter and setter methods allow read-only or write-only access to the members of our class.
For example:
get()
– provides read-only accessset()
– provides write-only access
- It aids in decoupling system components. For example, the codes were assigned to numerous bundles.
These detached components (bundles) can be individually and concurrently produced, tested, and debugged. And modifications to a specific component do not affect other components.
- Encapsulation also applies in hiding data. If the length and width variables in the example are set to “private” or “protected,” then no one can access these fields.
Data Hiding
Data hiding is a method for restricting data member access by concealing implementation details.
Using access modifiers, we can conceal data in C++.
For example:
#include <iostream> using namespace std; class Rectangle { private: int length; int width; public: void setLength(int len) { length = len; } void setWidth(int brth) { width = wid; } int getLength() { return length; } int getWidth() { return width; } int getArea() { return length * width; } }; int main() { Rectangle rectangle1; rectangle1.setLength(5); rectangle1.setWidth(7); cout << "Length = " << rectangle1.getLength() << endl; cout << "Width = " << rectangle1.getWidth() << endl; // Call getArea() function cout << "Area = " << rectangle1.getArea(); return 0; }
Output:
Length = 5 With = 7 Area = 35
Here, the length and width variables are made private.
Therefore, these variables cannot be accessed directly outside of the Rectangle class.
We have used public functions setLength()
and getLength()
, setWidth()
and getWidth()
to access these private variables()
or known as getter and setter.
Conclusion
The notion of encapsulation is shown with a real-world analog and a straightforward programming example.
In turn, this makes data more secure and prevents malicious use.
All in all, we examined abstraction and encapsulation ideas in C++ in this tutorial.