Introduction

In C++, the concept of “friend” is used to allow certain functions or classes to have access to the private and protected members of another class. This is useful in scenarios where you want to grant specific external functions or classes special privileges to access private members without making them public, thus maintaining encapsulation. There are two primary uses of the “friend” keyword: friend functions and friend classes.

Friend function

In object-oriented programming, a friend function is a special type of function that is granted access to the private and protected members of a class. Normally, the private members of a class are accessible only by other member functions within the same class. However, sometimes it is necessary for a function outside the class to access these private members.

here are some important points about friend functions in C++:

  1. Friend functions are not members of a class: Unlike regular member functions, friend functions are not part of the class in which they are declared. They are external functions that have access to the private and protected members of the class.
  2. Declaration inside the class: Friend functions are declared inside the class definition, but their actual implementation is done outside the class.
  3. Access to private members: A friend function can access private and protected members of the class it is declared as a friend of. This allows the function to work with the internal data of the class, even though it is not a member of the class.
  4. Not inherited or overridden: Friend functions are not inherited by derived classes, and they cannot be overridden. Each class that wants to use the friend function must declare it as a friend.
  5. Can be a regular or a member function of another class: A friend function can be a regular standalone function or a member function of another class. In the latter case, it can access the private members of both the friend class and the class it is a member of.
  6. Friendship is not mutual: The “friendship” is not mutual, meaning that if Class A declares Class B as a friend, it doesn’t automatically make Class A a friend of Class B. Class B needs to explicitly declare Class A as a friend if it wants to access its private members.
  7. Friendships are not transitive: Friendship is not transitive, meaning that if Class A is a friend of Class B, and Class B is a friend of Class C, it does not imply that Class A is a friend of Class C.
  8. Use with caution: While friend functions can be useful in certain situations, they can also break encapsulation and lead to less maintainable code if used excessively. It’s generally a good practice to minimize the use of friend functions and prefer encapsulation whenever possible.

To declare a friend function in a class, you use the friend keyword in the class declaration and then define the function outside the class like a regular function.

Here’s an example of a friend function:

#include <iostream>

class MyClass {
private:
    int privateData;

public:
    MyClass(int data) : privateData(data) {}

    // Declare the friend function inside the class
    friend void friendFunction(MyClass obj);
};

// Define the friend function outside the class
void friendFunction(MyClass obj) {
    // The friend function can access the private member of the class
    std::cout << "Value of privateData: " << obj.privateData << std::endl;
}

int main() {
    MyClass myObj(42);

    // Call the friend function from the main function
    friendFunction(myObj);

    return 0;
}

In this example, the friendFunction is declared as a friend of the MyClass class. It can access the private member privateData of the MyClass objects, as shown in the main function.

Friend Class

In C++, a friend class is a concept that allows one class to grant access to its private and protected members to another class. By declaring a class as a friend, you are giving that class special permission to access private and protected members of the declaring class. To declare a friend class in C++, you need to add a friend declaration inside the class definition.

The main points of a friend class in C++ are:

  1. Access to Private and Protected Members: A friend class can access the private and protected members (data members and member functions) of the class it is declared as a friend of.
  2. Declaration: To declare a class as a friend, you need to use the friend keyword followed by the class declaration inside the target class’s body.
  3. Two-Way Relationship: The friendship relationship is not symmetric. If class A is a friend of class B, it doesn’t automatically mean that class B is a friend of class A. Friendship must be explicitly declared in both directions if needed.
  4. No Inheritance: Friendship is not inherited. In other words, if a class is a friend of another class, its subclasses do not automatically become friends of that class.
  5. Limited Scope: Friendships are limited to the classes in which they are declared. A friend class does not have access to the private members of other instances of the same class.
  6. Granularity Control: Friendships can be used to provide limited access to specific classes, helping to control the granularity of the access granted.

Usage of friend classes should be handled with caution, as it can violate encapsulation and lead to increased coupling between classes. In many cases, alternative designs using accessors and mutators (getters and setters) may be preferred to minimize direct access to private members.

Here’s a simple example of how you can use a friend class:

#include <iostream>

class MyClass {
private:
    int privateMember;

public:
    MyClass(int value) : privateMember(value) {}

    friend class FriendClass;
};

class FriendClass {
public:
    void accessPrivateMember(const MyClass& obj) {
        // Accessing the private member of MyClass
        std::cout << "Value of privateMember: " << obj.privateMember << std::endl;
    }
};

int main() {
    MyClass obj(42);
    FriendClass fc;
    fc.accessPrivateMember(obj); // Output: Value of privateMember: 42
    return 0;
}

Keep in mind that friend classes can only access the private and protected members of the declaring class, and they do not inherit the members of the class they are friends with.


more related content on Object Oriented Programming