C++ Program using friend function


In this program, You will learn how to implement friend function in C++.


friend void display();

Example: How to implement friend function in C++.

#include<iostream>
using namespace std;

class Test {
private:
    int x;
public:

    Test() {
        x = 10;
    }

    friend void display(Test &t);
};

void display(Test &t) {
    cout << "x value is:" << t.x;
}

int main() {

    Test t;
    display(t);

    return 0;
}

Output:

x value is:10