Friend Function in C++As we already know that in an object-oriented programming language, the members of the class can only access the data and functions of the class but outside functions cannot access the data of the class. There might be situation that the outside functions need to access the private members of the class so in that case, friend function comes into the picture. What is a friend function?A friend function is a function of the class defined outside the class scope but it has the right to access all the private and protected members of the class. The friend functions appear in the class definition but friends are the member functions. Characteristics of a Friend function:
Why do we need a friend function in C++?
Characteristics of a Friend function
Syntax for the declaration of a friend function. In the above declaration, the friend function is preceded by the keyword friend. The function can be defined anywhere in the program like a normal C++ function. The function definition does not use either the keyword friend or scope resolution operator. Let's understand the friend function through an example. In the above code, Distance is the class that contains private field named as 'meters'. The Distance() is the constructor method that initializes the 'meters' value with 0. The display_data() is a method that displays the 'meters' value. The addvalue() is a friend function of Distance class that modifies the value of 'meters'. Inside the main() method, d1 is an object of a Distance class. Output Friend function can also be useful when we are working on objects of two different classes. Let's understand through an example. In the above code, we have defined two classes named as ClassA and ClassB. Both these classes contain the friend function 'multiply()'. The friend function can access the data members of both the classes, i.e., ClassA and ClassB. The multiply() function accesses the num1 and num2 of ClassA and ClassB respectively. In the above code, we have created object1 and object2 of ClassA and ClassB respectively. The multiply() function multiplies the num1 and num2 and returns the result. As we can observe in the above code that the friend function in ClassA is also using ClassB without prior declaration of ClassB. So, in this case, we need to provide the forward declaration of ClassB. Output Friend class in C++We can also create a friend class with the help of friend keyword. In the above declaration, Class1 is declared as a friend class of Class2. All the members of Class2 can be accessed in Class1. Let's understand through an example. In the above code, we have created two classes named as ClassA and ClassB. Since ClassA is declared as friend of ClassB, so ClassA can access all the data members of ClassB. In ClassB, we have defined a function add() that returns the sum of num1 and num2. Since ClassB is declared as friend of ClassA, so we can create the objects of ClassA in ClassB. Output Next TopicSnake Code in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India