Access Specifier
Access Specifier
Private
Protected
public
❮ PreviousNext ❯
Access Specifiers
By now, you are quite familiar with the public keyword that
appears in all of our class examples:
Example
class MyClass { // The class
private: int roll; // Access specifier
// class members goes here
};
Try it Yourself »
Example
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
protected:
int z;
};
}
int main() {
clild myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
myObj.z=100; // Allowed (protected)
return 0;
}