# Friend Function

Friend functions in C++ are a special type of functions that are granted access to private and protected members of a class, even though they are not members of that class.

Key points about friend functions:

1. Declaration: Friend functions are declared inside the class definition using the `friend` keyword, but they are not member functions of the class.
2. Access: They can access private and protected members of the class without being a member of that class.
3. Scope: They are not in the scope of the class, and they don't have a `this` pointer.
4. Declaration vs. Definition: The function can be declared as a friend in the class definition, but the actual function definition is outside the class.
5. Cannot be inherited: Friendship is not inherited, meaning if a base class has a friend function, it doesn't automatically become a friend of the derived class.

Here's a simple example to illustrate friend functions:

```cpp
#include <iostream>

class Box {
private:
    int width, height, depth;

public:
    Box(int w, int h, int d) : width(w), height(h), depth(d) {}

    // Declaration of friend function
    friend int getBoxVolume(const Box& b);

    // Friend function can be a member of another class
    friend class BoxPrinter;
};

// Definition of friend function
int getBoxVolume(const Box& b) {
    // Can access private members of Box
    return b.width * b.height * b.depth;
}

class BoxPrinter {
public:
    void printBoxDimensions(const Box& b) {
        // Can access private members of Box
        std::cout << "Width: " << b.width 
                  << ", Height: " << b.height 
                  << ", Depth: " << b.depth << std::endl;
    }
};

int main() {
    Box myBox(3, 4, 5);
    std::cout << "Volume: " << getBoxVolume(myBox) << std::endl;

    BoxPrinter printer;
    printer.printBoxDimensions(myBox);

    return 0;
}
```

> Run it [here](https://onecompiler.com/cpp/42m3t6sty).

In this example:

1. `getBoxVolume` is a friend function of the `Box` class. It can access private members of `Box`.
2. `BoxPrinter` is a friend class of `Box`. All its member functions can access private members of `Box`.
3. The `getBoxVolume` function and `BoxPrinter::printBoxDimensions` method can access `width`, `height`, and `depth` directly, even though they're private.

Use cases for friend functions:

1. When you need a function to have access to private members of a class, but you don't want it to be a member function.
2. For operator overloading, especially when the left-hand operand is not an object of the class (like stream insertion operators).
3. To allow a function to work intimately with two different classes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/_/cpp/code/classes/friend-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
