What are the differences between public, private, and
protected access?
Answer Posted / swetcha
We use these keywords to specify access levels for member
variables, or for member functions (methods).
.Public variables, are variables that are visible to all
classes.
.Private variables, are variables that are visible only to
the class to which they belong.
.Protected variables, are variables that are visible only
to the class to which they belong, and any subclasses.
Deciding when to use private, protected, or public
variables is sometimes tricky. You need to think whether or
not an external object (or program), actually needs direct
access to the information. If you do want other objects to
access internal data, but wish to control it, you would
make it either private or protected, but provide functions
which can manipulate the data in a controlled way.
| Is This Answer Correct ? | 172 Yes | 22 No |
Post New Answer View All Answers
Will c++ be replaced?
Is swift a good first language?
What are enumerations?
Mention the purpose of istream class?
What are the advantages of early binding?
What are inline functions? What is the syntax for defining an inline function?
Which field is used in c++?
Explain what is class definition in c++ ?
What is size_type?
What is buffering in c++?
Write a function to find the nth item from the end of a linked list in a single pass.
What is #include sstream?
What is the benefit of encapsulation?
Using a smart pointer can we iterate through a container?
Define pre-condition and post-condition to a member function in c++?