C++ public Keyword
Example
The difference between public
and private
attributes:
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Definition and Usage
The public
keyword is an access specifier that declares attributes and methods as public, which means that they can be accessed from anywhere outside of the class.
Related Pages
Read more about access specifiers in our C++ Access Specifiers Tutorial.