Friend Function In C++ : Check Here To Know

Safalta Expert Published by: Saksham Chauhan Updated Wed, 12 Jul 2023 05:39 PM IST

Highlights

Despite being defined outside of the class, a friend function has access to all of the private and protected members. 

A friend function, despite being defined outside of the class, has access to all private and protected members. Friends are not member functions, despite the fact that prototypes for them can be found in the class definition. In C++, a buddy function is a one-of-a-kind function that, although not being a member of a class, has access to its private and protected data. A friend function is a non-member or ordinary function of a class that has been marked as a friend by using the word "friend" within the class. When a function is declared as a friend, it gains all access permissions. The term "friend" appears only in the function declaration of the friend function, not in the definition.When the friend function is called, neither the object's name nor the dot operator are used. Friends should only be utilised for particular purposes. When too many functions or external classes are defined as friends of a class with protected or private data, the benefit of encapsulating various classes in object-oriented programming is diminished. Friendships are not mutual. Class B does not become a buddy of class A simply because class A is. The object whose value is sought may be accepted as an argument. Download these FREE Ebooks:

Source: Safalta.com


1. Introduction to Digital Marketing
2. Website Planning and Creation

A friend class has access to secret and protected members of the class to which it has been designated as a friend. Allowing a specific class to access private members of another class is occasionally beneficial. For example, a LinkedList class may be granted access to Node's secret members. A friend class has access to both private and protected members of the class that has declared it as a friend.

Free Demo Classes

Register here for Free Demo Classes



Place the following statement in the definition of class ClassOne to declare all member functions of class ClassTwo as friends of class ClassOne:

friend class ClassTwo;
class buddy ClassTwo;




The outcome of compiling and running the aforementioned code is as follows:
Width of box : 10

A straightforward and comprehensive C++ application that shows how friends are used in another class


int main()
{
    A a;
    B x;
    a.showB(x);
    return 0;
}

Output

B::b = 0
You can also check

 Introduction to MS Excel- Basics, Features, Benefits, Sample Questions Basic Opps in C++ Interview Questions for Freshers OPPS Concepts in C++
 

Related Article

RRB ALP Admit Card: 25 नवंबर की एएलपी भर्ती परीक्षा के लिए जारी हुआ प्रवेश पत्र, जानें डाउनलोड करने का तरीका

Read More

CHSE Odisha Class 12 date sheet 2025 out now; Check the exam schedule here

Read More

CBSE Date Sheet 2025: सीबीएसई बोर्ड कक्षा 10वीं 12वीं की डेटशीट हुई जारी, यहां देखें पूरा शेड्यूल

Read More

CBSE Date Sheet 2025: Class 10, 12 timetable at cbse.gov.in awaited, Check the latest update here

Read More

CBSE Board Exam 2025: सीबीएसई बोर्ड 10वीं- 12वीं के लिए जल्द जारी होगी डेटशीट, पिछले साल इस दिन हुई घोषित

Read More

UP Board Exam 2025: यूपी बोर्ड कक्षा 10वीं-12वीं के लिए घोषित हुईं परीक्षा तिथियां, यहां देखें पूरा शेड्यूल

Read More

UP Board Datesheet 2025: Class 10, 12 Exams from 24 February, Check the full schedule here

Read More

UP Board Eams 2025: यूपी बोर्ड परीक्षा की संभावित तिथि जारी, महाकुंभ की वजह से देरी से शुरू होंगे एग्जाम्स

Read More

JEE Advanced 2025: जेईई एडवांस्ड के लिए पात्रता मानदंडों में एक बार फिर हुआ बदलाव, प्रयासों की संख्या घटाई गई

Read More