Member Functions vs Friend Functions

Introduction

C++ में Operator Overloading को implement करने के लिए दो तरीके होते हैं — Member Functions और Friend Functions। दोनों का उपयोग operators को overload करने के लिए किया जाता है, लेकिन इनके काम करने का तरीका और access mechanism अलग होता है।

जब हम class के अंदर operator function define करते हैं, तो वह member function कहलाता है। वहीं, जब हम class के बाहर function define करके उसे friend घोषित करते हैं, तो वह friend function कहलाता है।

इन दोनों के बीच का अंतर समझना आवश्यक है ताकि सही परिस्थिति में सही method का चयन किया जा सके।

Definition

Member Function

Member function वह function होता है जो class के अंदर define किया जाता है और class के data members को सीधे access कर सकता है।

Friend Function

Friend function वह function होता है जो class का member नहीं होता, लेकिन उसे class के private और protected members को access करने की अनुमति दी जाती है।

Member Function का Syntax

class ClassName {
public:
returnType operator symbol (parameters) {
// function body
}
};

Friend Function का Syntax

class ClassName {
private:
dataType data;public:
friend returnType operator symbol (ClassName obj1, ClassName obj2);
};

Example (Member Function)

#include <iostream>
using namespace std;

class Number {
public:
int value;

Number(int v) {
value = v;
}

Number operator+(Number n) {
return Number(value + n.value);
}
};

int main() {
Number n1(10), n2(20);

Number n3 = n1 + n2;

cout << "Sum: " << n3.value;

return 0;
}

Output:

Sum: 30

Example (Friend Function)

#include <iostream>
using namespace std;

class Number {
private:
int value;
public:
Number(int v) {
value = v;
}

friend Number operator+(Number n1, Number n2);
};

Number operator+(Number n1, Number n2) {
return Number(n1.value + n2.value);
}

int main() {
Number n1(5), n2(15);

Number n3 = n1 + n2;

cout << "Sum: " << n3.value;

return 0;
}

Output:

Sum: 20

Working को समझना

Member Function

  • Left operand object होता है (n1)
  • Function उसी object के साथ call होता है
  • Right operand parameter के रूप में जाता है

Friend Function

  • दोनों operands parameters के रूप में pass होते हैं
  • Function class के बाहर define होता है
  • फिर भी private data को access कर सकता है

Member vs Friend Function में अंतर

आधारMember FunctionFriend Function
LocationClass के अंदरClass के बाहर
AccessDirect accessFriend declaration से access
OperandsLeft operand implicitदोनों operands explicit
Callobject के साथnormal function call
UseSimple casesComplex या symmetric cases

कब कौन सा उपयोग करें?

  • Member Function → जब left operand class object हो
  • Friend Function → जब दोनों operands समान हों या external access चाहिए

निष्कर्ष

Member functions और friend functions दोनों operator overloading के लिए उपयोगी हैं, लेकिन उनका उपयोग situation के अनुसार किया जाता है। Member functions simple cases के लिए उपयुक्त होते हैं, जबकि friend functions flexibility और wider access प्रदान करते हैं।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top