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 

#include<iostream> 
using namespace std; 

class Encapsulation 
{ 
	private: 
		// data hidden from outside world 
		int x; 
		
	public: 
		// 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; 
	
	obj.set(5); 
	
	cout<<obj.get(); 
	return 0; 
} 
Translate »