C++ Friend Functions



C++ Friend Function

A friend function of a class is defined outside that class scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, member function, or a class or class template, in which case the entire class and all of its members are friends.

Declaring Friend Function

To declare a function as a friend of a class, precede the function prototype in the class definition with the keyword friend as follows

Syntax

class Box {
   double width;
   
   public:
      double length;
      friend void printWidth( Box box );
      void setWidth( double wid );
};

To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne −

friend class ClassTwo;

Example of Friend Function

Here is the following code for Function Friend in C++:

#include <iostream>
 
using namespace std;
 
class Box {
   double width;
   
   public:
      friend void printWidth( Box box );
      void setWidth( double wid );
};

// Member function definition
void Box::setWidth( double wid ) {
   width = wid;
}

// Note: printWidth() is not a member function of any class.
void printWidth( Box box ) {
   /* Because printWidth() is a friend of Box, it can
   directly access any member of this class */
   cout << "Width of box : " << box.width <<endl;
}
 
// Main function for the program
int main() {
   Box box;
 
   // set box width without member function
   box.setWidth(10.0);
   
   // Use friend function to print the wdith.
   printWidth( box );
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

Width of box : 10

Accessing Private and Protected Members

The private and protected members of a class are not accessible outside of the class. Still, if you want to access them, you can use the friend function. The friend function provides the ability to directly access the class's private and protected members.

Example

The following example demonstrates accessing private and protected members of a class using the friend function:

#include <iostream>
using namespace std;

class ClassName {
 private:
  int privateData;  // Private member

 protected:
  int protectedData;  // Protected member

 public:
  ClassName() : privateData(0), protectedData(0) {}

  // Declare a friend function
  friend void friendFunction(ClassName& obj);
};

// Friend function definition
void friendFunction(ClassName& obj) {
  obj.privateData = 42;    // Access private member
  obj.protectedData = 24;  // Access protected member
  cout << "Private Data: " << obj.privateData << endl;
  cout << "Protected Data: " << obj.protectedData << endl;
}

int main() {
  ClassName obj;
  friendFunction(obj);  // Call the friend function
  return 0;
}

When the above code is compiled and executed, it produces the following result −

Private Data: 42
Protected Data: 24

Friend Function vs Member Function

In C++, both friend functions and member functions are used to access and manipulate the data of a class, but still, they have significant differences in their scope and usage.

Friend Function

A friend Function is a non-member function that is declared inside a class using the "friend" keyword, it has special access to the class's private and protected members. Since it's not a member it is not bound to a specific object, can't overloaded based on objects, not use this pointer, and cannot be inherited by derived classes. They are defined outside the class but declared inside it.

Member function

Whereas the member function is defined within the class and operates using this pointer. It can access all members of the class (private, protected, and public), and as it is tied to class objects, it can be overloaded and inherited by derived classes.

Friend Classes

In C++, a friend class is a class that gives access to private and protected members of another class. When a class declares another class as a friend, the second class (the friend) can directly access the private and protected members of the first class.

This concept is similar to friend functions, but here the friend is an entire class rather than a specific function.

Syntax

Here is the following syntax for the friend class in C++:

class ClassB;  // Forward declaration of ClassB

class ClassA {
private:
  int dataA;

public:
  // Declare ClassB as a friend of ClassA
  friend class ClassB;  
};

Example

The following example demonstrates the example of a friend class in C++:

#include <iostream>
using namespace std;

class ClassB;  // Forward declaration of ClassB

class ClassA {
 private:
  int dataA;

 public:
  ClassA() : dataA(42) {}  // Initialize dataA with a value

  // Declare ClassB as a friend of ClassA
  friend class ClassB;
};

class ClassB {
 public:
  void showDataFromA(const ClassA& objA) {
    // Access private member of ClassA
    cout << "Data from ClassA: " << objA.dataA << endl;
  }
};

int main() {
  ClassA objA;
  ClassB objB;

  // Use ClassB to access ClassA's private data
  objB.showDataFromA(objA);

  return 0;
}
Advertisements