Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, September 29, 2024

Friend Functions and Friend Classes in C++

 

Friend Functions and Friend Classes in C++

 

In C++, friend functions and friend classes provide a way to grant access to the private and protected members of a class to non-member functions or other classes. This feature is useful when you need to allow certain functions or classes to access the internals of a class without making those members public.

 

1. Friend Function

 

Definition: A friend function is a function that is not a member of a class but has access to its private and protected members. This allows the function to interact with the class in ways that regular non-member functions cannot.

 

Syntax:

class ClassName {

    friend return_type function_name(parameters); // Friend function declaration

private:

    // Private members

};

 

Example:

 #include <iostream>

using namespace std;

class A {

    int num;  // Private member

    // Declare friend function

    friend void show(A& obj); //declares a function show as a friend of class A,

    //allowing it to access private members of A.

public:

    A() //Constructor

    {

    num = 5;

    }

};


// Friend function definition

void show(A& obj) {

    cout << "Value of num: " << obj.num << endl;

}


int main() {

    A a;

    show(a);  // Access private member through friend function

    return 0;

}


Explanation:

- `calculateVolume` is a friend function of the `Box` class and can access its private members (`length`, `width`, `height`).

 

Key Points:

- Friend functions are not members of the class and do not have a `this` pointer.

- They are declared in the class using the `friend` keyword and defined outside the class.

- Friend functions can be global functions or member functions of other classes.

 

2. Friend Class

Definition: A friend class is a class that is granted access to the private and protected members of another class. This allows one class to access the internals of another class.

 

Syntax:

class ClassName1 {

    friend class ClassName2; // Friend class declaration

private:

    // Private members

};

 

Example:

#include <iostream>

using namespace std;

class A {

    int num;  // Private member

    // Declare B as a friend class

    friend class B;

public:

    A() //Constructor

    {

    num = 9;

    }

};


class B {

public:

    void show(A& obj) 

    {

        cout << "Value of num: " << obj.num << endl;

    }

};

int main() {

    A a;

    B b;

    b.show(a);  // Access private member through friend class

    return 0;

}


 

Key Points:

- Friend classes can access private and protected members of the class they are friends with.

- The friendship is not reciprocal; if `Engine` is a friend of `Car`, `Car` does not automatically become a friend of `Engine`.

- Friend classes are declared within the class whose members they are allowed to access.

 

Summary

 

Friend Functions:

 Access: Can access private and protected members of the class where they are declared as friends.

 Usage: Useful for functions that need to access class internals without being members of the class.

 Scope: Not a member of the class, so does not have access to `this` pointer.

 

Friend Classes:

 Access: Can access private and protected members of the class where they are declared as friends.

 Usage: Useful when one class needs to access the internals of another class, such as for implementing tightly coupled functionalities.

 Scope: The friendship is one-way; a friend class does not grant access to the class that declared it as a friend.


Both friend functions and friend classes provide a controlled way to grant access to a class’s private and protected members while keeping those members hidden from other parts of the code.

No comments:

Post a Comment