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 class invariant in c++?

736


When is the copy constructor called?

619


Is swift better than c++?

529


How does c++ sort work?

549


What is near, far and huge pointers? How many bytes are occupied by them?

645






What is the difference between reference and pointer?

607


What is constructor and destructor in c++?

607


What is do..while loops structure?

611


What is DlgProc?

588


Differentiate between C and C++.

703


What are the two types of polymorphism?

583


What is a try block?

632


Why do we need c++?

589


Is overriding possible in c++?

565


When are exception objects created?

601