 Welcome to the session on friend function and friend class. At the end of this session, student will be able to write a C++ program using the concept of friend function or friend class. In this video, first we will recall the concept of a member function, its use, and then we will see the concept of friend function. Let's see what member function is. Members that are elements of a class are called member variables or data members. Any element of a class can be referred to as member of that class. One member function can call another member function or refer to a data member directly without using the dot operator. As we know, member function may access any element of a class of which it is a part. This includes all private elements. When a member function is called, an implicit argument is automatically passed which is a pointer to the invoking object. That is, pointer to the object on which the function is called. Let's see a program of member function in the code blocks ID. Here class name is rectangle which has two data members length and breadth of integer type. It has a parameterized constructor which assigns the values to length and breadth. It has a member function calc area which calculates and prints area of rectangle. S is an object of rectangle class. When S is created, memory for object S is allocated and a parameterized constructor is automatically called in this case which assigns 5 to length and 10 to breadth. This is a function called to calc area function. When the function is called, the control is passed to function definition and the area is calculated. Length is 5 and breadth is 10 so 5 into 10 is 50. Now let's see the output. Area is 50. This is displayed. As we know, one of the important features of object oriented programming is data hiding. That is, a non-member function cannot access an object's private or protected data. But sometimes this restriction may force programmer to write long and complex codes. So there is a mechanism in C++ to access private or protected data from non-member functions. This is done using a friend function or friend class. To declare a friend function, include its prototype within the class preceding it with the keyword friend as shown in the slide. The function is defined elsewhere in the program like a normal C++ function. The function definition doesn't use the keyword friend. A function can be declared as a friend in any number of classes. A friend function, although not a member, has full access rights to the private data members of the class. Friend functions have some special characteristics as it is not in the scope of the class to which it has been declared as friend. Since it is not in the scope of the class, it cannot be called using the object of that class. It can be invoked like a normal function without the help of any object. Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name. For example, a.x where a is object and x is a data member. It can be declared either in the public or private part of a class without affecting its meaning. Usually it has the objects as arguments. Let's see an example to call a member function and friend function. Member function is called through object. S is an object of rectangle class. Function calc area is called by statement s.calc area as this is a member function. Call to friend function requires an object to be passed by value or by reference as a parameter. The statement calc area in parenthesis s calls calc area function and object s is passed as a parameter. This is the difference to call member function and friend function. Let's see an example of friend function. Once again the class name is rectangle, data member's length and breadth, parameterized constructor and a friend function name is calc area. Here the object s is constructed and a parameterized constructor will be called automatically which assigns the values 5 and 10 to length and breadth respectively. Now notice the difference to call a friend function. Friend function is called without using the dot operator and it does not need to be qualified with the object name. So it is called as calc area and object s is passed as an argument. It is passed by value to the calc area function and here the area is calculated s dot length into s dot breadth. The object name dot data member name is used here and it calculates the area which is 5 into 10, it is 50 and it is printed. Let's see the output. The area is 50, although there is nothing gained by making calc area function a friend rather than a member function of rectangle class. There are some circumstances in which friend functions are quite valuable. Friend function can be useful when we are overloading certain types of operators. Friend functions make the creation of some types of IO functions easier. Friend functions may be desirable in some cases where two or more classes may contain data members that are interrelated relative to other parts of our program. Before going further pause the video for some time and find the output of the given code. Once you have done this you may resume the video. The answer is option D 295. When object E is constructed before constructor gets called, it initializes data members A and B with values 100 and 200 respectively. Compute is a friend function. The object E is passed to the friend function as a parameter, E dot A is 100 and E dot B is 200. So the addition of these two is 300, 300 minus 5 is 295. 295 is returned from compute function and it is displayed. So the output is 295. A member function of one class can be a friend function of another class. In such cases they are defined using scope resolution operator as shown here. Here function 1 is a member of class A and it is friend of class B. It is possible for one class to be a friend of another class. When this is the case, the friend class and all of its member functions have access to the private members defined within the another class. When this is the case the friend class and all of its member functions have access to the private members defined within the other class. Let's see an example of friend class in the code blocks ID. This is the example of friend class. Your class name is A and the friend class is class B. Class B has a function display where display function can access the data members of class A. Small A is the object of class A and small B is the object of class B. As display function is the member of class B, it is called on the object of class B. And object of class A is passed as an argument to the display function. It is passed by reference and display function can access the value of X which is the data member of class A. X is the data member of class A and it is printed in the function display of class B. Let's see the output. The output is value of X equal to 5. In this video we have seen the concept of friend function and friend class. Now you may write the programs using this concept. Thank you.