What is the access Specifier in cpp

In cpp We can customize the accessing of data member or member function.
What  does it mean, Let's consider you created a class named "acess".put the function and data which you want. 

Class access
{ 
    int data;
   
       int Function()

      {

        //definition of function;

      }

};
If data,function can be accessed by any where of program then Object Oriented Programming loss its data hiding concept.

So,CPP gives you permission to hide,show your data to the other part of program .
                for this purpose Cpp gives three Access Specifier
Public:

Private:


protected :
Public : if we want to share the data member every where of program we use this keyword .
    

                                    By default cpp apply this keyword for member ,like as above we haven't declare any access specifier ,so cpp by default apply it as public.
Thus it will be same as :

 class access 
  {
      public://By default 

      int data;

      int function()

         {
            
        //Definition of function ;
         
        }
  
}

Consider this program to understand ,how accessing is performed .


class dog
  {

     public:  // Public access specifier 

        int leg;

         void bark()
          {
             cout<<"Barking dog seldom bites ";
          }

   };
     
int main()

 {
        dog tomy ;

         cout<<"How many legs tom has ?";

                cin>>tomy.leg; 


// Above Accessing the "leg "data of that class from 

// out side of class i.e. in main function.

     if(tomy.leg<4) 
 
         cout<<"Tomy need extra care.";

     cout<<"\n See tomy bark or not .";

       tomy.bark(); // Accessing member function.

  }
   So, what we knew that if we have to access member of a class outside or by outsider function we must declare the member as Public.

Private:
 private is another access specifier of cpp ,as the name suggested ,if we have to make the member of a class private ,we use this access specifier .

   But the question is what the mean by making member private .
So,making the member private means


1.Private member can't be accessed by outside or another outsider function.
2.Private member is only accessible in the class in which it exist.
3.Private member only can be accessed by the function of that class.
  


 let's understand all the term by example .
class access 
  {
      private:  

      int data;

      int function()

         {
            
        //Definition of function ;
         
        }
  
}

This is how we declare member as private .

An example for accessing the private member :
class dog
  {

     private:

        int leg;

     public:

      void bark();   

   };
void dog::bark()

          {   
               
            cout<<"How many legs tom has ?\n";

              cin>>leg; //can accessed private member

// because function bark() is of same class.
 
            if(leg<4)
 
                cout<<"Tomy need extra care.\n";
            else

                cout<<"\n See tomy bark or not \n.";

          }
     
int main()

 {
        dog tomy ;

         cout<<"How many legs tom has ?\n";

              //  cin>>tomy.leg; Error we can't access

//private member by outsider function(main)  

    tomy.bark(); // Accessing member function since it

's public.

  }
output:
How many legs tom has ?
4
Tomy need extra care.
As we go through the program,we see that tomy.leg can't be accessed because it's being accessed by a function(main)which is not in the same class.
                                               But we can access the tomy.bark().i.e. bark() function of class dog.again from the bark() function we can accessed all member.because as i say earlier we can access the private member by the function of same class. 
   
Share on Google Plus

0 comments: