Friend Function in C++

Friend Function in C++

What is Friend function in C++?

  • In C++, a friend function is a function that is not a member of a class but is granted special access to the private and protected members of that class.
  • A friend function is declared as a friend of the class using the friend keyword within the class definition.

Friend Function Syntax

Declaration of a friend function within a C++ class as follows:
Loading…

Characteristics of Friend function

  • A friend function is not a member of the class it's associated with. It is defined outside the class.
  • Friend functions are not inherited by derived classes. They are specific to the class in which they are declared as friends.
  • Friend functions do not affect the member functions of a class.

C++ Friend Function Example

Example of a Friend Function:
Loading…
  • In this example:
  • MyClass is a class with private member data.
  • friendFunction is declared as a friend function within the MyClass class.
  • Inside friendFunction, we can access and display the private member data of MyClass objects.

How to use friend function for two classes in C++?

let's see an example below:
Loading…
  • We have two classes, ClassA and ClassB, each with its own private data member (dataA and dataB, respectively).
  • Both ClassA and ClassB declare the same friend function friendFunction using the friend keyword.

What is a friend class in C++?

  • C++, a friend class is a class that is granted access to the private and protected members of another class.
  • Here's an example of a friend class:
Loading…
  • We have two classes, MyClass and FriendClass.
  • MyClass has a private data member privateData, and it declares FriendClass as a friend class using friend class FriendClass.
  • This allows FriendClass to access the private members of MyClass.

What are forward declarations in C++?

  • In C++, a forward declaration is a way to declare the existence of a class, function, or variable without providing its complete definition.
  • It's essentially a way to tell the compiler that a certain entity exists and will be defined later in the code.

What is the difference between friend and member functions in C++?

  • The key difference between friend and member functions in C++
  • member functions are part of the class and have direct access to its private members,
  • while friend functions are external to the class but can access its private members if explicitly declared as friends.

What is the difference between friend function and operator overloading?

  • Friend Function: It can be a standalone function of another class but is not a member of the class it's providing access to.
  • Operator Overloading: It typically involves defining member functions within the class, known as operator overloading functions.
  • Friend Function: It can access and modify private members of a class by being explicitly declared as a friend.
  • Operator Overloading: It can access and modify private members of a class if the operator overloading function is a member of that class.

Conclusion

A friend function in C++ is a non-member function that is granted special access to the private and protected members of a class. It is declared as a friend within the class, allowing it to work with the class's private data.