que is:- what is access modifiers?

This is not a term used in C++. C++ tends to use access specifier instead. However I will assume this is what you mean, as access modifier is used by other languages such as Java and C#.

They are supported by the keywords public, private and protected and possibly friend.

The first three are used in C++ in two places, both relating to class definitions.

Most obviously they are used to specify the access others have to the class members that follow (until another access specifier is reached or the class definition ends).

Public allows anyone access to those members, private allows only other members of the class access to such members. Protected allows access only to other members of the class and to members of classes derived from the class. For example:

   class Base
   // Classes start with private access for members by default
   // Structs have public access by default

       Base(); // default constructor
       Base(Base const & other); // copy constructor
       Base & operator=(Base const & rhs ); Assignment operator
       void DoSomethingInteresting();

       void CalledBySubclasses():

   private:  // only accessible to Base members
       int    iState;

   Base::Base() : iState( 0 ) {}
   Base::Base(Base const & other) : iState(other.iState) {}
   Base & Base::operator=( Base const & rhs )
       iState = rhs.iState;
        return *this;
   void Base::DoSomethingInteresting()

   void Base::CalledBySubclasses()

In the example class Base only members of Base are allowed to access the private parts such as iState. Anyone can access the public parts such as the constructors, the assignment operator and the member function DoSomethingInteresting. Only members of Base and sub-classes derived from base can access the protected parts, which in the example only applies to CalledBySubclasses.

In general the implementation specific parts of a class - such as its internal representation of data etc. should be private. Only those parts that form the required interface to the rest of the application (or applications if part of a library) should be public. Protected access is tricky. In a simple world it would not be needed. However there are cases where sub-classes, as related types,  would require partial access to the implementation of their base class(es), and making such details public would be opening the class up far too wide. In such cases protected is useful.

The other use of access specification is when deriving one class from another. In this case all members of the base class derived from can have no greater access within the derived class than that specified in the derivation clause. (note: public gives greatest access, then protected, private gives least access). Again, the default derivation access is private for class definitions and public for struct definitions.

OK that last paragraph has probably got you scratching your head. Let's try some examples:

   class HasPrivateBase : Base
   // ...

HasPrivateBase is derived from the Base class from the previous example using the default access for classes - i.e. privately. This means that within an instance of HasPrivateBase all Base members are private. Thus:

    Base b;
    b.DoSomethingInteresting();; // OK, public in Base

   HasPrivateBase h;
   h.DoSomethingInteresting(); // **BAD**, private in HasPrivateBase

Note that HasPrivateBase itself can access public and protected parts of Base, it is as if HasPrivateBase has a special Base member that is private.

   class HasProtectedBase : protected Base
   // ...

HasProtectedBase is derived from the Base class using an access of protected. This means that within an instance of HasProtectedBase all public and protected members of Base are protected and all private members of Base members are private.

Finally public base class access:

   class HasPublicBase : public Base
   // ...

This is by far and away the most common case. HasPublicBase is derived from the Base class using an access of public. This means that within an instance of HasPublicBase all public members of Base are public, protected members of Base are protected and all private members of Base members are private.

Now a note on the friend specifier. The friend keyword can be used to specify functions and classes that are friends of a given class. Friends have unrestricted access to all parts of a class - whether they are public, protected or private. Hence friendship should be used sparingly. It is most often useful in very closely coupled types where making a member public just for this other related by design but not by implementation class would allow others access to details of the class that are not for general use. The other common use is when writing non-member operator overload functions. In such cases the operator function may require access to the internals of the objects it is working with.

Here is a simple example showing the syntax for making the function friend_fn and the class Y friends of class X

   class X

   // ...

       friend void friend_fn(X*, int);
       friend class Y;

   // ...


Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.