C++ private Keyword
The private
keyword in C++ is used to define the access level of class members. Members declared as private
are accessible only within the class in which they are declared. They cannot be accessed directly from outside the class, including by objects of the class or derived classes.
By default, all members of a class are private unless explicitly specified as public
or protected
.
Syntax
</>
Copy
class ClassName {
private:
// Private members
};
- class
- The keyword used to define a class.
- private
- The access specifier indicating that the members below it are private.
- members
- The data members or member functions that are accessible only within the class.
Examples
Example 1: Accessing Private Members through Public Functions
In this example, you will learn how private members can be accessed indirectly using public member functions.
</>
Copy
#include <iostream>
using namespace std;
class Person {
private:
string name; // Private member
public:
// Setter function to modify private member
void setName(const string& newName) {
name = newName;
}
// Getter function to access private member
string getName() const {
return name;
}
};
int main() {
Person person;
person.setName("Alice"); // Modify private member through public function
cout << "Name: " << person.getName() << endl; // Access private member through public function
return 0;
}
Output:
Name: Alice
Explanation:
- The
name
member is declared as private, so it cannot be accessed directly outside the class. - The public functions
setName
andgetName
provide controlled access to the private member. - The
main
function uses these public methods to modify and access the private member.
Example 2: Attempting to Access Private Members Directly
In this example, you will learn how the error is generated when trying to access private members directly.
</>
Copy
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width; // Private members
public:
Rectangle(int l, int w) : length(l), width(w) {}
int area() const {
return length * width; // Access private members within the class
}
};
int main() {
Rectangle rect(5, 3);
// Uncommenting the line below will cause a compilation error
// cout << "Length: " << rect.length << endl; // Direct access to private member
cout << "Area: " << rect.area() << endl; // Use public method to access private members
return 0;
}
Output:
Area: 15
Explanation:
- The
length
andwidth
members are private, so direct access outside the class is not allowed. - An attempt to access these private members directly in the
main
function results in a compilation error. - The
area
public method is used to calculate and return the area of the rectangle, allowing indirect access to the private members.
Key Points about private
Keyword
- The
private
keyword restricts access to members within the class in which they are declared. - Private members cannot be accessed directly from outside the class, ensuring encapsulation.
- Public methods (getters and setters) are commonly used to provide controlled access to private members.
- By default, all members of a class are private unless explicitly specified as
public
orprotected
. - Encapsulation provided by
private
helps in hiding implementation details and ensuring data integrity.