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 |
How do I start a c++ project?
class HasStatic { static int I; }; Referring to the sample code above, what is the appropriate method of defining the member variable "I", and assigning it the value 10, outside of the class declaration? a) HasStatic I = 10; b) int static I = 10; c) static I(10); d) static I = 10; e) int HasStatic::I = 10;
What parameter does the constructor to an ofstream object take?
How the endl and setw manipulator works?
What is the full form of dos?
total amount of milk produced each morning and then calculates and outputs the number of cartons needed for this milk , the cost of producing the milk and the profit from producing this milk.
How to declaring variables in c++?
Write a function that swaps the values of two integers, using int* as the argument type?
Explain function overloading and operator overloading.
Describe about storage allocation and scope of global, extern, static, local and register variables?
What do you mean by late binding?
What is the use of setfill in c++?