Encapsulation

Encapsulation is implemented in C++ as a class that encapsulates data and the methods that operate on it. Data is typically designated as private so that it cannot be accessed outside of the class. The public functions are defined and may be retrieved by using the class’s objects.

Two properties of Encapsulation:

  • Data member and variables are declared private to ensure security.
  • Member function are declared public so so that anyone can’t change and work according to that function.
#include <iostream>
using namespace std;
class temp{
	int a;
	int b;
public:
	int solve(int input){
		a = input;
		b = a/2;
	return b;
}
};
 
int main() {
	int n;
	cin >> n;
	temp half;
	int ans = half.solve(n);
	cout << ans << endl;	
}