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 memberspublic: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: 85Explanation:
Studentis a class containing a private data membermarks.display()is declared as a friend function using thefriendkeyword.The function is defined outside the class.
It accesses the private member
marksthrough the objects.The function is called like an ordinary function:
display(s1).
व्याख्या:
Studentएक class है जिसमेंmarksनाम का private data member है।display()कोfriendkeyword की सहायता से friend function declare किया गया है।इसकी definition class के बाहर लिखी गई है।
यह
sobject के माध्यम से private membermarksको 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: 10Protected Member: 20
Explanation:
ais a private data member.bis a protected data member.display()is declared as a friend of theDemoclass.Therefore, it can access both
aandb.
व्याख्या:
aएक private data member है।bएक protected data member है।display()कोDemoclass का 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: 30Explanation:
ClassAandClassBare two different classes.The function
add()is declared as a friend in both classes.It can access the private members
aandb.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 की कुछ विशेषताएँ होती हैं।
A friend function is not a member of the class.
Friend Function class का member नहीं होता।
It is declared inside the class using the
friendkeyword.इसे class के अंदर
friendkeyword की सहायता से declare किया जाता है।It is defined outside the class like an ordinary function.
इसकी definition class के बाहर एक सामान्य function की तरह लिखी जाती है।
It can access private and protected members of the class that declares it as a friend.
यह उस class के private और protected members को access कर सकता है जिसने इसे friend declare किया है।
It is called like an ordinary function, not through an object using the dot operator.
इसे dot operator की सहायता से object के माध्यम से नहीं, बल्कि एक सामान्य function की तरह call किया जाता है।
It does not have a
thispointer because it is not a member function.इसमें
thispointer नहीं होता क्योंकि यह member function नहीं है।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 उपयोगी होते हैं।
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 कर सकता है।
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 करती हैं।
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 चाहिए।
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
| Basis | Friend Function | Member Function |
|---|---|---|
| Meaning | Non-member function granted special access | Function belonging to a class |
| Declaration | Declared using friend inside a class | Declared as a class member |
| Definition | Usually defined outside the class | Can be defined inside or outside the class |
| Calling | Called like an ordinary function | Called through an object or class, depending on the function |
this pointer | Not available | Available in non-static member functions |
| Private data access | Can access it through an object when declared as a friend | Can 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
Post a Comment