Friend Function in C++ - Definition, Syntax and Examples

1. Introduction to Friend Function in C++

A friend function in C++ is a non-member function that is granted permission to access the private and protected members of a class. It is declared inside the class using the friend keyword, but it is defined outside the class like an ordinary function. A friend function is not a member of the class, even though it can access its private and protected members.

C++ में Friend Function एक non-member function होता है जिसे किसी class के private और protected members को access करने की permission दी जाती है। इसे class के अंदर friend keyword की सहायता से declare किया जाता है, लेकिन इसकी definition class के बाहर एक normal function की तरह लिखी जाती है। Friend Function class का member नहीं होता, फिर भी यह उसके private और protected members को access कर सकता है।

2. Syntax of Friend Function in C++

The friend keyword is used to declare a friend function inside a class.

C++ में Friend Function declare करने के लिए class के अंदर friend keyword का उपयोग किया जाता है।

Syntax

class ClassName
{
private:
// Private data members
public:
friend return_type functionName(parameters);
};

The function is defined outside the class without using the scope resolution operator (::).

Function की definition class के बाहर लिखी जाती है और इसमें Scope Resolution Operator (::) का उपयोग नहीं किया जाता।

return_type functionName(parameters)
{
// Function Body
}

3. Example of Friend Function in C++

The following program demonstrates how a friend function can access the private data member of a class.

निम्न program demonstrate करता है कि Friend Function किस प्रकार किसी class के private data member को access कर सकता है।

C++ Program

#include <iostream>
using namespace std;
class Student
{
private:
int marks;
public:
Student()
{
marks = 85;
}
friend void display(Student s);
};
void display(Student s)
{
cout << "Marks: " << s.marks;
}
int main()
{
Student s1;
display(s1);
return 0;
}

Output:

Marks: 85

Explanation:

  • Student is a class containing a private data member marks.

  • display() is declared as a friend function using the friend keyword.

  • The function is defined outside the class.

  • It accesses the private member marks through the object s.

  • The function is called like an ordinary function: display(s1).

व्याख्या:

  • Student एक class है जिसमें marks नाम का private data member है।

  • display() को friend keyword की सहायता से friend function declare किया गया है।

  • इसकी definition class के बाहर लिखी गई है।

  • यह s object के माध्यम से private member marks को access करता है।

  • इसे एक normal function की तरह display(s1) call किया गया है।

4. Friend Function Accessing Private and Protected Members

A friend function can access both private and protected members of the class that declares it as a friend.

Friend Function उस class के private और protected दोनों प्रकार के members को access कर सकता है जिसने उसे friend declare किया है।

Example: Accessing Private and Protected Members

#include <iostream>
using namespace std;
class Demo
{
private:
int a = 10;
protected:
int b = 20;
public:
friend void display(Demo d);
};
void display(Demo d)
{
cout << "Private Member: " << d.a << endl;
cout << "Protected Member: " << d.b;
}
int main()
{
Demo obj;
display(obj);
return 0;
}

Output:

Private Member: 10
Protected Member: 20

Explanation:

  • a is a private data member.

  • b is a protected data member.

  • display() is declared as a friend of the Demo class.

  • Therefore, it can access both a and b.

व्याख्या:

  • a एक private data member है।

  • b एक protected data member है।

  • display() को Demo class का friend declare किया गया है।

  • इसलिए यह a और b दोनों को access कर सकता है।

5. Friend Function of Two Classes in C++

A single friend function can be declared as a friend in two or more classes. In that case, it can access the private and protected members of all those classes.

एक ही Friend Function को दो या अधिक classes में friend declare किया जा सकता है। ऐसी स्थिति में वह उन सभी classes के private और protected members को access कर सकता है।

Example: Friend Function of Two Classes

#include <iostream>
using namespace std;
class ClassB;
class ClassA
{
private:
int a = 10;
public:
friend void add(ClassA, ClassB);
};
class ClassB
{
private:
int b = 20;
public:
friend void add(ClassA, ClassB);
};
void add(ClassA obj1, ClassB obj2)
{
cout << "Sum: " << obj1.a + obj2.b;
}
int main()
{
ClassA x;
ClassB y;
add(x, y);
return 0;
}

Output:

Sum: 30

Explanation:

  • ClassA and ClassB are two different classes.

  • The function add() is declared as a friend in both classes.

  • It can access the private members a and b.

  • The sum of both values is displayed.

व्याख्या:

  • ClassA और ClassB दो अलग-अलग classes हैं।

  • add() function को दोनों classes में friend declare किया गया है।

  • यह दोनों classes के private members a और b को access कर सकता है।

  • दोनों values का sum display किया जाता है।

Note: The forward declaration class ClassB; is required here because ClassB is mentioned in the friend function declaration inside ClassA before its full definition.

ध्यान दें: यहाँ class ClassB; की forward declaration आवश्यक है क्योंकि ClassA के अंदर friend function declaration में ClassB का उपयोग उसकी पूरी definition से पहले किया गया है।

6. Characteristics of Friend Function in C++

A friend function has some special characteristics in C++.

C++ में Friend Function की कुछ विशेषताएँ होती हैं।

  1. A friend function is not a member of the class.

    Friend Function class का member नहीं होता।

  2. It is declared inside the class using the friend keyword.

    इसे class के अंदर friend keyword की सहायता से declare किया जाता है।

  3. It is defined outside the class like an ordinary function.

    इसकी definition class के बाहर एक सामान्य function की तरह लिखी जाती है।

  4. It can access private and protected members of the class that declares it as a friend.

    यह उस class के private और protected members को access कर सकता है जिसने इसे friend declare किया है।

  5. It is called like an ordinary function, not through an object using the dot operator.

    इसे dot operator की सहायता से object के माध्यम से नहीं, बल्कि एक सामान्य function की तरह call किया जाता है।

  6. It does not have a this pointer because it is not a member function.

    इसमें this pointer नहीं होता क्योंकि यह member function नहीं है।

  7. Friendship is not inherited, reciprocal or transitive.

    Friendship inherited, reciprocal या transitive नहीं होती।

7. Advantages of Friend Functions in C++

Friend functions are useful in certain situations where a non-member function needs access to the internal data of a class.

कुछ situations में जब किसी non-member function को class के internal data तक access की आवश्यकता होती है, तब Friend Functions उपयोगी होते हैं।

  1. Access to Private Data: A friend function can access private and protected members when permission is granted.

    Private Data तक Access: Permission मिलने पर Friend Function private और protected members को access कर सकता है।

  2. Working with Multiple Classes: A single friend function can access members of multiple classes that declare it as a friend.

    Multiple Classes के साथ कार्य: एक Friend Function उन multiple classes के members को access कर सकता है जो उसे friend declare करती हैं।

  3. Operator Overloading: Friend functions are useful for implementing certain operator overloads, such as binary operators that need access to private data.

    Operator Overloading: Friend Functions कुछ operator overloading operations में उपयोगी होते हैं, जैसे ऐसे binary operators जिन्हें private data तक access चाहिए।

  4. Flexible Function Design: They allow selected non-member functions to work with class data without making those functions class members.

    Flexible Function Design: ये selected non-member functions को class member बनाए बिना class data के साथ कार्य करने की सुविधा देते हैं।

8. Difference Between Friend Function and Member Function

BasisFriend FunctionMember Function
MeaningNon-member function granted special accessFunction belonging to a class
DeclarationDeclared using friend inside a classDeclared as a class member
DefinitionUsually defined outside the classCan be defined inside or outside the class
CallingCalled like an ordinary functionCalled through an object or class, depending on the function
this pointerNot availableAvailable in non-static member functions
Private data accessCan access it through an object when declared as a friendCan access the class's private data directly

A Friend Function is a special feature of C++ that allows a non-member function to access the private and protected members of a class. It is declared inside the class using the friend keyword and defined outside the class. Friend functions are useful in selected situations, especially when working with multiple classes and operator overloading.

C++ में Friend Function एक विशेष feature है जो किसी non-member function को class के private और protected members access करने की permission देता है। इसे class के अंदर friend keyword की सहायता से declare किया जाता है और class के बाहर define किया जाता है। Friend Functions कुछ विशेष situations में उपयोगी होते हैं, विशेष रूप से multiple classes और operator overloading के साथ कार्य करते समय।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)