C++ Encapsulation is a feature of object oriented programming OOP. Data encapsulation is the concept of binding data and methods in a single unit called class. Encapsulation means hiding some features of code from the user. Combining similar data like variables and functions in a single class helps to achieve encapsulation.
A class is a combination of data and functions. There are two parts of C++ program:
- Program Functions: These are the code statements that perform functions.
- Program Data: These are variables or constants that are used in functions.
The class members variables and functions have private access specifier in encapsulation. In order to access (read or write) the value of private variables, C++ provides public get and set methods.
Programmer can define get and set methods inside the class under public access specifier to access private members of class.
Get Method
Get function usually accesses and displays the value of private variable. The return type of get function can be any of the allowed return types including void, int, char, double etc. The definition of this function returns the private variable.
Set Method
Set function assigns a new value to the private variable. The return type of set function can be any of the allowed return types including void, int, char, double etc. but best practice is to define return type as void.
The below example shows how to use to use get set functions to read and modify private values. In the following code, the variable price is a private variable of class Book. That means only that the programmer can access it only from inside the class. The class Book has two public members that are getPrice and setPrice functions. These public functions use the private data member. In this way, data encapsulation is achieved. getprice function takes no argument and returns the variable price. setPrice function takes integer value as an argument and assigns the integer value to the variable price.
#include <iostream> using namespace std; class Book { private: int price; public: void setPrice(int p) { price = p; } int getPrice() { return price; } }; int main() { Book book1; book1.setPrice(899); cout << book1.getPrice(); return 0; }
The output of the above code after compilation is:
The program can all get and set functions multiple times. Below is another examples that demonstrates the concept of encapsulation. In the following code, class Addition has a private data member, an integer type variable sum. Its value is defined as 0. The sumNum method acts as both set function and addition function. As it takes the sum of numbers entered by the user and also replaces the value in sum variable to the sum of numbers. The getSum method then returns the value of sum. In the main function the get and set functions are called multiple times for a single object.
#include <iostream> using namespace std; class Addition { public: Addition(int n = 0) { sum = n; } void sumNum(int num) { sum += num; } int getSum() { return sum; }; private: int sum; }; int main() { Addition a1; a1.sumNum(10); a1.sumNum(15); a1.sumNum(20); cout << "Total " << a1.getSum() <<endl; a1.sumNum(20); cout << "Total " << a1.getSum() <<endl; return 0; }
The output of the above code after compilation is:
Advantages of C++ Encapsulation
One main advantage of C++ Encapsulation is data hiding. Data encapsulation provides the ability to hide sensitive data from the user. At the same time, it provides user the flexibility to read and change the values of private members using getter and setter functions. The best practice is to declare all possible class attributes as private. With C++ encapsulation, the sensitive data is secure from outside threats and interference as the data is hidden. It also provides an interface to the user to use data members and related functions by creating objects of class.