Friend Function in C++

A friend function in C++ is a special function which can access private and protected members of a class even though it is not a member function of the class.

It is declared as a friend using the ‘friend’ keyword inside the class. By declaring the function as a friend friend in C++ all the permission can be granted to the function. ‘friend’ keyword is placed only inside the class when the function is declared as a friend function, not in the function definition.

We don’t have to use the name of the object or dot operator during the calling of the function, a friend function in C++ can accept the object of the class where the function is declared as a friend function in C++ as an argument. Friend function in C++ can be declared in any section of the class i.e. public or private or protected.

Syntax of friend function in C++:

class     
{    
    friend    (argument/s_type);  
};

Characteristics of Friend Function in C++:

  • The function is not in the scope of the class to which it has been declared as a friend.
  • It cannot be called using the object as it is not in the scope of that class.
  • It can be invoked like a normal function without using the object.
  • It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
  • It can be declared either in the private or the public part of the class.

Types of Friend Function

A friend function in C++ can be of two types:

Global function as friend function in C++

Global Functions in C++ are the functions that are declared outside of any class, function or namespace. Global Functions are also called free functions.
Examples of Global Function in C++ as a friend function in C++ :

  1. Global functions as a friend function in C++ example to only one class to find the product of two numbers.

    #include<iostream>
    using namespace std;
    class Multiplication
    {
        int a,b;
        public:
       	 void set_data()
    {
        cout<<"Enter the First No:";
        cin>>a;
        cout<<"Enter the Second No:";
        cin>>b;
    }
    // declaration of Global function as a friend function in C++
       	 friend void multiply(Multiplication);    
    };
    // defining Global function (friend function for Multiplication class)
    void multiply(Multiplication t)
    {
        int c=t.a * t.b;
        cout<<”Product ”<<c<<endl;
    }
    int main()
    {
    //instantiation of Multiplication class
        Multiplication l;
        l.set_data();
    // calling of friend function in C++
        multiply(l);
        return 0;
    }
    

    OUTPUT

     Enter the First No:5
     Enter the Second No:2
     Product 10
  2. Global functions as a friend function in C++ to more than one class used to print the full name.

    #include<bits/stdc++.h>
    using namespace std;
    // forward declaration of LastName class
    class LastName;
    class FirstName
    {
        string  first_name;
        public:
    	//Constructor
       	FirstName(string first){
    	        first_name=first;
    }
       	 //declaring friend function in C++
       	 friend void fullName(FirstName,LastName);
    };
    
    class LastName
    {
       string last_name;
        public:
    	// Constructor
       	 LastName(string last){
    	     last_name=last;
    }   
        // declaring friend function in C++
        friend void fullName(FirstName,LastName);
    };
    
    //defining friend function in C++
    void fullName (FirstName first,LastName last)
    {
        string full=first.first_name + “  “ + last.last_name;
       cout<<full<<endl;
    }
    
    main()
    {
    //instantiation of FirstName class
        FirstName a(“Aditya”);
    //instantiation of LastName class
        LastName b(“Rastogi”);
     //calling of friend function in C++
        fullName(a,b);
        return 0;
    }
    

    OUTPUT

    Aditya Rastogi

Member function of another class

Member functions are the functions declared as a member of class. These functions can access private and protected member of the class in which these are declared as member. These functions do not include functions declared with friend specifiers.

Examples of Member Function in C++ as a friend function in C++ used to print the full name.

#include <bits/stdc++.h>
using namespace std;
class Last;
class First {
string a="Aditya";
public:
	void full(Last);
};

class Last {
private:
	string b="Rastogi";
Public:
//declaring a Member Function as a Friend Function in C++
	friend void First::full(Last x); // Friend function
};
//calling of friend function in C++
void First::full(Last y){
	cout<<this->a<<" "<<y.b;
}

int main()
{
	First x;
	Last y;
	x.full(y);
	return 0;
}

OUTPUT

Aditya Rastogi

Important points related friend functions in C++:

  1. A class should not have many functions or classes as friend, as it will reduce the encapsulation value of that class.
  2. Friend classes are not mutual, meaning if class A is declared friend class of class B then B will not be the friend of class A.
  3. Friendship is not inherited, meaning if a base class has a friend function, then the function doesn’t become a friend of the derived class(es).

Advantages and Disadvantages of friend functions in C++:

Advantages:

  • A friend function is able to access members without the need of inheriting the class.
  • Friend function acts as a bridge between two classes by accessing their private data.
  • It can be used to increase the versatility of an overloaded operator.
  • It can be declared either in the public or private or protected part of class.

Disadvantages:

  • Friend functions have access to private members of a class from outside the class which violates the law of the data hiding.
  • Friend functions cannot do any run time polymorphism in its members.

Summary
A friend function in C++ are the function which can access private and protected members of a class without being the member function of that class.
Friend Function in C++ can be declared anywhere in the class i.e. public, private or protected sections.

Friend Function in C++ are of two types:

  1. Global Functions as a Friend Functions
  2. Member function of another class

Leave a Reply

Your email address will not be published. Required fields are marked *