Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Friend Functions in C++

Introduction

In C++, a friend function is a function that is not a member of a class but still has access to its private and protected members. This can be useful when you need to allow an external function to access the private data of a class without exposing the data to the rest of the program. Friend functions are declared using the friend keyword within the class whose private or protected data is to be accessed.

Declaring a Friend Function

To declare a friend function, you use the friend keyword within the class definition. Here is the basic syntax:

class ClassName {
    friend ReturnType FunctionName(Parameters);
};
                

Let's break this down:

  • ReturnType: The return type of the friend function.
  • FunctionName: The name of the friend function.
  • Parameters: The parameters that the friend function takes.

Example: Friend Function

Consider a class Box that has a private member variable length. We want to create a friend function that can access this private member. Here is how you can do it:

#include <iostream>
using namespace std;

class Box {
private:
    double length;

public:
    Box() : length(0.0) {}

    // Friend function declaration
    friend void printLength(Box);
};

// Friend function definition
void printLength(Box b) {
    cout << "Length of box: " << b.length << endl;
}

int main() {
    Box box;
    printLength(box);  // Accessing private member of Box
    return 0;
}
                

In this example:

  • The class Box has a private member variable length.
  • The friend function printLength is declared inside the class Box.
  • The friend function printLength is defined outside the class and it has access to the private member length.

Advantages of Friend Functions

Friend functions can be useful in several scenarios:

  • Operator Overloading: Certain operators can be overloaded more easily with friend functions.
  • Non-Member Functions: When you need a function that operates on multiple objects of different classes, friend functions can access the private data of those objects.
  • Enhanced Functionality: They can help in providing some specific functionality that cannot be achieved with member functions alone.

Example: Operator Overloading

Friend functions are often used for overloading operators. Here is an example of overloading the + operator using a friend function:

#include <iostream>
using namespace std;

class Complex {
private:
    int real;
    int imag;

public:
    Complex() : real(0), imag(0) {}
    Complex(int r, int i) : real(r), imag(i) {}

    // Friend function for overloading +
    friend Complex operator+(Complex const &, Complex const &);

    void print() {
        cout << real << " + i" << imag << endl;
    }
};

Complex operator+(Complex const &c1, Complex const &c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

int main() {
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2;  // Adding two complex numbers using the overloaded operator
    c3.print();
    return 0;
}
                

In this example:

  • The class Complex has two private member variables real and imag.
  • The friend function operator+ is used to overload the + operator.
  • The overloaded + operator can access the private members of the Complex class.

Summary

Friend functions in C++ are a powerful feature that allows external functions to access the private and protected members of a class. They are declared using the friend keyword within the class and can be used for various purposes such as operator overloading and providing specific functionalities that cannot be achieved with member functions alone.

Output

For the provided examples, the outputs would be:

First Example:

Length of box: 0

Second Example:

12 + i9