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


Please Help Members By Posting Answers For Below Questions

What is a block in c++?

557


Is it possible to use a new for the reallocation of pointers ?

603


Which field is used in c++?

644


What is the benefit of c++?

607


Are strings immutable in c++?

664






What happens when you make call 'delete this;'?

607


How many static variables are created if you put one static member into a template class definition?

573


What is a forward referencing and when should it be used?

581


What is name hiding in c++?

623


When should we use multiple inheritance?

621


Define pointers?

613


What is a manipulative person?

549


You run a shell on unix system. How would you tell which shell are you running?

667


Which bit wise operator is suitable for checking whether a particular bit is on or off?

615


What is the best c++ ide?

610