Friend Class & Friend Functions

In C++, the friend keyword is used to grant non-member functions or classes access to the private and protected members of a class. This mechanism is often used when you need to provide certain functions or classes with special privileges to access internal data or methods of a class. Here's how friend works and some interview questions related to it:

Friend Functions:

A friend function is a function that is not a member of a class but has access to its private and protected members. To declare a friend function, you use the friend keyword inside the class declaration. Here's an example:

class MyClass {
private:
    int privateData;

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

    // Declare a friend function
    friend void FriendFunction(const MyClass&);
};

// Define the friend function
void FriendFunction(const MyClass& obj) {
    // Friend function can access privateData
    std::cout << "FriendFunction accessed privateData: " << obj.privateData << std::endl;
}

int main() {
    MyClass obj(42);
    FriendFunction(obj); // Call the friend function

    return 0;
}

In this example, FriendFunction is declared as a friend function inside the MyClass definition. It can access the private member privateData of MyClass objects.

Interview Questions:

  1. What is a friend function in C++?

    • A friend function is a function that is not a member of a class but has access to its private and protected members.

  2. Why would you use a friend function?

    • Friend functions are used to grant non-member functions or classes special access to the private and protected members of a class when it's necessary for the design or implementation of a particular feature.

  3. What is the difference between a friend function and a member function?

    • Member functions are part of a class and have direct access to its private and protected members. Friend functions are not part of the class but have been granted access to these members.

  4. Can a friend function access static members of a class?

    • Yes, friend functions can access static members (both data members and member functions) of a class.

  5. Can you have multiple friend functions for a single class?

    • Yes, you can declare and define multiple friend functions for a single class.

Friend Classes:

A friend class is a class that is granted access to the private and protected members of another class. You declare a friend class by using the friend keyword inside the class definition. Here's an example:

class MyClass {
private:
    int privateData;

    // Declare a friend class
    friend class FriendClass;

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

class FriendClass {
public:
    void AccessPrivateData(const MyClass& obj) {
        // Friend class can access privateData
        std::cout << "FriendClass accessed privateData: " << obj.privateData << std::endl;
    }
};

int main() {
    MyClass obj(42);
    FriendClass fc;
    fc.AccessPrivateData(obj); // Call a method of the friend class

    return 0;
}

In this example, FriendClass is declared as a friend class of MyClass, which allows it to access the private member privateData of MyClass objects.

Interview Questions:

  1. What is a friend class in C++?

    • A friend class is a class that is granted access to the private and protected members of another class.

  2. What's the difference between a friend function and a friend class?

    • A friend function is a standalone function that has access to the private and protected members of a class, while a friend class is a whole class that is granted such access.

  3. Can you have multiple friend classes for a single class?

    • Yes, you can declare multiple friend classes for a single class.

  4. When might you use a friend class instead of a friend function?

    • You might use a friend class when you want an entire class to have access to the private and protected members of another class, perhaps for implementing a closely related feature or to simplify interactions between the two classes.

Last updated

Was this helpful?