C++ Friend Function

A friend function in C++ is a non-member function that is given access to the private and protected members of a class. The function is declared using the keyword “friend” inside the class, but it is defined outside the class. Friend functions are not in the scope of the class and are not considered a member of the class.

Declaring a Friend Function

A friend function is declared inside a class using the keyword “friend” followed by the function prototype. For example:

class Rectangle {
    int width, height;
  public:
    int area() {return width * height;}
    friend void printWidth(Rectangle &);
};

In this example, the function “printWidth” is a friend function of the class “Rectangle”. It can access the private members “width” and “height” of the class.

Defining a Friend Function

A friend function is defined outside the class just like any other non-member function. The function definition does not need the keyword “friend” again. For example:

void printWidth(Rectangle &r) {
    cout << "Width of rectangle : " << r.width << endl;
}

In this example, the friend function “printWidth” is defined outside the class “Rectangle” and it can access the private member “width” of the class.

Using a Friend Function

Friend functions can be used like any other function. They can be called using the object of the class or using the class name followed by the scope resolution operator (::). For example:

Rectangle rect;
rect.width = 10;
rect.height = 20;
printWidth(rect); // calling friend function

Examples

Example 1: Friend Function to Access Private Members

The following example demonstrates how a friend function can access the private members of a class.

#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void setWidth(int w) { width = w; }
    void setHeight(int h) { height = h; }
    friend int getWidth(Rectangle &);
    friend int getHeight(Rectangle &);
};

int getWidth(Rectangle &r) {
    return r.width;
}
int getHeight(Rectangle &r) {
    return r.height;
}

int main() {
    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(10);

    cout << "Width of rectangle : " << getWidth(rect) << endl;
    cout << "Height of rectangle : " << getHeight(rect) << endl;
    return 0;
}

Example 2: Friend Function as a Member of a Different Class

The following example demonstrates how a friend function can be a member of a different class.

#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void setWidth(int w) { width = w; }
    void setHeight(int h) { height = h; }
    friend class Square; // friend class declaration
};

class Square {
    int side;
  public:
    void setSide(Rectangle &r) { side = r.width; }
    int getArea() { return side * side; }
};

int main() {
    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(10);

    Square sq;
    sq.setSide(rect);

    cout << "Area of square : " << sq.getArea() << endl;
    return 0;
}

Example 3: Friend Function as a Template Function

The following example demonstrates how a friend function can be a template function.

#include <iostream>
using namespace std;
template 
class Stack {
T arr[100];
int top;
public:
Stack() { top = -1; }
void push(T x) { arr[++top] = x; }
T pop() { return arr[top--]; }
friend void printStack(Stack &);
};

template 
void printStack(Stack &s) {
for (int i = 0; i <= s.top; i++)
cout << s.arr[i] << " ";
cout << endl;
}

int main() {
Stack s;
s.push(1);
s.push(2);
s.push(3);
printStack(s);
return 0;
}

Explanation

Example 1: Friend Function to Access Private Members

In this example, two friend functions “getWidth” and “getHeight” are declared inside the class “Rectangle” with the keyword “friend”. These functions are defined outside the class and they can access the private members “width” and “height” of the class “Rectangle” respectively. The main function sets the width and height of the rectangle and then calls the friend functions to get the width and height. The output of the program will be:

Width of rectangle : 5
Height of rectangle : 10

Example 2: Friend Function as a Member of a Different Class

In this example, the class “Square” is declared as a friend class of the class “Rectangle” using the keyword “friend”. This means that the members of the class “Square” can access the private members of the class “Rectangle”. The main function sets the width of the rectangle and then the side of the square is set using the width of the rectangle. Then the area of the square is calculated and printed. The output of the program will be:

Area of square : 25

Example 3: Friend Function as a Template Function

In this example, a template class “Stack” is defined with a template function “printStack” declared as a friend function. The main function creates an object of the class “Stack” and pushes some elements into it. Then it calls the friend function “printStack” to print the elements of the stack. The output of the program will be:

1 2 3

It’s worth noting that, friend functions and classes are not inherited, a derived class will not have access to the private members of its base class if it is not explicitly declared as a friend. Also, it is not recommended to use friend functions and classes excessively as it can make the class design less encapsulated and harder to maintain.

Leave a Reply

Your email address will not be published. Required fields are marked *