classes in c++

Can anyone explain the private, public and protected access specifiers in c++?
Please make sure your answers are easy enough for a beginner to understand.

Public

Public, means all the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. We can access them inside the class, outside the class and in child class also.

Private

Private keyword, means that no one can access the class members declared private outside that class. If someone tries to access the private member, they will get a compile time error. By default class variables and member functions are private. Method or property with private visibility can only be accessible inside the class.

Protected

Protected, is the last access specifier, and it is similar to private, it makes class member inaccessible outside the class. But they can be accessed by any subclass of that class. (If class A is inherited by class B, then class B is subclass of class A.) Protected is generally used in the process of inheritance.

Hope this helps :slight_smile:

Classes, structs and union provides 3 access specifiers namely public, private and protected in C++. They define how the members of the class can be accessed. Data hiding is one of the important features of Object Oriented programming which specifies how anything outside our class (any function in our program) will access the class members i.e. who has access to what. This access restriction to the class members are specified by these keywords namely, private, public and protected.

Take an example:

class sampleClass {
    public:
       int a;
       void setVal(int x);
    private:
       int b;
    protected:
       int c;
};

int main() {
   sampleClass sc1;
   sc1.a = 10;  // this is valid.
}

As per above class, we can see that variables a is declared public, b is private and c is protected. Therefore in main(), sc1.a is valid because a is public and hence can be accessed outside the class. Thus to quote,

A public member is accessible from
anywhere outside the class but within
a program. You can set and get the
value of public variables without any
member function

Doing sc1.b = 10; is not allowed because b is private.

A private member variable or function
cannot be accessed, or even viewed
from outside the class. Only the class
and friend functions can access
private members.

Thus, if we create a member function of a class, lets say setVal(int x) , we can set the value of any private variable by this:

void sampleClass:: setVal( int x ) {
    b = x; // accessing private variable
}

Doing sc1.c = 10 is also invalid as c is a protected member.

A protected member variable or
function is very similar to a private
member but it provided one additional
benefit that they can be accessed in
child classes which are called derived
classes.

Thus, for accessing variable c, create a derived class, its object and then access that variable like shown below:

class sampleClass {
    public:
       int a;
       void setVal(int x);
    private:
       int b;
    protected:
       int c;
};

class derivaedClass :: sampleClass {
     public:
        void setValDerived( int x) {
            c = x;  // accessing protected member of sampleClass here
        }
};

For better understanding take an example of a facebook post, You can chose who will see your post or not. Public means everyone has access to your post ( public access specifier), “only for me” means private access specifier and then you have another option saying “Friends or friends of friends”, this corresponds to protected access specifier.

Hope this clears.

1 Like

I think this video will help you out.