Access-Modifier-in-C++ .

In this session i have discussed access modifier in C++ .


Access modifiers play a very important role in object oriented programming . It define which member is accessible outside of the class or in subclass .


There are three types of access modifier as follows

1-Private

2-Public

1-Protected


1-Private

If you have not specify any access modifier then by default all the members are private . The private member allow to use inside the class not outside the class that means you can not access the private members with object of the class . It is also not available in derived class .

Private Access Modifier Demo :

Example : Write a program that demonstrate the private access modifier in C++

    #include<iostream>
    using namespace std;  
    class A
    {
        private :
        int x;
        void show()
        {
            cout<<"This is private show()";
        }

        public :
         void sum(int x ,int y)
         {
             cout<<"\nSum : "<<x+y;
         }

    };

    int main()
    {
            A obj= A();
            obj.show(); // error due to private function
            obj.x; //error due to private member
            obj.sum(10,20);
            return 0;
    }

    OUTPUT : A::show() is private function
             A::x is private member 
    

2-Public

The public access modifier is every where available . That means you can access the out side of the class like call the member or function with object . It is also available in derived class .

Public Access Modifier Demo :

Example : Write a program that demonstrate the public access modifier in C++

    #include<iostream>
    using namespace std;  
    class A
    {

        public :
         void sum(int x ,int y)
         {
             cout<<"\nSum : "<<x+y;
         }

    };
    class B : public A
    {
       public :
       void sub(int x,int y)
       {
           cout<<"\nSub : "<<x-y;
       }

    };

    int main()
    {
          A obj1;
          obj1.sum(20,10); //public sum() called with A class object obj1
          B obj2; // B class object created
          obj2.sum(30,20); // public sum() called with B class object obj2
          obj2.sub(20,10); // public sub() called with B class object obj2
          return 0;
    }


    OUTPUT : Sum : 30
             Sum : 50
             Sub : 10 
    

3-Protected

The protected access modifier also available with in class and derived class but we can not call with help of object of the class .

Protected Access Modifier Demo :

Example : Write a program that demonstrate the protected access modifier in C++

    #include<iostream>
    using namespace std;  
    class A
    {

        protected :
         void show()
         {
             cout<<"\nThis is protected show() : ";
         }

    };
    class B : public A
    {
       public :
       void display()
       {
           show();
           cout<<"The protected show() is called inside the display()";
       }

    };

    int main()
    {
            A obj1;
            obj1.show(); //error due to protected access modifier
            B obj2;
            obj2.display();
            return 0;
    }
    OUTPUT : error void A::show() is protected  
    

If you want to run the program comment the obj1.show() and execute the program .

Welcome to AndroidATPoint.

back image                     next image