The public access specifier allows class members to be accessed from anywhere in the program. Public members are visible to all code that has access to the class.
Example:
class MyClass {
public:
int publicVar;
void publicMethod() {
// Code for the public method
}
};
int main() {
MyClass obj;
obj.publicVar = 42; // Accessing public member variable
obj.publicMethod(); // Calling public member function
return 0;
}