+ 2

In the below program How can i print the name value on the screen?

#include <iostream> #include <string> using namespace std; class myClass { public: void setName(string x) { name = x; } private: string name; }; int main() { myClass myObj; myObj.setName("John"); return 0; }

13th Mar 2017, 5:33 AM
Leo
2 ответов
+ 2
You need to add a function to get the name of myClass.
13th Mar 2017, 5:39 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
If the 'name' variable were public, it would be easy. In this case, a getter method is what you need.
13th Mar 2017, 9:50 AM
Norbivar
Norbivar - avatar