Friend Function & Friend Class in C++ Notes | Friend Keyword, Friend Function, Friend Class & Advantages | Basic Computer Engineering | RGPV BTech First Year

Friend Function & Friend Class in C++ Notes | Friend Keyword, Friend Function, Friend Class & Advantages | Basic Computer Engineering | RGPV BTech First Year


Friend Function & Friend Class in C++

Friend Function C++ का एक विशेष Feature है जो किसी Class का Member Function न होने के बावजूद उस Class के Private तथा Protected Data Members को Access करने की अनुमति देता है। सामान्यतः Private Members केवल उसी Class के Member Functions द्वारा Access किए जा सकते हैं, लेकिन Friend Function इस नियम का अपवाद (Exception) है।

इसी प्रकार Friend Class वह Class होती है जिसे किसी अन्य Class के Private एवं Protected Members को Access करने की अनुमति प्रदान की जाती है। Friend Function तथा Friend Class का उपयोग विशेष परिस्थितियों में किया जाता है जहाँ दो Classes के बीच Data Sharing आवश्यक होती है।

RGPV B.Tech First Year के Basic Computer Engineering Subject में Friend Function & Friend Class अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Friend Keyword, Friend Function, Friend Class तथा इनके Applications से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।

Class



friend Keyword



Friend Function / Friend Class



Private Members Access

Introduction to Friend Function

Object Oriented Programming में Data Hiding एक महत्वपूर्ण सिद्धांत है जिसके अनुसार Private Data केवल उसी Class के भीतर Access किया जा सकता है। लेकिन कुछ परिस्थितियों में किसी बाहरी Function अथवा दूसरी Class को भी Private Data Access करने की आवश्यकता होती है। ऐसी स्थिति में Friend Function तथा Friend Class का उपयोग किया जाता है।

Friend Function Member Function नहीं होती, फिर भी इसे Class के Private तथा Protected Members तक पहुँचने का अधिकार प्राप्त होता है।


What is Friend Function?

Friend Function ऐसा Function होता है जिसे किसी Class के अंदर friend Keyword द्वारा Declare किया जाता है। यह Function Class का Member नहीं होता, लेकिन Class के सभी Private तथा Protected Members को Access कर सकता है।

friend void display();

ऊपर दिया गया Statement एक Friend Function Declaration को दर्शाता है।


Need of Friend Function

कुछ Programs में दो या अधिक Classes के बीच Data Share करना आवश्यक होता है। यदि Member Functions द्वारा यह कार्य कठिन हो, तो Friend Function सबसे उपयुक्त समाधान प्रदान करती है।

  • Private Data को नियंत्रित तरीके से Access करने के लिए।
  • दो या अधिक Classes के बीच Data Sharing करने के लिए।
  • Operator Overloading में सहायता प्राप्त करने के लिए।
  • Complex Mathematical Operations को सरल बनाने के लिए।
  • Code Reusability बढ़ाने के लिए।
  • Specific External Functions को Access Permission देने के लिए।
  • Professional Object Oriented Software Development के लिए।

Advantages of Friend Function

Advantage Description
Private Data Access Private तथा Protected Members को Access कर सकती है।
Data Sharing दो Classes के बीच Data Exchange आसान बनाती है।
Operator Overloading Complex Operators को Overload करने में उपयोगी।
Flexibility विशेष परिस्थितियों में External Function को Access प्रदान करती है।
Easy Programming Complex Object Interactions को सरल बनाती है।

Characteristics of Friend Function

Friend Function C++ का एक विशेष प्रकार का Function होता है जो किसी Class का Member Function नहीं होता, फिर भी वह उस Class के Private तथा Protected Members को Access कर सकता है। इसकी कुछ महत्वपूर्ण विशेषताएँ निम्नलिखित हैं।

  • Friend Function Class का Member Function नहीं होता।
  • इसे friend Keyword द्वारा Declare किया जाता है।
  • यह Private तथा Protected Data Members को Access कर सकता है।
  • इसे Object के माध्यम से Call नहीं किया जाता।
  • यह एक सामान्य (Normal) Function की तरह Call किया जाता है।
  • एक Friend Function एक से अधिक Classes का Friend हो सकता है।
  • Friendship Inherited नहीं होती।

