Friend Function - Part 2

Duration: 17 min

This video lesson is available to enrolled students.

Enroll to watch — NTA-UGC-NET Paper - 2

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This C++ lecture, titled 'Friend Function - Part 2', systematically covers the four categories of friend functions and classes using Circle and Rectangle examples. It begins by explaining Category 2, where a single non-member function acts as a bridge between two classes. The instructor emphasizes that forward declaration is required because the compiler must recognize class names before they are used in friend declarations. The function res_area() is declared as a friend of both Circle and Rectangle, allowing it to access their private area member (ar). A console demonstration shows inputs of radius 5 and dimensions 4 by 3, producing a combined area result of 21.85.

The lecture then transitions to Category 3, defining a friend class as one whose all member functions can access the private and protected members of another class. Key rules are highlighted: friendship is not mutual, not inherited, and not transitive. The code example declares 'friend class Rectangle;' inside the Circle class, enabling Rectangle's member functions to access Circle's private data. The instructor annotates the code with red arrows and boxes, pointing out the forward declaration 'class Rectangle;' and the calculation 'ar - c.ar'. The console output displays 'Rectangle area - Circle area = -60.50', verifying the access.

Finally, Category 4 is introduced: a member function of one class declared as a friend of another. The instructor circles the title words 'one class' and 'another class', showing how a specific member function (like res_area) in Rectangle can access Circle's private members without making the entire class a friend. The lesson concludes with a summary slide defining friend functions as non-member functions granted access to private and protected members, listing the four categories: simple friend function, bridge function, friend class, and member function as friend. The instructor uses handwritten annotations to clarify syntax and access rules throughout.

Chapters

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

    The lecture introduces Category 2, where a single friend function bridges two classes. On-screen text states 'res_area() is a single friend function of both classes' and explains that forward declaration is required because the compiler needs class names before use. The code editor shows 'friend float res_area(Rectangle, Circle);' inside both Circle and Rectangle classes. A console pane displays 'Enter radius: 5', 'Enter length and breadth: 4 3', and the result '21.85'. Red hand-drawn arrows mark the friend declarations, emphasizing how one function accesses private members (ar) of both classes.

  2. 2:00 5:00 02:00-05:00

    The instructor defines a friend class as 'a class whose all member functions are allowed to access the private and protected members' of another class. The syntax 'class A { friend class B; };' is shown with red circles around key terms. Four key points are listed: friendship is not mutual, not inherited, and not transitive. The instructor underlines these rules with red annotations and draws arrows connecting the syntax to explanations. A code example begins showing Circle and Rectangle classes with forward declaration 'class Rectangle;' and the friend class keyword inside Circle.

  3. 5:00 10:00 05:00-10:00

    The Category 3 code example is demonstrated with red boxes marking 'class Rectangle;' as forward declaration and 'friend class Rectangle;' inside Circle. The console shows inputs: radius 5, length 4, breadth 3, and output 'Rectangle area - Circle area = -60.50'. A handwritten red 'R' and arrow point to 'ar = l * b;' in Rectangle's cal_area(). The instructor annotates the formula 'ar - c.ar' to explain how Rectangle accesses Circle's private area member. Red arrows trace the access flow from friend declaration to member variable, verifying that the friend class successfully breaks encapsulation.

  4. 10:00 15:00 10:00-15:00

    The slide title switches to 'Category 4: Member function of one class is friend of the another class.' Red circles mark 'one class' and 'another class' in the title. The code editor shows Circle and Rectangle classes with get() and cal_area() members plus friend declarations. Checkmarks appear beside cal_area() and res_area(Circle c), indicating which functions are involved. The console displays 'Enter length: 4', 'Enter breadth: 3', and the result 'Rectangle area - Circle area = -60.50'. The instructor explains how a specific member function in Rectangle can access Circle's private data without making the entire class a friend.

  5. 15:00 16:48 15:00-16:48

    The lesson concludes with a summary slide defining 'A friend function is a non-member function that is granted access to the private and protected members of a class.' The syntax 'friend void functionName(ClassName)' is shown. Four categories are listed: simple friend function, friend function of all the classes (bridge), friend class, and member function as friend. The instructor annotates with handwritten notes on access levels and crosses out incorrect usage examples to clarify rules, providing a comprehensive overview of friend function categories in C++.

The lecture progresses logically through four categories of friend functions/classes in C++, using consistent Circle and Rectangle examples to build understanding. Category 2 (bridge function) demonstrates how a single non-member function can access private members of multiple classes, requiring forward declaration. Category 3 (friend class) shows how an entire class's member functions gain access to another class's private data, with important rules that friendship is neither mutual, inherited, nor transitive. Category 4 refines this by allowing only a specific member function to be granted friend status, providing more granular control over encapsulation. The instructor consistently uses red annotations to highlight key syntax elements, forward declarations, and access patterns. Console outputs verify each concept with concrete numerical results (21.85 for combined area, -60.50 for area difference). The teaching method combines definition slides with annotated code examples, ensuring students understand both the theoretical rules and practical implementation. The final summary slide consolidates all four categories, providing a reference framework for exam preparation.

Loading lesson…