C++ The Friend Keyword
C++ Friend Functions
Normally, private
members of a class can only be accessed using public methods like getters and setters. But in some cases, you can use a special function called a friend
function to access them directly.
A friend
function is not a member of the class, but it is allowed to access the class's private data:
Example
class Employee {
private:
int salary;
public:
Employee(int s) {
salary = s;
}
// Declare friend function
friend void displaySalary(Employee emp);
};
void displaySalary(Employee emp) {
cout << "Salary: " << emp.salary;
}
int main() {
Employee myEmp(50000);
displaySalary(myEmp);
return 0;
}
Example Explained
- The
friend
functiondisplaySalary()
is declared inside theEmployee
class but defined outside of it. - Even though
displaySalary()
is not a member of the class, it can still access the private membersalary
. - In the
main()
function, we create anEmployee
object and call the friend function to print its salary.