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

Friend Function and Friend Class

Friend function क्या है?

Private members सामान्यतः बाहरी code के लिए वर्जित होते हैं। Friend function एक अपवाद है: एक non-member function जिस पर class स्पष्ट रूप से भरोसा करती है, उसे private और protected members तक पहुँच देते हुए। Class इसे friend keyword से declare करती है।

Friends क्यों हैं

कभी कोई function जो member नहीं है फिर भी वाकई internals चाहता है — जैसे दो objects की तुलना करने वाला operator, या एक helper जो दो अलग classes का private data पढ़े। सबके लिए data public बनाने के बजाय, class केवल उस एक function को पहुँच देती है।

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;
class Account {
    double balance = 1000;                 // private
    friend void show(const Account &a);    // access dein
};
void show(const Account &a) {              // member nahi
    cout << "Balance: " << a.balance << "\n";  // private padh sakta hai
}
int main() {
    Account acc;
    show(acc);        // normal function ki tarah call
    return 0;
}
Output:
Balance: 1000

ध्यान दें show अपने आप call होता है — acc.show() नहीं — क्योंकि यह member नहीं है।

Friend classes

पूरी class friend हो सकती है, तो उसके सभी methods दूसरे के private members तक पहुँच सकते हैं:

C++
class Engine {
    int power = 200;
    friend class Car;      // Car, Engine ke privates dekh sakti hai
};
class Car {
public:
    void showPower(const Engine &e) { cout << e.power; }
};

Friend बनाम member function

Member functionFriend function
Class का है?हाँनहीं
Call ऐसेobj.f()f(obj)
this है?हाँनहीं
Privates access?हाँहाँ (दी गई)

संक्षिप्त syntax उदाहरण और operator इस्तेमाल के लिए, friend function syntax reference देखें।

Friends कब इस्तेमाल करें

  • ऐसे operators जिन्हें दोनों operands का private data चाहिए (जैसे operator<<)।
  • दो घनिष्ठ संबंधित classes जहाँ एक को दूसरे के internals पढ़ने हों।
  • केवल तब जब member function या public getter इतनी साफ़ तरह फिट न बैठे।
💡 कम इस्तेमाल करें

Friendship encapsulation में जानबूझकर एक छेद है। कुछ friends ठीक हैं; बहुत सारे design की गंध हैं।

आम गलतियाँ

  • Friend function को obj.f() की तरह call करना — यह member नहीं है।
  • Friendship को mutual या inherited मानना (यह न कोई है)।
  • Friends का अति-इस्तेमाल करके चुपचाप encapsulation नष्ट करना।
  • Class के अंदर friend declaration भूलना।
🏋️ अभ्यास

एक Distance class लिखें जो metres private रखे, और एक friend function add(const Distance&, const Distance&) जो उनका योग लौटाए। पुष्टि करें कि यह दोनों objects के private metres पढ़ सकता है।

सारांश

  • Friend function एक non-member है जिसे private members तक पहुँच दी गई।
  • Class के अंदर friend से declare, बाहर परिभाषित।
  • Normal function की तरह call, object पर नहीं; इसके पास this नहीं।
  • Friend class अपने सभी methods के साथ पहुँच साझा करती है।
  • Friendship mutual या inherited नहीं — कम इस्तेमाल करें।

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

C++ में friend function क्या है?
Friend function एक non-member function है जिसे कोई class अपने private और protected members तक पहुँचने की विशेष अनुमति देती है। आप इसे class के अंदर friend keyword से declare करते हैं, पर यह बाहर परिभाषित होता है और member function की तरह object पर call नहीं होता।
C++ में friend functions क्यों इस्तेमाल होते हैं?
ये तब इस्तेमाल होते हैं जब कोई function जो member नहीं है फिर भी किसी class के private data तक पहुँच चाहता है — जैसे एक operator जो दो objects पर काम करता है, या एक helper जिसे एक से ज़्यादा classes की आंतरिक अवस्था पढ़नी हो। Friendship वह पहुँच data को public बनाए बिना देती है।
C++ में friend class क्या है?
Friend class ऐसी class है जिसके सभी member functions किसी अन्य class के private और protected members तक पहुँच सकते हैं जिसने इसे friend घोषित किया। यह तब इस्तेमाल होता है जब दो classes घनिष्ठ रूप से संबंधित हों और एक को वैध रूप से दूसरे के internals तक पूरी पहुँच चाहिए।
क्या C++ में friendship encapsulation तोड़ती है?
Friendship encapsulation को जानबूझकर और नियंत्रित तरीके से ढीला करती है: केवल friends के रूप में नामित विशिष्ट functions या classes को पहुँच मिलती है, और class खुद वह अनुमति देती है। कम इस्तेमाल में यह सुरक्षित है, पर friends का अति-इस्तेमाल data private रखने के लाभ कमज़ोर करता है।
क्या C++ में friendship inherited या mutual होती है?
नहीं। Friendship न inherited है न अपने आप mutual। अगर class A, B को friend घोषित करे, B की A तक पहुँच का मतलब यह नहीं कि A, B तक पहुँच सके, और B से derived classes अपने आप A की friends नहीं होतीं। हर friendship स्पष्ट रूप से देनी होती है।
← 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicInheritance in C++ आपकी प्रगति: 44 / 81 (54%) · Lesson 45 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

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

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

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

▶ CodingEasily on YouTube

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

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

▶ Channel subscribe करें