0

access specifier

The code was giving no output when attribute name was private. it is still not giving output when I set the access specifier to public. #include <iostream> #include <string> using namespace std; class myClass { public: void setName(string x) { name = x; } public: string name; }; int main() { myClass myObj; myObj.setName("John"); return 0; }

18th Jun 2017, 4:28 AM
Mrityunjay Mohan Borah
Mrityunjay Mohan Borah - avatar
7 odpowiedzi
+ 4
You are only setting the name instance variable. Now you need to either make a get function or make a print function. void printName () { cout << name; } // in main myObj.setName ("Fred"); myObj.printName (); cout << endl;
18th Jun 2017, 5:36 AM
Zeke Williams
Zeke Williams - avatar
+ 3
Why should it give output? The program never prints anything.
18th Jun 2017, 5:02 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
hi, access specifiers determines the scope of variable. where it can be accessed some thing like that.
18th Jun 2017, 5:56 AM
Nanda Balakrishnan
- 1
O
28th Sep 2017, 12:33 PM
Gaurav kumar
Gaurav kumar - avatar
- 1
Google
28th Sep 2017, 12:34 PM
Gaurav kumar
Gaurav kumar - avatar