Encapsulation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson


can anyone please explain with an easy code about encapsulation?

14th Jan 2018, 6:32 PM
Mahmud Farabi
Mahmud Farabi - avatar
1 Answer
+ 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; }
14th Jan 2018, 6:49 PM
Shubham Dalal
Shubham Dalal - avatar