C++ Access Specifiers C-PLUS-PLUS
- C++ Introduction
 - C++ Syntax
 - C++ Output (cout <<)
 - C++ Comments
 - C++ Variables
 - C++ User Input
 - C++ Data Types
 - C++ Operators
 - C++ Strings
 - C++ Math
 - C++ Booleans
 - C++ If Else
 - C++ Switch
 - C++ While Loop
 - C++ Do/While Loop
 - C++ For Loop
 - C++ Break and Continue
 - C++ Arrays
 - C++ References
 - C++ Pointers
 - C++ Functions
 - C++ Functions Parameters
 - C++ Function Overloading
 - C++ OOP
 - C++ Classes and Objects
 - C++ Class Methods
 - C++ Constructors
 - Destructors in C++
 - C++ Access Specifiers
 - C++ Encapsulation
 - C++ Inheritance
 - C++ Polymorphism
 - C++ Files
 - C++ Exception Handling
 
										C++ Access Specifiers																					
												
											
																				
										
										 										
									
									 
									
									C++ Access Specifiers
In C++, there are three access specifiers:
public- members are accessible from outside the classprivate- members cannot be accessed (or viewed) from outside the classprotected- members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Example
#include <iostream>
using namespace std;class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};int main() {
MyClass myObj;
myObj.x = 21; // Allowed (x is public)
myObj.y = 40; // Not allowed (y is private)
return 0;
}
Output:
In function 'int main()':
Line 8: error: 'int MyClass::y' is private
Line 14: error: within this context