Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 68

Friend Function & Class

त्वरित सार

Friend function एक non-member है जिस पर class अपने private data तक पहुँच का भरोसा करती है। यह पृष्ठ एक व्यावहारिक syntax reference है; पूरे concept, friends कब इस्तेमाल करें, और friend classes के लिए, मुख्य friend functions lesson देखें।

Declaration syntax

Class के अंदर prototype से पहले friend रखें; body बाहर बिना friend और बिना scope prefix परिभाषित करें:

C++
class Box {
    int w = 5;
    friend void print(const Box &b);   // declaration
};
void print(const Box &b) {             // definition (Box:: nahi)
    cout << b.w;
}

तीन friend रूप

रूपDeclaration
Free functionfriend void f(const T&);
अन्य class का methodfriend void C::m(const T&);
पूरी classfriend class C;

operator<< के लिए friend

Classic असली इस्तेमाल: cout << obj से printing। चूँकि stream बायाँ operand है, operator को non-member होना चाहिए — तो इसे private data तक पहुँचने को friend बनाया जाता है:

C++
#include <iostream>
using namespace std;
class Point {
    int x, y;
public:
    Point(int a, int b) : x(a), y(b) {}
    friend ostream& operator<<(ostream &os, const Point &p);
};
ostream& operator<<(ostream &os, const Point &p) {
    os << "(" << p.x << ", " << p.y << ")";
    return os;
}
int main() {
    Point p(3, 4);
    cout << p << "\n";   // (3, 4)
}
Output:
(3, 4)

व्यापक विषय के लिए operator overloading देखें।

दो objects पढ़ना

Friend एक साथ दो objects के privates भी पढ़ सकता है — तुलना या योग के लिए काम का:

C++
class Money {
    int paise;
public:
    Money(int p) : paise(p) {}
    friend int total(const Money &a, const Money &b) {
        return a.paise + b.paise;   // dono privates
    }
};

आम गलतियाँ

  • Friend की definition से पहले ClassName:: जोड़ना — यह member नहीं है।
  • बाहरी definition पर फिर friend लिखना (केवल declaration में)।
  • operator<< को member बनाना — stream बायाँ operand होना चाहिए।
  • operator<< से stream return करना भूलना।
🏋️ अभ्यास

एक Time class को private hours और minutes दें, फिर एक friend operator<< लिखें जो इसे hh:mm के रूप में print करे। cout << t; से test करें।

सारांश

  • Class के अंदर friend से declare; बाहर बिना scope prefix परिभाषित।
  • तीन रूप: free function, अन्य class का method, या पूरी class।
  • Friends किसी class के लिए operator<< लिखने का मानक तरीका हैं।
  • Friend एक साथ दो objects का private data पढ़ सकता है।
  • Concept और design मार्गदर्शन के लिए, मुख्य friend functions lesson देखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में friend function कैसे declare करते हैं?
Class के अंदर आप function के prototype से पहले friend लिखते हैं, जैसे friend void print(const Box &b);। फिर function class के बाहर friend keyword और class scope के बिना परिभाषित होता है, क्योंकि यह member नहीं है।
C++ में operator overloading के लिए friend function कैसे इस्तेमाल होता है?
Friend आमतौर पर operator<< जैसे operators के लिए इस्तेमाल होता है जिन्हें बाईं ओर stream और दाईं ओर object लेना होता है, जो member function नहीं कर सकता। Operator को friend घोषित करना इसे object के private data तक पहुँच देता है जबकि यह non-member रहता है।
operator<< के लिए member के बजाय friend function क्यों इस्तेमाल करें?
क्योंकि cout << obj में stream बायाँ operand है, operator को non-member होना चाहिए ताकि इसका पहला parameter stream हो सके। इसे friend बनाना object के private members तक पहुँच देता है जबकि स्वाभाविक stream << object syntax बनाए रखता है।
क्या C++ में friend function किसी अन्य class का member हो सकता है?
हाँ। एक class का member function दूसरी का friend घोषित हो सकता है, friend void OtherClass::method(...) रूप से। यह केवल उस एक method को पहुँच देता है, जो पूरी दूसरी class को friend बनाने से ज़्यादा प्रतिबंधात्मक और अक्सर साफ़ है।
C++ में friend function कहाँ परिभाषित होता है?
friend declaration class के अंदर जाता है, पर function body class के बाहर एक सामान्य non-member function की तरह परिभाषित होती है, बिना ClassName:: prefix के। इसे class body के अंदर inline भी परिभाषित किया जा सकता है, हालाँकि यह फिर भी non-member माना जाता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicCopy Constructor आपकी प्रगति: 68 / 81 (84%) · Lesson 69 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें