Scroll Progress Bar

Access Specifiers

In C++, access specifiers are keywords that control the visibility and accessibility of class members (data members and member functions) from outside the class. C++ provides three primary access specifiers: public, private, and protected. Here's an explanation of each access specifier:

Public Access Specifier:

Members declared as public are accessible from anywhere in the program, both inside and outside the class. Public members it can be accessed through objects of the class.

Program:

class MyClass {
public:
    int publicVar; // Public data member
    
    void publicFunction() {
        // Public member function
    }
};
Program:

MyClass obj;
obj.publicVar = 42;     // Accessing public data member
obj.publicFunction();   // Accessing public member function
Private Access Specifier:

Members declared as private are accessible only within the class in which they are defined. Private members it cannot be accessed directly from outside the class.

Program:

class MyClass {
private:
    int privateVar; // Private data member
    
    void privateFunction() {
        // Private member function
    }
};
Program:

MyClass obj;
obj.privateVar = 42;     // Compilation error, it cannot access private data member
obj.privateFunction();   // Compilation error, it cannot access private member function

To access private members, typically provide public member functions that act as interfaces to these private members.

Protected Access Specifier:

Members declared as protected are similar to private members in that they are not accessible from outside the class. However, they it can be accessed by derived classes (classes that inherit from the base class).

Program:

class MyBaseClass {
protected:
    int protectedVar; // Protected data member
    
    void protectedFunction() {
        // Protected member function
    }
};
Program:

class MyDerivedClass : public MyBaseClass {
public:
    void accessProtected() {
        protectedVar = 42;     // Accessing protected data member (from derived class)
        protectedFunction();   // Accessing protected member function (from derived class)
    }
};

In this example, protectedVar and protectedFunction are accessible within MyDerivedClass, which inherits from MyBaseClass.

Access Specifiers in Inheritance:

Access specifiers also play a role in inheritance. The visibility of base class members in a derived class depends on the access specifier used during inheritance.

The three common forms of inheritance are:
  • public inheritance: Public members of the base class remain public in the derived class, protected members remain protected, but private members are not accessible.
  • protected inheritance: Public and protected members of the base class become protected in the derived class, while private members remain inaccessible.
  • private inheritance: All members of the base class become private in the derived class, regardless of their original access level.
Program:

class Base {
public:
    int pubVar;
protected:
    int protVar;
private:
    int privVar;
};

class DerivedPublic : public Base {
    // pubVar is public, protVar is protected, privVar is inaccessible
};

class DerivedProtected : protected Base {
    // pubVar and protVar are protected, privVar is inaccessible
};

class DerivedPrivate : private Base {
    // All members of Base are private in DerivedPrivate
};

Access specifiers are crucial for encapsulation and data hiding, allowing to control the level of visibility and access to class members based on design and security requirements.


Whatis the default access specifier for class members in C++?


private

Which access specifier allows class members to be accessed from outside the class?


public

Which access specifier allows class members to be accessed within the class and its derived classes?


protected

Whatdoes the private access specifier restrict in a class?


Access to members from outside the class.

Whatis the primary purpose of access specifiers in C++ classes?


Controlling the visibility and accessibility of class members.