Syntax of Friend Function

Friend Function को Class के अंदर friend Keyword की सहायता से Declare किया जाता है, जबकि इसकी Definition Class के बाहर लिखी जाती है।

class Demo { friend void show(); };

ऊपर दिए गए उदाहरण में show() एक Friend Function है।


Example of Friend Function

#include<iostream> using namespace std; class Demo { private: int x; public: Demo() { x = 100; } friend void display(Demo); }; void display(Demo d) { cout << d.x; } int main() { Demo obj; display(obj); return 0; }

Output

100

ऊपर दिए गए Program में display() Class का Member नहीं है, फिर भी वह x नामक Private Data Member को Access कर रहा है क्योंकि उसे Friend Function घोषित किया गया है।


Working of Friend Function

Class



friend Keyword



External Function



Private Data Access

Friend Function को Class के भीतर Access Permission प्रदान की जाती है। इसके बाद वह सामान्य Function की तरह कार्य करता है, लेकिन Class के Private एवं Protected Members तक भी पहुँच सकता है।


Rules of Friend Function

  • Friend Function Class का Member Function नहीं होता।
  • इसे Class के Public, Private या Protected किसी भी Section में Declare किया जा सकता है।
  • Friend Function को Object के माध्यम से Call नहीं किया जाता।
  • यह सीधे Class के Private तथा Protected Members को Access कर सकता है।
  • Friend Function में this Pointer उपलब्ध नहीं होता क्योंकि यह Member Function नहीं है।
  • Friendship एकतरफा (Not Mutual) होती है।
  • Friendship Inherited नहीं होती तथा Automatically Transfer भी नहीं होती।

Advantages of Friend Function

  • Private Data तक Controlled Access प्रदान करता है।
  • दो या अधिक Classes के बीच Data Sharing को सरल बनाता है।
  • Complex Operator Overloading में उपयोगी होता है।
  • Program की Flexibility बढ़ाता है।
  • Specific External Functions को Class के Data तक पहुँचने की अनुमति देता है।
  • Complex Mathematical एवं Scientific Applications में उपयोगी है।

Friend Class

Friend Class ऐसी Class होती है जिसे किसी अन्य Class के Private तथा Protected Members को Access करने की अनुमति प्रदान की जाती है। जब किसी Class को friend घोषित किया जाता है, तब उस Friend Class के सभी Member Functions दूसरी Class के Private Data तक पहुँच सकते हैं।

Friend Class का उपयोग तब किया जाता है जब दो Classes के बीच घनिष्ठ (Closely Related) संबंध हो तथा उन्हें आपस में Data Share करना आवश्यक हो।


Syntax of Friend Class

class B; class A { friend class B; };

ऊपर दिए गए उदाहरण में B को A का Friend Class घोषित किया गया है। अब B के सभी Member Functions, A के Private तथा Protected Members को Access कर सकते हैं।


Example of Friend Class

#include<iostream> using namespace std; class A { private: int value; public: A() { value = 500; } friend class B; }; class B { public: void display(A obj) { cout << obj.value; } }; int main() { A a; B b; b.display(a); return 0; }

Output

500

इस Program में B, A की Friend Class है। इसलिए B का display() Function, A के Private Member value को सीधे Access कर सकता है।


Friend Function vs Member Function

Friend Function Member Function
Class का Member नहीं होता। Class का Member होता है।
friend Keyword द्वारा Declare किया जाता है। सामान्य Member Function की तरह Declare किया जाता है।
Object के माध्यम से Call नहीं किया जाता। Object द्वारा Call किया जाता है।
this Pointer उपलब्ध नहीं होता। this Pointer उपलब्ध होता है।
Private Members को Friend Permission मिलने पर Access कर सकता है। हमेशा अपने Class के Members को Access कर सकता है।

