C++ private 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 private
keyword is an access specifier that declares attributes and methods as private, which means that they are only accessible to methods within the class.
Related Pages
Read more about access specifiers in our C++ Access Specifiers Tutorial.