What are the differences between public, private, and
protected access?
Answers were Sorted based on User's Feedback
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 |
Answer / manjusinga
public is used for global access that is its scope is not
only within the class but also outside the class.
private is used only in the class class in which it is
defined.It cannot be accessible by its derived members.
protected is used within the class in which it is defined
and derived members of it can also access it.
Is This Answer Correct ? | 133 Yes | 10 No |
Answer / golledla prathap reddy
public data can access anybody in the program.
private data can access olny members of that class.
protected also same like private but the difference is it
can access by derive class also.
we can see the use of protected data in inheritance.
Is This Answer Correct ? | 64 Yes | 12 No |
Answer / krishnachaitanya narne adeesh
Public data is used to access from any where in the program
means any class or any member function can utilize those
variables and member functions.
Where as Private data will not give access to any other
classes and also for derived classes it use those variables
only on particular class only.
Here Protected is similar to Private but difference is just
it gave access to its derived classes also. This is also
opposite to Public.
Is This Answer Correct ? | 46 Yes | 9 No |
Answer / bebo
prvate:is accessed by members of that class only.
public:is accessed by members of any class
protected: variables that are visible only
to the class to which they belong, and any subclasses.
Is This Answer Correct ? | 22 Yes | 4 No |
Answer / alka choubey
public:class members are access from the outside the class.
private:class members are access only within the class.
protected:class members can access inside the same package.
Is This Answer Correct ? | 29 Yes | 13 No |
Answer / prashant kalmodiya
PUBLIC: Specifies that those all members are accessible
from any function.
PRIVATE: Specifies that those members are accessible only
from member functions and friends functions of the class.
PROTECTED: Specifies access to class members in the member-
list up to the next access specifier (public or private)
Is This Answer Correct ? | 18 Yes | 8 No |
Answer / it
Public is used for global access so its scope is not only
within the class but also outside the class. For example
public variables will be visible to all classes and any
class or member function can utilize those variables and
member functions.
Private is used only in the class in which it is defined.
It cannot be accessible by its derived members. So private
variables will be visible only to the class to which they
belong and it will not give access to any other classes.
Public and protected members will become private in derived
class.
Protected is used within the class in which it is defined
and derived members of it can also access it. So protected
variables will be visible only to the class to which they
belong and it gave access to its derived classes also.
Is This Answer Correct ? | 17 Yes | 7 No |
Answer / prashanth c patel
public-->inherite the protected members as preotected in
drived class and pubic members wiull be public in derived
class
protected--->pubic and protecated members of the base class
will become protected in derived class
Private-->pubilc and proteacted members will become private
in derived class
Is This Answer Correct ? | 21 Yes | 12 No |
Answer / abhay
public-->inherite the protected members as preotected in
drived class and pubic members wiull be public in derived
class
protected--->pubic and protecated members of the base class
will become protected in derived class
Private-->pubilc and proteacted members will become private
in derived class
Is This Answer Correct ? | 14 Yes | 7 No |
What are abstract data types in c++?
What is the type of this pointer in c++?
Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers
Explain deep copy and a shallow copy?
What are the total number of lines written by you in C/C++? What is the most complicated or valuable program written in C/C++?
write a program that takes two numbers from user that prints the smallest number
1.what is the difference between software & package &application.
What is functions syntax in c++?
What is the best way to take screenshots of a window with c++ in windows?
How would you differentiate between a pre and post increment operators while overloading?
What are 2 ways of exporting a function from a dll?
Differentiate between an inspector and a mutator ?