What is public, protected, private?

Public, protected and private are three access specifiers in C++.

1. Public data members and member functions are accessible outside the class.

2. Protected data members and member functions are only available to derived classes.

3. Private data members and member functions can’t be accessed outside the class.

However there is an exception can be using friend classes.

Showing Answers 1 - 28 of 28 Answers

v.chandrasekhar

  • Jan 17th, 2006
 

Private,public and protected are access specifiers..They are usually specified after the declaration after of data members.

reena

  • Mar 29th, 2007
 

public,private and procted are access specifier.private use best for security purpose.public is use whole application.

  Was this answer useful?  Yes

natasha26

  • Jun 4th, 2007
 

They are access modifiers and are typically used when defining a class' method(s) and variable(s). I don't think it has anything to do with security. It's just a programatic way to allow/disallow access when you are programming (in the raw code state). But if you're shipping a DLL accross, private class members won't be visible.

In order of increasing restriction:
public = accessible to class's methods and subclasses' methods
protected = ? something to do with accessibility over different namespaces
private = access only within scope of class

(TBC)

jegathesan

  • Jun 8th, 2007
 

public, protected, private are access specifiers that is used to implement encapsulation of data at various level.
public - A member type public can be accessed by object
protected - A member type privat can not be accessed by object. An extended class can access protected data
private - Only the other members insid the class can access

  Was this answer useful?  Yes

Som Gollakota

  • Jun 13th, 2007
 

Private:

  • Can be data or method members 
  • Are private to the class where they are declared
  • Accessible ONLY by the member methods of the class where they are declared
  • Only exception to the above rule is Friend (explanation of friends is beyond the scope of this topic
  • In a C++ class, private is default for member declaration. That is, if you do not specify any access specifier (private, public, protected), the member is considered private

Public:

  • Can be data or method members
  • Are accessible by any function/method/application globally, so long as an instance of the class where the public members are declared is created.
  • These members are accessible only thru an instance of the class where they are declared
  • Generally used to define a C++ class behaviour and/or to access private data members (act as private data modifiers)

Protected

  • Can be data or method members
  • Act exactly as private members for all practical purposes, so long as they are referenced from within the class (and/or instances of the class) where they are declared
  • Specifically used to define how certain data/method members of a class would behave in a child class (used to define their behaviour in inheritance)
  • The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.

Private:

  • Can be data or method members
  • Are accessible ONLY thru other methods of the same class where they are declared
  • Cannot be accessed thru a class instance
  • Will not be inherited

Public:

  • Can be data or method members
  • Are accessible globally from any function/method outside the class they are declared, across the application
  • Are accessible ONLY thru a valid instance of the class in which they are declared

Protected:

  • Can be data and method members 
  • Exactly same as private members for all practical purposes, except for the inheritance part
  • These members are inherited by a child of the class in which they are declared
  • Their behaviour in the child class depends on how the inheritance has been defined
  • If the inheritance is private, these are private in the child class, if it is public, these are public in the child class, and if the inheritance in protected, these are protected in the child class thus allowing further inheritance.

saroy09

  • Mar 26th, 2009
 

In case of private inheritance, base class public and protected members become private in child class.
In case of protected inheritance, base class public and protected members become protected in child class.
In case of public inheritance, base class protected members become protected in child class and public members become public in child class.

These are three important access specifiers.

1. Private - Members are accessible only to member functions and friend function.


2. Protected - Members are accessible to member functions of the class and classes which are derived from this class.


3. Public - Accessible by all members of the class n outside too.

  Was this answer useful?  Yes

Private Protected and Public all these are access specifiers.
Private Data Members are only available inside class
Protected Data Members are  only available inside class as well as in derived classes.
Public Data members are available everywhere

  Was this answer useful?  Yes

ankit

  • Jun 26th, 2015
 

Can we use protected specifier first than public than private in a class definition in c++ by making class

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions