C++ Friend Function

Duration: 2 min

This video lesson is available to enrolled students.

Enroll to watch — UPPSC Polytechnic Lecturer 2025 (CS)

AI Summary

An AI-generated summary of this video lecture.

The video is a lecture on C++ Friend Functions, presented as a slide deck. It begins by defining a friend function as a function defined outside a class that has access to its private and protected members, clarifying that friends are not member functions. The lecture then explains the syntax for declaring a friend function, which involves using the 'friend' keyword before the function prototype within the class definition. An example is provided using a 'Box' class with 'printWidth' and 'setWidth' functions declared as friends. The presentation further explains that a friend can be a function, function template, member function, or a class, and that declaring a class as a friend makes all its member functions friends. The final segment demonstrates how to declare a function or class as a friend of another class, using 'class Gadget' and 'class Widget' as an example, where 'myFunc' is declared as a friend of 'Widget' and 'class Gadget' is declared as a friend of 'Widget'.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video starts with a slide titled 'C++ Friend Function'. The instructor explains that a friend function is defined outside a class but has the right to access all private and protected members of the class. It is emphasized that even though the prototypes for friend functions appear in the class definition, friends are not member functions. The slide then details that a friend can be a function, function template, member function, or a class, and that if a class is a friend, all its members are friends. The section 'Declaring Friend Function' explains the syntax: to declare a function as a friend, precede its prototype in the class definition with the keyword 'friend'. An example code snippet for a 'class Box' is shown, with 'friend void printWidth(Box box);' and 'friend void setWidth(double wid);' as the friend function declarations.

  2. 2:00 2:22 02:00-02:22

    The video transitions to a new slide titled 'Friend Function/Class in C++'. The instructor explains that normally, only a class has access to its private members, but sometimes it's necessary to allow another class or function to access them. To achieve this, the 'friend' statement is used. The slide provides a code example with two classes, 'Gadget' and 'Widget'. It shows a forward declaration of 'class Gadget;'. Inside the 'class Widget' definition, it declares 'friend void myFunc();' and 'friend class Gadget;'. The instructor explains that this makes the function 'myFunc' and the entire 'Gadget' class a friend of 'Widget', allowing them to access 'Widget's private members.

The lecture systematically builds the concept of C++ friend functions. It starts with a clear definition, distinguishing them from member functions, and then provides the syntax for declaration. The progression moves from a simple function friend to the more powerful concept of a class friend, using code examples to illustrate how the 'friend' keyword grants access to private and protected members. The final example demonstrates the practical application of declaring both a function and a class as friends, reinforcing the core idea that friendship is a way to break encapsulation selectively.