Friend Function vs Friend Class

Friend Function Friend Class
केवल एक Function को Access Permission मिलती है। पूरी Class को Access Permission मिलती है।
Specific Task के लिए उपयोगी। Multiple Related Functions के लिए उपयोगी।
Memory Overhead कम होता है। पूरी Class को Access मिलने से Scope अधिक होता है।
छोटे Programs में अधिक उपयोगी। Large Object Oriented Applications में अधिक उपयोगी।

Working of Friend Class

Class A



friend class B



B Member Functions



Private Data Access

Friend Class घोषित होने के बाद Friend Class के सभी Member Functions संबंधित Class के Private तथा Protected Data को Access कर सकते हैं। यही Friend Class की मुख्य विशेषता है।


Rules of Friend Class

Friend Class का उपयोग करते समय C++ के कुछ महत्वपूर्ण Rules का पालन करना आवश्यक होता है। ये Rules Friend Relationship तथा Data Access को नियंत्रित करते हैं।

  • Friend Class को friend Keyword द्वारा Declare किया जाता है।
  • Friend Class दूसरी Class के Private तथा Protected Members को Access कर सकती है।
  • Friendship एकतरफा (One Way) होती है।
  • Friendship Automatic नहीं होती, इसे Explicitly Declare करना पड़ता है।
  • Friendship Inherited नहीं होती।
  • Friend Class स्वयं Member Class नहीं बनती।
  • Friend Declaration Access Specifier (Public, Private या Protected) के किसी भी Section में लिखी जा सकती है।

Applications of Friend Function

Friend Function का उपयोग उन परिस्थितियों में किया जाता है जहाँ किसी External Function को Class के Private Data पर कार्य करना आवश्यक हो। विशेष रूप से Operator Overloading तथा Multiple Class Interaction में इसका उपयोग अधिक किया जाता है।

Application Purpose
Operator Overloading Binary Operators को Efficient तरीके से Overload करना।
Mathematical Libraries Complex Mathematical Objects पर Operations करना।
Data Sharing दो Classes के बीच Private Data का आदान-प्रदान।
Graphics Software Different Graphic Objects के बीच Information Exchange।
Scientific Applications Complex Scientific Calculations करना।
System Software Closely Related Classes के बीच Coordination बनाए रखना।

Advantages of Friend Class

  • दो Classes के बीच Efficient Data Sharing संभव होती है।
  • Private तथा Protected Members तक Controlled Access प्रदान करती है।
  • Complex Programs में Code को सरल बनाती है।
  • Related Classes के बीच Communication आसान हो जाता है।
  • Operator Overloading जैसे Advanced Features में उपयोगी है।
  • Large Object Oriented Applications में Design को बेहतर बनाती है।

Limitations of Friend Function and Friend Class

  • Data Hiding के सिद्धांत (Principle of Encapsulation) को कुछ हद तक प्रभावित करते हैं।
  • Private Members पर बाहरी Access मिलने से Security कम हो सकती है।
  • Program की Dependency बढ़ सकती है।
  • Friend Functions का अत्यधिक उपयोग Program को Maintain करना कठिन बना सकता है।
  • Improper Design से Software Architecture जटिल हो सकती है।
  • Friendship Inherited नहीं होती, इसलिए प्रत्येक नई Class के लिए अलग Declaration आवश्यक होती है।

Real Life Example

मान लीजिए किसी Bank Management System में Account Class के Private Balance को Transaction Class द्वारा Verify करना आवश्यक है। ऐसी स्थिति में Transaction को Friend Class बनाया जा सकता है ताकि वह बिना Public Getter Functions के Account का आवश्यक Data Access कर सके। इसी प्रकार किसी विशेष Calculation Function को Friend Function बनाकर वह विभिन्न Classes के Private Data पर कार्य कर सकता है।


Working of Friend Function and Friend Class

Class



friend Declaration



Friend Function / Friend Class



Private & Protected Members



Required Operation

Applications of Friend Function and Friend Class

Friend Function तथा Friend Class का उपयोग आधुनिक Object Oriented Programming में विशेष परिस्थितियों में किया जाता है जहाँ दो या अधिक Classes के बीच सुरक्षित रूप से Data Sharing आवश्यक होती है। ये Features Program को अधिक Flexible तथा Efficient बनाते हैं।

Application Purpose
Operator Overloading Binary Operators एवं Stream Operators को Overload करने के लिए।
Banking Software Account एवं Transaction Classes के बीच Secure Data Sharing।
Mathematical Libraries Complex Mathematical Objects पर Operations करने के लिए।
Graphics Applications Different Graphic Objects के बीच Information Exchange।
Scientific Software Multiple Related Classes के बीच Calculations करना।
System Software Closely Related Classes के बीच Internal Communication।

Advantages of Friend Function

  • Private तथा Protected Members तक Controlled Access प्रदान करता है।
  • दो या अधिक Classes के बीच Data Sharing आसान बनाता है।
  • Operator Overloading को सरल बनाता है।
  • Complex Programs में Code Reusability बढ़ाता है।
  • Related Objects के बीच Efficient Communication संभव बनाता है।
  • Complex Mathematical तथा Scientific Applications के लिए उपयोगी है।
  • Program की Flexibility बढ़ाता है।

Advantages of Friend Class

  • पूरी Class को आवश्यक Data तक Access प्रदान करती है।
  • Multiple Friend Functions लिखने की आवश्यकता कम हो जाती है।
  • Closely Related Classes के बीच Interaction सरल हो जाता है।
  • Large Software Projects में Code Organization बेहतर होती है।
  • Complex Object Relationships को Manage करना आसान होता है।
  • Professional Software Design में उपयोगी होती है।

Limitations of Friend Function and Friend Class

  • Encapsulation के सिद्धांत को आंशिक रूप से प्रभावित करते हैं।
  • Private Data की Security कम हो सकती है यदि Friend का उपयोग आवश्यकता से अधिक किया जाए।
  • Classes के बीच Dependency बढ़ जाती है।
  • Maintenance अपेक्षाकृत कठिन हो सकती है।
  • Friendship Automatic या Inherited नहीं होती।
  • Improper Design से Software Architecture Complex हो सकता है।
  • Friend Features का अत्यधिक उपयोग Object Oriented Design को कमजोर कर सकता है।

Best Practices for Using Friend Function

  • Friend Function का उपयोग केवल आवश्यकता होने पर ही करें।
  • Encapsulation बनाए रखने के लिए Friend Declaration को सीमित रखें।
  • केवल Trusted Functions अथवा Classes को Friend घोषित करें।
  • यदि Public Member Functions पर्याप्त हों, तो Friend Function का उपयोग न करें।
  • Friend Class का उपयोग केवल Closely Related Classes के लिए करें।
  • Program Documentation में Friend Relationship का उल्लेख अवश्य करें।
  • Software Security को ध्यान में रखते हुए Friend Features का उपयोग करें।

Real Life Example

मान लीजिए किसी University Management System में Student Class के Private Marks को Result Class द्वारा Process करना आवश्यक है। यदि Result को Friend Class घोषित किया जाए, तो वह सीधे Student के Private Data को Access करके Grade तथा Percentage Calculate कर सकती है। इसी प्रकार किसी विशेष calculateResult() Function को Friend Function बनाकर आवश्यक Calculations की जा सकती हैं।


Working of Friend Function & Friend Class

Class A



friend Declaration



Friend Function / Friend Class



Access Private & Protected Members



Perform Required Operations

Summary

Friend Function तथा Friend Class C++ के महत्वपूर्ण Features हैं जिनकी सहायता से किसी Class के Private तथा Protected Members को नियंत्रित (Controlled) तरीके से Access किया जा सकता है। Friend Function Class का Member नहीं होता, लेकिन friend Keyword द्वारा Declare किए जाने पर वह Class के Private Data तक पहुँच सकता है। इसी प्रकार Friend Class के सभी Member Functions दूसरी Class के Private तथा Protected Members को Access कर सकते हैं। इन Features का उपयोग विशेष रूप से Operator Overloading, Mathematical Libraries तथा Closely Related Classes के बीच Data Sharing के लिए किया जाता है।

