What is the difference between public, private, and protected access in C++?

by Rohit| Views: 4200

difference between public, private, and protected access



Tags:  Oops Interview

Answers (1)
 
Gourav Said..

Public, Private and Protected keywords are used to specify access levels for member variables, or for member functions (methods). Difference between public, private, and protected access in oops is given below:

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.



Register or Login to Post Your Opinion/Answer