Object Oriented Programming is a way to integrate with objects which can contain data (in the form of attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributes, methods) and we should protect each member inside this class. How we can use these all members inside the class and how we can protect them from other classes, applications, or users? The answer is, you all need to learn Encapsulation.
Encapsulation term in C++ is used to cover attributes (properties) and methods (functions) inside a single statement called Class. In another term, it is wrapping up data and information under a single unit. Encapsulation is needed to be sure that sensitive data is hidden from users. It is to prevent access to the data directly, the access to them is provided through the functions of the class.
Data Encapsulation term is a mechanism of bundling the data, and the functions that use them and Data Abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user. This is mostly related to attributes in a class.
We can do encapsulation by using Classes wisely, we must declare these class attributes or methods as private which means they cannot be accessed from outside the class. They also can be used inside the class or by the class attributes of that class or by the methods in that class.
To do this:
1. We should make all the data members private
2. We should create public setter and getter functions for each data member in such a way that the set function sets the value of the data member and get the function get the value of the data member.
Note that if we want others to read or modify the value of a private member of a class, we can use public get and set methods to reach these private members.
For example, let’s assume that salary of a human is a private thing, and let’s define this as private. In this example, we can define set_salary() and get_salary() public methods and we can set and get private salary value by using these functions. Here is a THuman class with set and get Encapsulation methods used,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class THuman { private: int salary; public: void set_salary(int sal) { salary = sal; } int get_salary() { return salary; } }; |
Note that these set_salary() and get_salary() methods are defined in public section. Now, we can set salary by using set_salary() method and we can get salary value by using get_salary() methods. Here is the full code to show how to use these Encapsulation methods,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> class THuman { private: int salary; public: void set_salary(int sal) { salary = sal; } int get_salary() { return salary; } }; int main() { THuman worker; // define object by the THuman class worker.set_salary(50000); // set private variable by using a public function std::cout << "Salary: " << worker.get_salary() << '\n'; // get private member value by using a public function getchar(); return 0; } |
Encapsulation methods are really necessary for the increased security of data. Thus, we highly recommend that declare your class attributes as private as you can. Encapsulation allows you or others to change one part of the data without affecting other parts of data, which means it ensures better control on your data. These kinds of methods show how the C++ Programming language is safe and secure in data-integrated applications.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition