+ 2
How do i print the setname john to 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; }
2 Answers
+ 14
You can make a method to retrieve a private value (called a getter) to go with your setter method setName(). Put this under the public header:
string getName() { return name; }
And use it like this:
cout << myObj.getName();
- 2
Cout>> name;
put that before the return