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

Does c++ have string data type?

689


What is cin clear () in c++?

610


What are the advantages of using a pointer? Define the operators that can be used with a pointer.

602


What does int * mean in c++?

633


What is DlgProc?

597






Can we make copy constructor private in c++?

593


Can circle be called an ellipse?

633


Why use of template is better than a base class?

642


What is null c++?

586


What is virtual base class?

567


What are the comments in c++?

572


Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].

601


What is c++ mutable?

702


What is difference between class and function?

585


write a corrected statement in c++ so that the statement will work properly. x = y = z + 3a;

1453