Friend Function in C++

 


1️⃣ What is a Friend Function?

A friend function in C++ is a special function that can access private and protected members of a class even though it is not a member of the class.

🔹 Key Features of Friend Function

Not a member of the class but has access to private data.
Declared inside the class but defined outside.
Uses friend keyword before the function declaration.
Can be a standalone function or a friend of multiple classes.


2️⃣ Syntax of Friend Function

class ClassName {
    private:
        int data;
    
    public:
        friend void friendFunction(ClassName obj);  // Friend Function Declaration
};

3️⃣ Example: Accessing Private Data Using Friend Function

#include <iostream>
using namespace std;

class Box {
private:
    int length;  // Private variable

public:
    Box(int l) {
        length = l;
    }

    // Declare friend function
    friend void showLength(Box obj);
};

// Friend function definition
void showLength(Box obj) {
    cout << "Length: " << obj.length << endl;  // Accessing private data
}

int main() {
    Box b(10);
    showLength(b);  // Calling friend function

    return 0;
}

🔹 Output

Length: 10

Why use a Friend Function?
✔ Can access private members without modifying the class.
✔ Useful for operator overloading and non-member functions.


4️⃣ Example: Friend Function Accessing Private Members of Two Classes

#include <iostream>
using namespace std;

class ClassB;  // Forward Declaration

class ClassA {
private:
    int numA;

public:
    ClassA(int a) { numA = a; }

    // Declare friend function
    friend void showData(ClassA objA, ClassB objB);
};

class ClassB {
private:
    int numB;

public:
    ClassB(int b) { numB = b; }

    // Declare friend function
    friend void showData(ClassA objA, ClassB objB);
};

// Friend function definition
void showData(ClassA objA, ClassB objB) {
    cout << "ClassA Num: " << objA.numA << endl;
    cout << "ClassB Num: " << objB.numB << endl;
}

int main() {
    ClassA objA(10);
    ClassB objB(20);

    showData(objA, objB);  // Calling friend function

    return 0;
}

🔹 Output

ClassA Num: 10
ClassB Num: 20

Why use a Friend Function in Multiple Classes?
✔ Allows data exchange between classes without inheritance.
Efficient for complex operations like mathematical calculations.


5️⃣ Friend Function in Operator Overloading

#include <iostream>
using namespace std;

class Complex {
private:
    int real, imag;

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

    // Friend function for addition
    friend Complex operator+(Complex c1, Complex c2);

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

// Overload + operator using a friend function
Complex operator+(Complex c1, Complex c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

int main() {
    Complex c1(3, 2), c2(1, 7);
    Complex c3 = c1 + c2;  // Calls friend function
    c3.display();

    return 0;
}

🔹 Output

4 + 9i

Why use a Friend Function for Operator Overloading?
✔ Allows direct access to private data for complex operations.
More readable and intuitive code.


6️⃣ Key Takeaways

Friend functions access private & protected members without being class members.
Declared inside a class, but defined outside.
Can be used for operator overloading and class interaction.
Breaks encapsulation, so use it only when necessary.

Would you like an example with friend class? 🚀

Post a Comment

0 Comments