Friend functions of the class are granted access to private and protected members of the class in C++. They are defined outside the class’ scope. Friend functions are not member functions of the class.
By using the keyword, ‘friend’ compiler knows that the given function is a friend function.
We declare friend function inside the body of a class, starting with the keyword friend to access the data. We use them when we need to operate between two different classes at the same time.
Declaration of a friend function in C++:
class class_name { friend data_type function_name(arguments/s); //syntax of friend function. };
In the above declaration, the keyword friend precedes the function. We can define the friend function anywhere in the program like a normal C++ function. A class’s function definition does not use either the keyword friend or scope resolution operator (: 🙂.
Friend function is called as function_name(class_name) and member function is called as class_name. function_name.
Characteristics of Friend Function in C++
- The function is not in the ‘scope’ of the class to which it has been declared a friend.
- It cannot be invoked using the object as it is not in the scope of that class.
- We can invoke it like any normal function of the class.
- Friend functions have objects as arguments.
- It cannot access the member names directly and has to use dot membership operator and use an object name with the member name.
- We can declare it either in the ‘public’ or the ‘private’ part.
Implementing friend functions can be done in two ways:
A method of another class:
We declare a friend class when we want to access the non-public data members of a particular class.
A Global function:
A ‘global friend function’ allows you to access all the private and protected members of the global class declaration.
A simple example of a C++ friend function used to print the length of the box.
Code:
#include <iostream> using namespace std; class Box { private: int length; public: Box (): length (0) {} friend int printLength (Box); //friend function }; int printLength (Box b) { b. length +=10; return b. length; } int main () { Box b; cout <<” Length of box:” <<printLength (b)<<endl; return 0; }
Output:
Length of box:10
Simple example when the function is friendly for two classes.
Code:
#include<iostream> using namespace std; class B; //forward declaration. class A { int x; public: void setdata (int i) { x=i; } friend void max (A, B); //friend function. } ; class B { int y; public: void setdata (int i) { y=i; } friend void max (A, B); }; void max (A a, B b) { if (a.x >= b.y) std:: cout<< a.x << std::endl; else std::cout<< b.y << std::endl; } int main () { A a; B b; a. setdata (10); b. setdata (20); max (a, b); return 0; }
Output:
20
In the above example, max () function is friendly to both class A and B, i.e., the max () function can access the private members of two classes.
Implementing through a method of another class:
A class cannot access the private members of another class. Similarly, a class cannot access its protected members of a class. We need a friend class in this case.
A friend class is used when we need to access private and protected members of the class in which it has been declared as a friend. It is also possible to declare only one member function of another class to be a friend.
Declaration of friend class:
class class_name { friend class friend_class;// declaring friend class }; class friend_class { };
All functions in friend_class are friend functions of class_name.
A simple example of a friend class:
Code:
#include <iostream> using namespace std; class A { int x=4; friend class B; //friend class }; class B { public: void display (A &a) { cout<<”value of x is:” <<a.x; } }; int main () { A a; B b; b. display (a); return 0; }
Output:
value of x is:4
Implementing a global function:
Code:
#include<iostream> using namespace std; class space { int x; int y; int z; public: void setdata (int a, int b, int c); void display(void); friend void operator- (space &s); }; void space ::setdata (int a, int b, int c) { x=a; y=b; z=c; } void space::display(void) { cout<<x<<" "<<y<<" "<<z<<"\n"; } void operator- (space &s) { s.x =- s.x; s.y =- s.y; s.z =- s.z; } int main () { space s; s. setdata (5,2,9); cout<<"s:"; s. display (); -s; cout<<"-s:"; s. display (); return 0; }
Output:
s: 5 2 9
-s: -5 -2 -9
In the above example operator- is the friend function globally declared at the scope of the class.
Friend Class in C++:
- If class A is a friend of class B, then class B is not a friend of class A.
- Also, if class A is a friend of class B, and then class B is a friend of class C, class A is not a friend of class C.
- If Base class is a friend of class X, subclass Derived is not a friend of class X; and if class X is a friend of class Base, class X is not a friend of subclass Derived.
Uses of friend function:
- You can use a friend function to bridge two classes by operating objects of two different classes.
- It increases the versatility of overloading operators.
- It enhances encapsulation. Only the programmer who has access to the class’s source code can make a function friend to that class.
- You may declare a member function of a class as a friend of another class.
- It works symmetrically with all its friends.
Summary:
- Even though the prototypes for friend functions appear in the class definition, friends are not members functions.
- We can do friend declarations anywhere in a class definition, i.e. either in public, private or protected sections.
- Violates the data hiding principle of classes, so we should avoid it as much as possible.
- You can grant friendship but not take it, i.e., for class B to be a friend of class A, class A must explicitly declare that class B is its friend.
- The friendship relation is neither symmetric nor transitive.
This brings us to the end of the blog on Friend functions in C++. Hope this helps you to up-skill your C++ skills. To learn more about programming and other related concepts, check out the courses on Great Learning Academy.
Also, if you are preparing for Interviews, check out these Interview Questions for C++ to ace it like a pro.
0