NOT SO COMMON C++ : Access Specifiers




Hello everyone , wecome to our NOT So Common C++ series.
In the last post for NOT SO COMMON C++ we discussed about some shortcuts , initializations of pointers, structures ..etc.

So today lets cover some tricky portions of C++ that are a bit confusing for the beginners.

I hope you people have basic knowledge of class in C++. 

Access Specifiers : public , private , protected.

Lets see an example to get an overview of access specifiers-

#include<iostream>
using namespace std;

class Example
{
private:

    int priV ;                              //PRIVATE VARIABLE or PRIVATE MEMBER

public:
     Example()                           //Constructor
     {
            priV =  10;                  // initializing "priV"
            proV =  4;                   // initializing "proV"
    void pubFunction()            // PUBLIC FUNCTION
    {
        cout << "public Function executed and priV = " << priV;   // PUBLIC FUNCTION accessing priV
           
           cout << "\npublic Function executed and proV =  " << proV;   // PUBLIC FUNCTION accessing proV
    }

protected:

    int proV = 4;    // protected member

};


int main()
{
    Example obj;                    // obj is the object of class Example.

    obj.pubFunction();          //using obj we can access "pubFunction" becausez it is a public member.

    obj.priV;                         // will give error : private member cannot be accessed directly by objects
    obj.proV;                       //will give error : protected member cannot be accessed directly by objects
}

OUTPUT :

public Function executed and priV = 10
public Function executed and proV =  4

You can try the program at : C++ shell (click here)


For the above example you can see we have private, public and protected access specifiers in a class. Keep the code in mind we will b discussing it alot to understand the topic.

Who can access private member ?

Any Constructor and Public Function of the same class can access private members.
Like in the above example the private variable "priV" was accessed by the public function "pubFunction" and was also accessed by Constructor "Example()" . 

Both public member as well as constructor can read the private data.

Inside both public function as well as in constructor we can assign(" priV=30 ") the values to private data.

But there are some exceptions in C++ due to which we can access the private member also outside the class using FRIEND FUNCTION and FRIEND CLASS.
 Now the question is who the hell is this "Friend" ?

 To get a full idea about  " Friend " go to this link : My "Friend in C++"

I hope you went through the Friend content and now you have a..... "Friend".

   Who cannot access private member:
  • You cannot directly access a private member using an object of the class.
  • You cannot access the private member of base class from the derived class( Hierarchy concept : we will see this in the next post).

Who can access public member ?


  • Object of the class
  • Derived class
There is not much complexity with the use of public members : we can simply say public member can be be accessed anytime anywhere .


Who can access protected member ?

  • can be accessed by public member(Public function).
  • can be accessed by constructor
  • can be accessed by the derived class( Hierarchy concept : we will see this in the next post)
Both public member as well as constructor can read the protected data.

Inside both public function as well as in constructor we can assign("proV = 10") the values to protected data.


    Who cannot access the protected member ?

  • we cannot access protected member directly using an object of the class.
 

I hope this cleared your doubts about the access specifiers , to get a bit detailed use of access specifier see my next post on Hierarchy concept.
    


Comments

Popular Posts

Why to choose C# ? - for Beginners

Li-Fi - Experience The Lightning Speed