C++ Encapsulation in OOP | Encapsulation in C++ with Examples | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation | OOP Tutorial in C++
In this tutorial, we are going to learn about the C++ Inheritance in OOP , Inheritance in C++, Types of Inheritance in OOP, Advantage and Disadvantages of using Inheritance, OOP Tutorial in C++
What is Encapsulation in C++?
Encapsulation means the process of wrapping up the data and functions in a single capsule.
Encapsulation also lead to data abstraction or hiding. As using encapsulation also hides the data.
The common example of encapsulation is Capsule. In capsule all medicine are encapsulated in side capsule.
How Encapsulation is achieved in a class:
To do this:
1) Make all the data members private.
2) Create public setter and getter functions for each data member in such a way that the set function set the value of data member and get function get the value of data member.
Why Encapsulation?
- Increased security of data.
- Encapsulation ensures better control of your data.
- Member variables are made private so that these cannot be directly accessed from outside the class .
- Encapsulation also helps us to make a flexible code which is easy to change and maintain .
- Encapsulation is also useful in hiding the data .
- Encapsulation helps us in binding the data .
Benefits of encapsulation:
There are various benefits of encapsulated classes:
- Provides abstraction between an object and its clients.
- Encapsulated classes reduce complexity.
- Help protect our data. A client cannot change an Account's balance if we encapsulate it.
- Encapsulated classes are easier to change.
- Protects an object from unwanted access by clients.
Dis-Advantage of Encapsulation in C++
You can't access private date outside the class.
Example of Encapsulation in C++:
#include <iostream>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}
#include <iostream>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}
Output:
50000
The salary attribute is private, which have restricted access.
The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary = s).
The public getSalary() method returns the value of the private salary attribute.
Inside main(), we create an object of the Employee class. Now we can use the setSalary() method to set the value of the private attribute to 50000. Then we call the getSalary() method on the object to return the value.
Difference Between Data Hiding and Encapsulation:
Data Hiding and Encapsulation are two concepts of OOP.
Data hiding is the process of protecting the members of the class from unauthorized access while encapsulation is the process of wrapping the data members and methods into a single unit. This is the key difference between data hiding and encapsulation.
Data hiding focus on securing the data while hiding the complexity of the system. Encapsulation mainly focuses on hiding the complexity of the system. Encapsulation is a way of achieving data hiding.
Next Post:
Previous Post:
Inheritance in C++ ~ Types of Inheritance in C++ with Examples ~ OOP in C++
0 Response to C++ Encapsulation in OOP | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation
Post a Comment