0
Encapsulation
can anyone please explain with an easy code about encapsulation?
1 Antwort
+ 4
// 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;
}
};
//only public member function of this class can access the private variables.
//which means that our variable x is hidden from outside world
// main function
int main() {
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}