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; }
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;
+ 3
Why should it give output? The program never prints anything.
+ 3
hi,
access specifiers determines the scope of variable.
where it can be accessed some thing like that.
- 1
O
- 1
Google