The private access specifier restricts the access of class members only to the class itself. Private members cannot be accessed or modified directly from outside the class.
Example:
class MyClass {
private:
int privateVar;
void privateMethod() {
// Code for the private method
}
public:
void accessPrivate() {
privateVar = 10; // Accessing private member variable
privateMethod(); // Calling private member function
}
};
int main() {
MyClass obj;
obj.accessPrivate(); // Accessing private members indirectly
return 0;
}