C++ C++ Tutorial C++ Functions C++ Classes



CPP Access Specifiers

C++ is an object-oriented programming language that allows developers to create classes and objects. Access specifiers are an essential part of C++ classes that determine the accessibility of class members. Access specifiers are used to control the access to the class members, such as variables and functions, from outside the class. There are three types of access specifiers in C++: public, private, and protected.

Public Access Specifier

The public access specifier allows the class members to be accessed from anywhere in the program. The public members of a class can be accessed by any function or object in the program. The public members are declared using the keyword "public" followed by a colon. Here is an example:


class MyClass {
public:
    int publicVar;
    void publicFunc() {
        // code here
    }
};

In the above example, the variable "publicVar" and the function "publicFunc" are declared as public members of the class "MyClass". These members can be accessed from anywhere in the program.

Private Access Specifier

The private access specifier restricts the access to the class members to only the class itself. The private members of a class cannot be accessed by any function or object outside the class. The private members are declared using the keyword "private" followed by a colon. Here is an example:


class MyClass {
private:
    int privateVar;
    void privateFunc() {
        // code here
    }
public:
    void setPrivateVar(int var) {
        privateVar = var;
    }
};

In the above example, the variable "privateVar" and the function "privateFunc" are declared as private members of the class "MyClass". These members can only be accessed by the class itself. The function "setPrivateVar" is declared as a public member of the class, which allows the private variable "privateVar" to be set from outside the class.

Protected Access Specifier

The protected access specifier is similar to the private access specifier, but it also allows the members to be accessed by the derived classes. The protected members of a class can be accessed by the class itself and any derived classes. The protected members are declared using the keyword "protected" followed by a colon. Here is an example:


class MyBaseClass {
protected:
    int protectedVar;
    void protectedFunc() {
        // code here
    }
};

class MyDerivedClass : public MyBaseClass {
public:
    void setProtectedVar(int var) {
        protectedVar = var;
    }
};

In the above example, the variable "protectedVar" and the function "protectedFunc" are declared as protected members of the class "MyBaseClass". These members can be accessed by the class itself and any derived classes. The class "MyDerivedClass" is derived from the class "MyBaseClass" and can access the protected variable "protectedVar" using the function "setProtectedVar".

Access specifiers are an essential part of C++ classes that allow developers to control the accessibility of class members. Public members can be accessed from anywhere in the program, private members can only be accessed by the class itself, and protected members can be accessed by the class itself and any derived classes.

References

Activity