WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Basic · question 14 of 100

What is the difference between ’public’, ’private’, and ’protected’ access specifiers in C++?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

In C++, access specifiers are used to control the access to the members (variables and functions) of a class. There are three access specifiers in C++: ’public’, ’private’, and ’protected’. The main difference between them is in their visibility and accessibility from outside the class.

Public access specifier: The ’public’ access specifier makes the member variables and member functions of a class accessible from outside the class. This means that they can be accessed by objects of the class and by functions outside the class. For example:

    class MyClass {
        public:
        int publicVariable;
        void publicFunction() {
            // function code
        }
    };
    
    int main() {
        MyClass obj;
        obj.publicVariable = 5;
        obj.publicFunction();
        return 0;
    }

In this example, we have a class called MyClass with a public member variable called publicVariable and a public member function called publicFunction(). These members can be accessed from outside the class by creating an object of the class and using the ’.’ operator.

Private access specifier: The ’private’ access specifier makes the member variables and member functions of a class accessible only from within the class. This means that they cannot be accessed by objects of the class or by functions outside the class. For example:

    class MyClass {
        private:
        int privateVariable;
        void privateFunction() {
            // function code
        }
        public:
        void setPrivateVariable(int value) {
            privateVariable = value;
        }
    };
    
    int main() {
        MyClass obj;
        obj.setPrivateVariable(5);
        return 0;
    }

In this example, we have a class called MyClass with a private member variable called privateVariable and a private member function called privateFunction(). These members can only be accessed from within the class. However, we can provide public member functions, such as setPrivateVariable(), to set the value of the private member variable.

Protected access specifier: The ’protected’ access specifier is similar to the ’private’ access specifier, but it allows access to the members of a class by its derived classes. This means that the protected members can be accessed from within the class and by its derived classes, but not by objects of the class or by functions outside the class hierarchy. For example:

    class MyBaseClass {
        protected:
        int protectedVariable;
    };
    
    class MyDerivedClass : public MyBaseClass {
        public:
        void setProtectedVariable(int value) {
            protectedVariable = value;
        }
    };
    
    int main() {
        MyDerivedClass obj;
        obj.setProtectedVariable(5);
        return 0;
    }

In this example, we have a base class called MyBaseClass with a protected member variable called protectedVariable. We also have a derived class called MyDerivedClass that inherits from MyBaseClass and provides a public member function, setProtectedVariable(), to set the value of the protected member variable. The protected member variable can be accessed from within the base class and by its derived classes, but not by objects of either class or by functions outside the class hierarchy.

In summary, access specifiers in C++ are used to control the access to the members of a class. The ’public’ access specifier makes the members accessible from outside the class, the ’private’ access specifier makes the members accessible only from within the class, and the ’protected’ access specifier allows access to the members by its derived classes.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics