C++ Access Specifiers
C++ Access Specifiers
Menu Log in
HTML CSS
Access Specifiers
By now, you are quite familiar with the public keyword that appears in all of our
class examples:
Example
};
Try it Yourself »
The public keyword is an access specifier. Access specifiers define how the
members (attributes and methods) of a class can be accessed. In the example above,
the members are public - which means that they can be accessed and modified
from outside the code.
However, what if we want members to be private and hidden from the outside world?
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 1/6
9/1/2021 C++ Access Specifiers
Example
class MyClass {
private: // Private access specifier
};
int main() {
MyClass myObj;
return 0;
error: y is private
Try it Yourself »
Note: It is possible to access private members of a class using a public method inside
the same class. See the next chapter (Encapsulation) on how to do this.
Tip: It is considered good practice to declare your class attributes as private (as often
as you can). This will reduce the possibility of yourself (or others) to mess up the
code. This is also the main ingredient of the Encapsulation concept, which you will
learn more about in the next chapter.
Note: By default, all members of a class are private if you don't specify an access
specifier:
Example
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 2/6
9/1/2021 C++ Access Specifiers
class MyClass {
};
❮ Previous Next ❯
NEW
We just launched
W3Schools videos
Explore now
COLOR PICKER
LIKE US
Get certified
by completing
a course today!
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 3/6
9/1/2021 C++ Access Specifiers
school
w3 s
1
CE
02
TI 2
R
FI .
ED
Get started
CODE GAME
Play Game
Report Error
Forum
About
Shop
Top Tutorials
HTML Tutorial
CSS Tutorial
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 4/6
9/1/2021 C++ Access Specifiers
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Web Courses
HTML Course
CSS Course
JavaScript Course
SQL Course
Python Course
PHP Course
jQuery Course
Java Course
C++ Course
C# Course
XML Course
Get Certified »
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 5/6
9/1/2021 C++ Access Specifiers
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness of all content.
While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/cpp/cpp_access_specifiers.asp 6/6