+ 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; }

19th Aug 2017, 7:42 AM
stephen haokip
stephen haokip - avatar
2 Antworten
+ 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();
19th Aug 2017, 8:08 AM
Tamra
Tamra - avatar
- 2
Cout>> name; put that before the return
19th Aug 2017, 7:48 AM
Kittiphat Paopeng