Friend Function in C++

 Friend Function

 

A friend function is a function that is not a member of a class but is granted special access to the class’s private and protected members.

Declared using the keyword friend inside the class.


Syntax

class ClassName {

private:

    int data;

 

public:

    // Declaration of friend function

    friend void display(ClassName obj);

};

 

// Friend function definition (outside class)

void display(ClassName obj) {

    // Can access private data

    cout << "Data = " << obj.data;

}


Key Points

  1. Declared inside class but defined outside (not a member).
  2. Can access private & protected members of the class.
  3. Declared with the friend keyword inside the class.
  4. Not in the scope of the class, so it is called like a normal function.
  5. Friendship is not mutual: If Class A declares Class B as a friend, Class B does not automatically make Class A a friend.
  6. Friendship is not inherited: Derived classes do not automatically inherit friendship.
  7. It can be:
    • A normal function
    • A member function of another class
    • Even an entire class

Example: Simple Friend Function

#include <iostream>

using namespace std;

 

class Box {

private:

    int length;

 

public:

    Box(int l) : length(l) {}

 

    // Friend function declaration

    friend void printLength(Box b);

};

 

// Friend function definition

void printLength(Box b) {

    cout << "Length of box: " << b.length << endl;

}

 

int main() {

    Box b1(10);

    printLength(b1);   // Can access private length

    return 0;

}

 

Output:

Length of box: 10


Advantages

  • Allows operator overloading (especially binary operators) to access private data.
  • Helpful in two classes, interacting closely.
  • Increases flexibility in special cases.

Disadvantages

  • Breaks the rule of data hiding (less secure).
  • Too many friend functions may make the class design less robust.

 

Usages of Friend Functions


1: Access Private Data of a Class

When an outside function needs to access private data of a class.

 

Example

#include <iostream>

using namespace std;

 

class Sample

{

private:

    int x;

public:

    Sample(int a) { x = a; }

    friend void display(Sample);

};

 

void display(Sample s)

{

    cout << "Value of x = " << s.x;

}

 

int main()

{

    Sample obj(10);

    display(obj);

    return 0;

}


 

2: Sharing Data Between Two Classes

When two or more classes need to share their private data.

 

Example

#include <iostream>

using namespace std;

 

class B;   // forward declaration

 

class A

{

private:

    int x;

public:

    A(int a) { x = a; }

    friend void sum(A, B);

};

 

class B

{

private:

    int y;

public:

    B(int b) { y = b; }

    friend void sum(A, B);

};

 

void sum(A a, B b)

{

    cout << "Sum = " << a.x + b.y;

}

 

int main()

{

    A obj1(10);

    B obj2(20);

    sum(obj1, obj2);

    return 0;

}


3: Operator Overloading Using Friend Function

When operator overloading requires access to private members.

 

Example (+ Operator)

#include <iostream>

using namespace std;

 

class Number

{

private:

    int x;

public:

    Number(int a) { x = a; }

    friend Number operator +(Number, Number);

};

 

Number operator +(Number n1, Number n2)

{

    Number temp(0);

    temp.x = n1.x + n2.x;

    return temp;

}

 

int main()

{

    Number a(10), b(20);

    Number c = a + b;

    return 0;

}


 

4: Function Working with Multiple Objects

When a function needs to work with multiple objects of the same or different classes.

Member functions work on one object; friend functions can work on many objects.

 

Example

friend int compare(A, A);


 

5: Overloading Binary Operators with Different Data Types

 

Example: Object + Integer

#include <iostream>

using namespace std;

 

class Test

{

    int x;

public:

    Test(int a) { x = a; }

    friend Test operator +(Test, int);

};

 

Test operator +(Test t, int y)

{

    return Test(t.x + y);

}

 

int main()

{

    Test obj(10);

    Test res = obj + 5;

    return 0;

}

==========================================================================

 

Post a Comment

0 Comments