What are the differences between public, private, and
protected access?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When do we run a shell in the unix system?

562


Explain public, protected, private in c++?

565


What do manipulators do?

570


What is a .lib file in c++?

568


What does the linker do?

591






What is constant in c++ with example?

607


How is computer programming useful in real life?

596


What is the difference between a pointer and a link in c ++?

564


What is #include cmath?

657


When do we use copy constructors?

593


What is a class definition?

613


How do you initialize a string in c++?

558


What are the basic data types used in c++?

553


What is the output of the following program? Why?

622


What is set in c++?

658