Concept Description
Friend Function Class का Member न होकर भी Private तथा Protected Members को Access करने वाला Function।
Friend Class दूसरी Class के Private तथा Protected Members को Access करने की अनुमति प्राप्त Class।
friend Keyword Friend Function अथवा Friend Class Declare करने के लिए उपयोग किया जाता है।
Private Members Friend Permission मिलने पर सीधे Access किए जा सकते हैं।
Protected Members Friend Function तथा Friend Class द्वारा Access किए जा सकते हैं।
Friendship One Way होती है तथा Inherited नहीं होती।
Applications Operator Overloading, Data Sharing, Scientific एवं Business Applications।
Advantages Controlled Access, Flexibility तथा Efficient Object Interaction।

Important Viva Questions

  1. Friend Function क्या है?
  2. Friend Function को कैसे Declare किया जाता है?
  3. Friend Class क्या होती है?
  4. Friend Function तथा Member Function में क्या अंतर है?
  5. Friend Function Private Members को कैसे Access करती है?
  6. Friendship One Way क्यों होती है?
  7. क्या Friendship Inherited होती है?
  8. Friend Function में this Pointer क्यों उपलब्ध नहीं होता?
  9. Friend Function तथा Friend Class के Applications क्या हैं?
  10. Friend Function के मुख्य Advantages क्या हैं?

University Examination Important Questions

  1. Friend Function क्या है? Syntax एवं Program सहित समझाइए।
  2. Friend Class क्या है? उदाहरण सहित समझाइए।
  3. Friend Function तथा Member Function में अंतर लिखिए।
  4. Friend Function तथा Friend Class में अंतर लिखिए।
  5. Friend Function के Rules तथा Characteristics लिखिए।
  6. Friend Class के Advantages एवं Limitations लिखिए।
  7. Friend Keyword का उपयोग उदाहरण सहित समझाइए।
  8. Friend Function का Operator Overloading में उपयोग स्पष्ट कीजिए।
  9. Friend Function तथा Friend Class के Applications लिखिए।
  10. Object Oriented Programming में Friend Function की आवश्यकता स्पष्ट कीजिए।

Exam Tips

  • हमेशा याद रखें कि Friend Function Class का Member Function नहीं होता।
  • friend Keyword द्वारा Friend Function तथा Friend Class Declare की जाती है।
  • Friend Function Object द्वारा Call नहीं की जाती, बल्कि सामान्य Function की तरह Call होती है।
  • Friend Function में this Pointer उपलब्ध नहीं होता।
  • Friendship One Way होती है तथा Inherited नहीं होती।
  • Friend Class के सभी Member Functions दूसरी Class के Private एवं Protected Members को Access कर सकते हैं।
  • RGPV Exam में Friend Function का Program तथा Friend Function vs Member Function Difference अक्सर पूछा जाता है।

Conclusion

Friend Function तथा Friend Class C++ के ऐसे विशेष Features हैं जो आवश्यक परिस्थितियों में Data Hiding तथा Flexibility के बीच संतुलन (Balance) बनाए रखते हैं। इनकी सहायता से दो या अधिक Closely Related Classes के बीच सुरक्षित रूप से Data Sharing की जा सकती है। हालांकि इनका उपयोग सावधानीपूर्वक करना चाहिए क्योंकि आवश्यकता से अधिक Friend Declarations Encapsulation को प्रभावित कर सकती हैं। Operator Overloading, Scientific Computing, Banking Software, Graphics Applications तथा Enterprise Systems में Friend Function एवं Friend Class का व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical तथा Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।

Related Articles

Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year

Arrays ...

Read More →

Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year

Object ...

Read More →

Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year

Pointer...

Read More →

Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year

Dynamic...

Read More →

this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More →