There are 'public', 'private', and 'protected' in oop like c++ language. And I tried two kinds of simple programs.
Below is first case in c++.
class A {
public:
string name;
}
int main(void) {
A a;
a.name;
}
And, second case...
class A {
protected:
string name;
public:
string getName(void) {
return name;
}
}
int main(void) {
A a;
//a.name; //can't access
cout << a.getName();
}
Which one is better in two cases?
Because the information has to be hidden, the second one is maybe better, I think. But in second one, it can also access 'name' variable by using function getName()
. If so, although the first one is simpler than second one, why should I use the second one? In other words, why is protected
used?