Access Specifiers In C++ – A Quick glimpse to private, public, and protected

In this article, we will learn about Access Specifiers in C++.
The access Specifiers in C++ are public, private, and protected.

Access specifiers in C++ basically used in OOPs Concept. In classes, we start their use, they are mainly used in inheritance. They set the range for the usage of the variable and the functions of a particular class. Access specifiers are used for data hiding purposes also.

Wish to enhance your foundation skills, then why not check out our Foundation Courses designed by experienced mentors at PrepBytes.

What is Access Specifier?

  1. Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type. The access restriction to the class members is specified by the labeled public, private, and protected sections within the class body. The keywords public, private, and protected are called access specifiers.
  2. A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing right brace of the class body is seen. The default access for members and classes is private.
    class Base {
    public:
    // public members go here
    protected:
    // protected members go here
    private:
    // private members go here
    };

How Does Access Specifiers Work in C++?

The accessibility of access specifiers in classes during inheritance is shown in Table.

Types of Access Specifier in C++:

  1. The public Members
    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 as shown in the following example −

    #include <iostream>
    using namespace std;
    class Line {
    public:
        double length;
        void setLength( double len );
        double getLength( void );
    };
    // Member functions definitions
    double Line::getLength(void) {
        return length ;
    }
    void Line::setLength( double len) {
        length = len;
    }
    // Main function for the program
    int main() {
        Line line;
        // set line length
        line.setLength(6.0);
        cout << "Length of line : " << line.getLength() <<endl;
        // set line length without member function
        line.length = 10.0; // OK: because length is public
        cout << "Length of line : " << line.length <<endl;
        return 0;
    } 
    

    When the above code is compiled and executed, it produces the following result −
    Length of line : 6
    Length of line : 10

  2. The private Members
    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.
    By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member −
    class Box {
    double width;
    public:
    double length;
    void setWidth( double wid );
    double getWidth( void );
    };

    Practically, we define data in the private section and related functions in the public section so that they can be called from outside of the class as shown in the following program.

    #include <iostream>
    using namespace std;
    class Box {
    public:
        double length;
        void setWidth( double wid );
        double getWidth( void );
    private:
        double width;
    };
    // Member functions definitions
    double Box::getWidth(void) {
        return width ;
    }
    void Box::setWidth( double wid ) {
        width = wid;
    }
    // Main function for the program
    int main() {
        Box box;
        // set box length without member function
        box.length = 10.0; // OK: because length is public
        cout << "Length of box : " << box.length <<endl;
        // set box width without member function
        // box.width = 10.0; // Error: because width is private
        box.setWidth(10.0);  // Use member function to set it.
        cout << "Width of box : " << box.getWidth() <<endl;
        return 0;
    }
    

    When the above code is compiled and executed, it produces the following result −

    Length of box : 10
    Width of box : 10

  3. The protected Members
    A protected member variable or function is very similar to a private member but it provides one additional benefit that they can be accessed in child classes which are called derived classes.
    You will learn derived classes and inheritance in the next chapter. For now you can check the following example where I have derived one child class SmallBox from a parent class Box.
    Following example is similar to the above example and here width member will be accessible by any member function of its derived class SmallBox.

    #include <iostream>
    using namespace std;
     
    class Box {
       protected:
          double width;
    };
     
    class SmallBox:Box { // SmallBox is the derived class.
       public:
          void setSmallWidth( double wid );
          double getSmallWidth( void );
    };
     
    // Member functions of child class
    double SmallBox::getSmallWidth(void) {
       return width ;
    }
     
    void SmallBox::setSmallWidth( double wid ) {
       width = wid;
    }
     
    // Main function for the program
    int main() {
       SmallBox box;
     
       // set box width using member function
       box.setSmallWidth(5.0);
       cout << "Width of box : "<< box.getSmallWidth() << endl;
     
       return 0;
    }
    

    When the above code is compiled and executed, it produces the following result −

    Width of box : 5

Frequently asked questions in an Interview on Access Specifiers in C++

  1. Difference between struct and class in terms of Access Modifier?
    All members of a class are private by default, whereas fields of a struct are public. Default access of a base class is private for classes and public for structures.
    For Example :
    struct Emp
    {
    char Name[20]; // Here Name variable is public
    };

  2. What is the default access level?
    Access modifiers define the accessibility of the specific type, for example: class, structure, data member etc.
    The access privileges in C++ are:
    1.Private
    2.Public
    3.Protected
    The default access level assigned to members of a class is private.
    Private members of a class are accessible only within the class and by friends of the class.
    Protected members are accessible by the class itself and its sub- classes.
    Public members of a class can be accessed by anyone.

  3. What is private, public and protected Inheritance?
    a. Private Inheritance :
    The Public and protected members of Base class become private members of the derived class.

    b. Public Inheritance :
    All the public members and protected members are inherited as public and protected respectively.

    c. Protected Inheritance :
    Public and Protected members are derived as protected members.

  4. What is the importance of a mutable keyword?

    • The mutable keyword allows the data member of a class to change within a const member function.
    • It allows us to assign the values to a data member belonging to a class defined as “Const” or constant.
    • It allows a const pointer to change members.
    • It can be only applied to non-static and non-const data members of a class.

    Syntax:
    mutable data_member_variable_declaration;

  5. Which access specifier is usually used for data members of a class?
    All the data members should be made private to ensure the highest security of data. In special cases we can use public or protected access, but it is advised to keep the data members private always.

This article tried to discuss Access Specifiers in C++. Hope this blog helps you understand the concept. To Practice problems you can check out MYCODE | Competitive Programming at PrepBytes.

Leave a Reply

Your email address will not be published. Required fields are marked *