Private Access Modifiers

dakshdeepHERE's avatar
Published Feb 5, 2025
Contribute to Docs

In C++, the private keyword keeps class members hidden. This means that only the methods within the same class can access these members. Objects of the class and functions outside the class cannot access them directly.

Syntax

class ClassName {
private:
  // Private members
};

Example

In the example below, attempting to directly access the private data member secretVar from outside the class would result in a compilation error:

#include <iostream>
class Secret {
private:
// Private member variable
int secretVar;
public:
// Public member function to set the private variable
void setSecret(int value) {
secretVar = value;
}
// Public member function to reveal the private variable
int revealSecret() {
return secretVar;
}
};
int main() {
Secret mySecret;
// Throws an error
// mySecret.secretVar = 10;
// Error: secretVar is private
// Use public function to set the value
mySecret.setSecret(10);
// Use public function to get the value
std::cout << mySecret.revealSecret();
return 0;
}

The above code will give the following output:

10

Codebyte Example

Run the following codebyte example to understand how the private access modifiers work:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy