Understanding Friend Function and Characteristics In C++

Need of Friend Function

Friend function has privileges to access all private and protected members (variables and functions) of the Class.

According to the data hiding concept in C++, no one has the permission to access private and protected members from outside the class. If a developer needs access to the private or protected members, then the developer can use friend functions. A friend function provides privileges to access all the private and protected members of the class.

Friend Function In C++

Friend Function Definition

If a function is friend function of a class, then the friend function is not the actual member of the class. But that friend function has rights to access to all private and protected members (variables and functions).

Friend Function Syntax

class class_name {
   //Member variables and Function

   // Friend Function Declaration
   friend return_type friend_function_name(arguments / objects);
}

// Friend Function Definition

return_type friend_function_name(arguments / objects) {
   // Friend function has privileges to access all private and protected members of the class
}

Friend Function Characteristics

  • The friend function is not in class scope.
  • It should be declared inside the class with ‘friend’ keyword.
  • It can be defined anywhere in the program and outside of the class like a normal function. But friend keyword must not be used in the function definition.
  • It is not in class scope. So it cannot be called by objects of the class.
  • It can be called like a normal function.
  • Usually, it has class objects as arguments.
  • It cannot access member variables or functions directly, but can only be accessed by the help of the objects of the class.
  • It can be declared with any access specifier and the access specifier does not have any impact on the friend function.

Simple Friend Function Example Program for Tax Calculation

/*  Simple Friend Function Example Program In C++
    Class Examples, Understanding Friend Function In Class*/

// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

// Class Declaration

class Person {
   //Member Variable Declaration
   string name;
   int salary;
public:
   //Member Functions getSalary() and printDetails() Declaration

   void getSalary() {
      //Get Input Values For Object Variables
      cout << "Enter the Name :";
      cin >> name;

      cout << "Enter the Salary :";
      cin >> salary;
   }

   void printDetails() {
      //Show the Output
      cout << "\nName : " << name << ", Salary : " << salary << endl;
   }

   // Friend Function Declaration
   friend int calculateTax(Person person);
};

// Friend Function Definition

int calculateTax(Person person) {
   //Calculate Tax 5% and returns Tax Amount
   int tax = (person.salary / 100) * 5;
   return tax;
}

int main() {
   // Object Creation For Class
   Person obj;
   int tax = 0;
   cout << "Simple Friend Function Example Program In C++\n";

   obj.getSalary();
   obj.printDetails();

   // Call Friend Function like a normal function without Object Help
   tax = calculateTax(obj);
   cout << "Calculated Tax 5% : " << tax;
   getch();
   return 0;
}

Sample Output

Simple Friend Function Example Program In C++
Enter the Name :Morgan
Enter the Salary :12000

Name : Morgan, Salary : 12000
Calculated Tax 5% : 600