GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 3 of 203    Print  
What is public, protected, private?
Ø      Public, protected and private are three access specifiers in C++. 
Ø      Public data members and member functions are accessible outside the class.
Ø      Protected data members and member functions are only available to derived classes.
Ø      Private data members and member functions can’t be accessed outside the class.         
    However there is an exception can be using friend classes.
 


  
Total Answers and Comments: 9 Last Update: November 14, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: SomGollakota
 

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.


Above answer was rated as good by the following members:
ozkan, the_ankit1987
January 17, 2006 04:31:46   #1  
v.chandrasekhar        

RE: What is public, protected, private?
Private public and protected are access specifiers..They are usually specified after the declaration after of data members.
 
Is this answer useful? Yes | No
March 29, 2007 06:47:33   #2  
reena        

RE: What is public, protected, private?
public private and procted are access specifier.private use best for security purpose.public is use whole application.
 
Is this answer useful? Yes | No
June 04, 2007 18:38:32   #3  
natasha26 Member Since: January 2007   Contribution: 1    

RE: What is public, protected, private?
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)

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 08, 2007 16:03:17   #4  
jegathesan Member Since: February 2007   Contribution: 1    

RE: What is public, protected, private?
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

 
Is this answer useful? Yes | No
June 13, 2007 01:41:05   #5  
Som Gollakota        

RE: What is public, protected, private?

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.


 
Is this answer useful? Yes | No
June 13, 2007 01:59:14   #6  
SomGollakota Member Since: June 2007   Contribution: 48    

RE: What is public, protected, private?

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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 26, 2009 08:49:36   #7  
saroy09 Member Since: March 2009   Contribution: 1    

RE: What is public, protected, private?
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.

 
Is this answer useful? Yes | No
June 12, 2009 08:17:54   #8  
gunjan.chandra Member Since: April 2009   Contribution: 7    

RE: What is public, protected, private?
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.


 
Is this answer useful? Yes | No
November 14, 2009 06:13:25   #9  
KomalUpadhyay Member Since: November 2009   Contribution: 2    

RE: What is public, protected, private?

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


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape