0

Help me

Can anybody help me to understand this program #include <iostream> #include <string> using namespace std; class myClass { public: void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass myObj; myObj.setName("John"); cout << myObj.getName(); return 0; }

5th Dec 2016, 10:49 AM
Arpit Joshi
Arpit Joshi - avatar
1 Answer
+ 3
Its encapsulation. You access to a private class member (name , in this case) , using methods (get and set). get will return the name, and set will assign a new value. You can do myObj.name = "something", because you can't access to a private member outside the class.
5th Dec 2016, 10:54 AM
Nahuel
Nahuel - avatar