Data Encapsulation

When you define a class, you also specify the private members, that is the member that are not available for external access, and the public members of that class. An application program access objects by using the public methods of the class and thus activating capacities.

Example of data encapsulation:

// c++ program to explain 
// Encapsulation 

using namespace std; 

class Encapsulation 
		// data hidden from outside world 
		int x; 
		// function to set value of 
		// variable x 
		void set(int a) 
			x =a; 
		// function to return value of 
		// variable x 
		int get() 
			return x; 

// main function 
int main() 
	Encapsulation obj; 
	return 0; 
Translate »