Friend class in C++

A friend class has access to the private variables of the class to which it’s friend. However, it does not inherit another class and the members of first class do not become members of another class.

For Example:

#include <iostream>

using namespace std;

class class1 {

int c;

int d;


class1(int e, int f) { c = e; d = f; }

friend class class2;


class class2 {


int min(class1 g);


int class2::min(class1 x)


return g.c < g.d ? g.c : g.d;


int main()


class1 ob(25, 30);

class2 x;

cout << x.min(ob);

return 0;


Leave a Reply