Header Ads Widget

Types of access specifier in C++ || Private, Protected, Public

 



Types of access specifier in C++

C++ provides different access specifier to control the data member according to program. Specially, it used to protect the data member from misuses.

In the class only data member can use the different access specifier. There are three access specifier:-  


 

Public: Members declared as public are accessible from anywhere outside the class. This means that they can be accessed by objects of the class, as well as by any functions or classes that are not part of the class.

            For example:-
                                    #include <iostream>
                                    using namespace std;
                                    class A
                                    {
                                                public:
                                                int a=12;
                                    };                                

                                    int main()
                                    {
                                                A obj;
                                                cout<<obj.a;
                                                return 0;
                                    }          
                        output:-
                                                a=12

 

Protected: Members declared as protected are accessible from within the class and its derived classes. This means that they cannot be accessed from outside the class, but can be accessed by any derived classes that inherit from the class.

For example:-
 #include<iostream>
using namespace std;


class A
{
protected:
int a;
};


class B:public A
{
public:
void square(int x)
{
            a=x;
}
void display()
{
cout<<"square of a="<<a*a;
}
};

int main()
{
B ob;
ob.square(5);
ob.display();
return 0;
}

output:-
                        square of a=25


                                   

 

Private: Members declared as private are accessible only from within the class. This means that they cannot be accessed from outside the class, not even by derived classes.

            For example:-
                                    #include <iostream>
                                    using namespace std;
                                    class A
                                    {
                                                private:
                                                int a=12;
                                                void show()
                                                {
                                                            cout<<”a=”<<a;
                                    };                                

                                    int main()
                                    {
                                                A obj;
                                                obj.show();
                                                return 0;
                                    }

            output:-

 a=12

 

                                   

Post a Comment

0 Comments