Encapsulation
Introduction to Encapsulation in C++
Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP). It's a mechanism of wrapping the data (variables) and code acting on the data (methods or functions) together as a single unit. In C++, this is achieved using the concept of 'classes'.
Understanding Encapsulation
In a real-world scenario, consider a medical capsule, where the medicine (data) is encapsulated within the capsule shell (methods). You don't need to know what kind of medicine is inside or how it was made; you just need to know how to consume it to get the desired effect.
Similarly, in OOP, encapsulation helps to hide the data from outside world and prevents it from being accidentally modified. The data is only accessible through methods, keeping it safe from accidental changes and misuse.
Benefits of Encapsulation
Data Hiding: The user will not have any idea about the inner implementation of the class. It will not be visible to the user how the class is stored values in the variables. The user can only know that we are passing the values to accessors and they are getting values from mutators.
Increased Flexibility: We can make the variables of the class read-only or write-only depending on our need. If we wish to make a variable as read-only then we have to omit the mutator method like
setXXX()
in this case. If we wish to make a variable as write-only then we have to omit the accessor method likegetXXX()
in this case.Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
Testing code is easy: Encapsulated code is more flexible and easy to test for unit testing.
Implementing Encapsulation in C++
In C++, we use Classes to implement encapsulation. A Class is a user-defined data type. It consists of data members and member functions, which can be accessed and used by creating an instance of that class.
Here's a simple example of encapsulation using a class in C++:
class Employee {
private: //Private data members
int id;
string name;
public: //Public member functions
void setId(int id) { //Setter
this->id = id;
}
int getId() { //Getter
return id;
}
void setName(string name) { //Setter
this->name = name;
}
string getName() { //Getter
return name;
}
};
int main() {
Employee emp1;
emp1.setId(101);
emp1.setName("John Doe");
cout << "Employee ID: " << emp1.getId() << endl;
cout << "Employee Name: " << emp1.getName() << endl;
return 0;
}
In the above code, id
and name
are private data members, and setId
, getId
, setName
, and getName
are public member functions. These member functions are the only way to access and modify the private data members.
Encapsulation, thus, is a protective shield that prevents the data from being accessed by the code outside this shield. This is not just a safety mechanism, but it also aligns with the real-world scenario where certain details are kept hidden to maintain security and functionality.
Remember, the power of encapsulation lies in the ability to hide and control who has access to what data to maintain the integrity of the data and prevent unauthorized access. So, use it wisely in your code and happy coding!