0% found this document useful (0 votes)
11 views

C++ Access Specifiers

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C++ Access Specifiers

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1/26/24, 10:23 PM C++ Access Specifiers

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

C++ Access Specifiers


❮ Previous Next ❯

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
public: // Access specifier
// class members goes here
};

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?

In C++, there are three access specifiers:

public - members are accessible from outside the class


private - members cannot be accessed (or viewed) from outside the class

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 1/6
1/26/24, 10:23 PM C++ Access Specifiers

protected - members cannot be accessed from outside the class, however,


 Tutorials  Exercises  Services   Sign Up Log in
they can be accessed in inherited classes. You will learn more about Inheritance
later.
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
In the following example, we demonstrate the differences between public and
private members:

Example
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;
}

If you try to access a private member, an error occurs:

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:

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 2/6
1/26/24, 10:23 PM C++ Access Specifiers

 Tutorials 
Example
Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
class MyClass {
int x; // Private attribute
int y; // Private attribute
};

❮ Previous Log in to track progress Next ❯

ADVERTISEMENT

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 3/6
1/26/24, 10:23 PM C++ Access Specifiers

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

COLOR PICKER



ADVERTISEMENT

ADVERTISEMENT

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 4/6
1/26/24, 10:23 PM C++ Access Specifiers

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS
 SPACES
JAVASCRIPT SQL
UPGRADE
PYTHON JAVA
AD-FREE
PHP HOW TO W3.CSS C

NEWSLETTER GET CERTIFIED

REPORT ERROR

Top Tutorials
HTML Tutorial
CSS Tutorial
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 Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 5/6
1/26/24, 10:23 PM C++ Access Specifiers

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS  JAVASCRIPT
   SQL
FORUM PYTHON
ABOUT JAVA PHP HOW TO W3.CSS C

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.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

https://www.w3schools.com/cpp/cpp_access_specifiers.asp 6/6

You might also like