Monday, 5 March 2018

Access Modifiers in C++

Access modifiers are called as Access Specifiers .It defines how the members of the class can be accessed.
There are 3 types of access modifiers available in C++:
1.     Public
2.     Private
3.     Protected
Note: By default, all members and function of a class is private i.e. if no access specifier is specified.
Declaring Access Specifiers in C++

  class class_name

{

private:

//private data members and member functions

public:

//public data members and member functions

  protected:

//protected data members and member functions

};

Public Access Specifier

public class members are accessible inside and outside the class .In other words it can access from anywhere in the program.

  Syntax

 class class_name

{

public:

//public data members and member functions

 };

        Example:
        class A
            {
      public:  //access specifier
      int a; // data member
     void display(); //member function
              };
     Example 1:
     #include<iostream.h>
     #include<conio.h>
         class  A
              {
           public:
           int b;

          public:
       void show()
             {
         b=40;
          clrscr();
           cout<<"\nAccessing variable within the class"<<endl;
          cout<<"Value of b: "<<b<<endl; //Output is: Value of b:40
               }
            };

         void main()
             {
           A a1; // create object
             a1.show();
            cout<<"Accessing variable outside the class"<<endl;
            cout<<"value of b: "<<a1.b<<endl;  // Output is: Value of b:40
            getch();
              }   
      
       Private Access Specifier
Private class members are accessible with the class and it is not accessible from outside the class. If you access private data members from outside the class it gives compile time error. By default class variables and member functions are private.

Syntax

class class_name

{

 private:

//private data members and member functions

 };

        Example:
        class A
          {
        private:  //access specifier
        int a; // data member
        void display(); //member function
         };

       Example 1:
       #include<iostream.h>
      #include<conio.h>
       class  A
       {
      private:
      int a;
 public:
     void show()
      {
       a=40;
      clrscr();
      cout<<"\n Accessing variable within the class"<<endl;
    cout<<"Value of a: "<<a<<endl; // It can be accessible because it can access  inside      the class and the output is: Value of a:40
         }
        };

    void main()
      {
       A a1; // create object
      a1.show();
  cout<<"Accessing variable outside the class"<<endl;
     cout<<"value of a: "<<a1.a<<endl;  // It cannot be accessible because a is private           and it  cannot access from outside the class, so it will give compile time error.
       getch();
         }
   
     Protected Access Specifier
It is similar to private access specifier. It cannot access class member from outside the class. But it can be accessed by any subclass of that class.

Syntax

class class_name

{

protected:

//protected data members and member functions

};

     Example:
     class A
      {
     protected:  //access specifier
      int a; // data member
     void display(); //member function
      };
     
    Example 1:
     class A
      {

    protected:
    int c;
    public:
   void show()
    {
    c=300;
    cout<<"\nAccessing variable within the class"<<endl;
    cout<<"Value of c: "<<c<<endl; // Output is: Value of c:300
       }
       };
      class B:public A
             {
           public:
         void display()
          {
    c=60;
         cout<<"\nAccessing variable in sub the class"<<endl;
         cout<<"Value of c: "<<c<<endl; // Output is: Value of c:60
            }
           };

            void main()
              {
            clrscr();
              A a1; // create object
             a1.show();
            a1.display();

        cout<<"\nAccessing variable outside the class"<<endl;
   cout<<"value of c: "<<a1.c<<endl; // Provides compile time error because c is           protected so it cannot be access from outside the class.

                     getch();
                           }


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...