Operator Overloading Using Member and Friend Functions in C++

Operator overloading in C++ can be implemented using member functions and non-member functions. A non-member operator function can also be declared as a friend of a class when it needs access to private or protected members. Both approaches allow programmers to define how operators behave when used with objects. However, their syntax, implementation and function calls are different.

C++ में Operator Overloading को member functions और non-member functions की सहायता से implement किया जा सकता है। जब किसी non-member operator function को class के private या protected members तक access की आवश्यकता होती है, तो उसे friend function भी declare किया जा सकता है। दोनों approaches programmers को यह define करने की सुविधा देते हैं कि objects के साथ operators किस प्रकार कार्य करेंगे। हालाँकि, इनका syntax, implementation और function calls अलग-अलग होते हैं।

Operator Overloading Using Member Functions

A member function is a function declared inside a class. When an operator is overloaded using a member function, the left-hand operand is the object that calls the function, while the right-hand operand is passed as an argument.

Member function वह function है जिसे class के अंदर declare किया जाता है। जब किसी operator को member function की सहायता से overload किया जाता है, तब left-hand operand वह object होता है जो function को call करता है, जबकि right-hand operand को argument के रूप में pass किया जाता है।

Syntax of Operator Overloading Using a Member Function

return_type operator symbol (parameter_list) {
// Function body
}

Here, operator is a keyword, and symbol represents the operator being overloaded, such as +, - or *.

यहाँ operator एक keyword है और symbol उस operator को represent करता है जिसे overload किया जा रहा है, जैसे +, - या *।

Example: Overloading the Plus (+) Operator Using a Member Function

The following program demonstrates how to overload the + operator using a member function to add two complex numbers.

निम्न program दिखाता है कि दो complex numbers को जोड़ने के लिए member function की सहायता से + operator को कैसे overload किया जाता है।

#include <iostream>
using namespace std;
class Complex {
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator+(const Complex& obj) const {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(10, 5);
Complex c2(20, 8);
Complex c3 = c1 + c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Sum: ";
c3.display();
return 0;
}

Output:

First Complex Number: 10 + 5i
Second Complex Number: 20 + 8i
Sum: 30 + 13i

Explanation of the Program

The class Complex contains two private data members, real and imag, which store the real and imaginary parts of a complex number.

Complex class में दो private data members real और imag हैं, जो complex number के real और imaginary parts को store करते हैं।

The constructor initializes these two data members. The default arguments allow us to create a complex number with zero values when no arguments are supplied.

Constructor इन दोनों data members को initialize करता है। Default arguments की सहायता से बिना arguments दिए भी zero values वाला complex number create किया जा सकता है।

The member function operator+() overloads the addition operator. It receives the right-hand object through the parameter obj and adds its data members to those of the calling object.

Member function operator+() addition operator को overload करता है। यह obj parameter के माध्यम से right-hand object प्राप्त करता है और उसकी data members को calling object की data members के साथ जोड़ता है।

The expression:

Complex c3 = c1 + c2;

is interpreted as:

Complex c3 = c1.operator+(c2);

इसका अर्थ है कि c1 object के माध्यम से operator+() function call होता है और c2 argument के रूप में pass किया जाता है।

The function returns a new Complex object containing the sum of both complex numbers. The returned object is used to initialize c3.

Function एक नया Complex object return करता है जिसमें दोनों complex numbers का sum होता है। इस returned object की सहायता से c3 initialize होता है।

Remember: For a binary operator overloaded as a member function, the left-hand operand is the calling object and only the right-hand operand is explicitly passed as a parameter.

याद रखें: जब किसी binary operator को member function से overload किया जाता है, तब left-hand operand calling object होता है और केवल right-hand operand को explicitly parameter के रूप में pass किया जाता है।

Operator Overloading Using Friend Functions

A friend function is a non-member function that is granted special access to 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.

Friend function एक non-member function होता है जिसे class के private और protected members तक विशेष access दिया जाता है। इसे class के अंदर friend keyword की सहायता से declare किया जाता है, लेकिन इसकी definition सामान्य function की तरह class के बाहर लिखी जाती है।

When an operator is overloaded using a friend function, both operands are passed explicitly as function arguments. This is different from a member function, where the left-hand operand is the calling object.

जब किसी operator को friend function की सहायता से overload किया जाता है, तब दोनों operands को explicitly function arguments के रूप में pass किया जाता है। यह member function से अलग है, जहाँ left-hand operand calling object होता है।

Syntax of Operator Overloading Using a Friend Function

class ClassName {
private:
// Private data members
public:
friend return_type operator symbol(
const ClassName& obj1,
const ClassName& obj2
);
};

The friend function is declared inside the class using the friend keyword and defined outside the class.

Friend function को class के अंदर friend keyword की सहायता से declare किया जाता है और इसकी definition class के बाहर लिखी जाती है।

Example: Overloading the Plus (+) Operator Using a Friend Function

The following program demonstrates how to overload the + operator using a friend function to add two complex numbers.

निम्न program दिखाता है कि दो complex numbers को जोड़ने के लिए friend function की सहायता से + operator को कैसे overload किया जाता है।

#include <iostream>
using namespace std;
class Complex {
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
friend Complex operator+(
const Complex& obj1,
const Complex& obj2
);
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
Complex operator+(
const Complex& obj1,
const Complex& obj2
) {
Complex temp;
temp.real = obj1.real + obj2.real;
temp.imag = obj1.imag + obj2.imag;
return temp;
}
int main() {
Complex c1(10, 5);
Complex c2(20, 8);
Complex c3 = c1 + c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Sum: ";
c3.display();
return 0;
}

Output:

First Complex Number: 10 + 5i
Second Complex Number: 20 + 8i
Sum: 30 + 13i

Explanation of the Program

The class Complex contains two private data members, real and imag, to represent a complex number.

Complex class में complex number को represent करने के लिए दो private data members real और imag हैं।

Inside the class, the function operator+() is declared using the friend keyword. This declaration allows the function to access the private data members of the class.

Class के अंदर operator+() function को friend keyword की सहायता से declare किया गया है। यह declaration function को class के private data members access करने की permission देता है।

The function is defined outside the class. It receives two objects, obj1 and obj2, as arguments and adds their real and imaginary parts.

Function की definition class के बाहर लिखी गई है। यह obj1 और obj2 नाम के दो objects को arguments के रूप में receive करता है और उनके real तथा imaginary parts को जोड़ता है।

The expression:

Complex c3 = c1 + c2;

is interpreted as:

Complex c3 = operator+(c1, c2);

यहाँ दोनों objects को explicitly arguments के रूप में pass किया जाता है। Friend function किसी एक object के माध्यम से member function की तरह call नहीं होता।

The function returns a new Complex object containing the calculated result.

Function calculated result वाला एक नया Complex object return करता है।

Remember: A friend function is not a member function. It does not have a this pointer and both operands of a binary operator are passed explicitly.

याद रखें: Friend function, member function नहीं होता। इसमें this pointer नहीं होता और binary operator के दोनों operands explicitly pass किए जाते हैं।

Difference Between Member Function and Friend Function in Operator Overloading

Both member functions and friend functions can be used to overload many operators. The main difference is how they receive operands and access class members.

Member functions और friend functions दोनों का उपयोग कई operators को overload करने के लिए किया जा सकता है। इनके बीच मुख्य अंतर operands प्राप्त करने और class members access करने के तरीके में होता है।


Member FunctionFriend Function
It is a member of the class.It is a non-member function.
It is declared and defined as a class member.It is declared as a friend inside the class and defined outside it.
The left-hand operand is the calling object for a binary operator.Both operands are passed explicitly for a binary operator.
It has access to private and protected members of its class.It can access private and protected members because friendship grants permission.
It has a this pointer when it is a non-static member function.It does not have a this pointer.
For a binary operator, it normally takes one explicit parameter.For a binary operator, it normally takes two explicit parameters.
It is called through an object.It is called like an ordinary function, although operator syntax can invoke it.


Hindi Explanation: Member function class का हिस्सा होता है और binary operator के लिए left-hand object calling object होता है। इसके विपरीत friend function class का member नहीं होता और binary operator के दोनों operands को arguments के रूप में receive करता है।

When Should We Use Member Functions and Friend Functions?

The choice between a member function and a friend function depends on the operator, the intended design and the required access to class data.

Member function और friend function में से किसका उपयोग करना चाहिए, यह operator, intended design और class data तक आवश्यक access पर निर्भर करता है।

Use Member Functions When

  • The left-hand operand is naturally an object of the class.

  • The operation is closely associated with the class.

  • The operator must be implemented as a member function, such as =, [], () or ->.

Member Functions का उपयोग कब करें?

  • जब left-hand operand स्वाभाविक रूप से class का object हो।

  • जब operation class से closely associated हो।

  • जब operator को member function के रूप में implement करना आवश्यक हो, जैसे =, [], () या ->।

Use Friend Functions When

  • Both operands need to be passed explicitly.

  • The operation requires access to private or protected members but should remain a non-member function.

  • The operator needs a non-member implementation, such as stream insertion (<<) and stream extraction (>>).

Friend Functions का उपयोग कब करें?

  • जब दोनों operands को explicitly pass करना आवश्यक हो।

  • जब operation को private या protected members तक access चाहिए, लेकिन उसे non-member function रखना हो।

  • जब operator के लिए non-member implementation की आवश्यकता हो, जैसे stream insertion (<<) और stream extraction (>>)।

Note: A friend function should not be used unnecessarily. If an ordinary public member function or a suitable non-member function can perform the task without friendship, prefer the simpler design.

नोट: Friend function का अनावश्यक उपयोग नहीं करना चाहिए। यदि कोई public member function या suitable non-member function बिना friendship के काम कर सकता है, तो simpler design को prefer करना चाहिए।

Frequently Asked Questions (FAQs)

Q1. What are the two methods of operator overloading in C++?

Many operators can be overloaded using member functions or non-member functions. A non-member function may be declared as a friend when it requires access to private or protected members.

C++ में Operator Overloading के दो तरीके कौन-से हैं?

कई operators को member functions या non-member functions की सहायता से overload किया जा सकता है। यदि non-member function को private या protected members तक access की आवश्यकता हो, तो उसे friend declare किया जा सकता है।

Q2. What is a member function in operator overloading?

A member function is a function defined as part of a class. When used for binary operator overloading, the left-hand operand becomes the calling object.

Operator Overloading में Member Function क्या है?

Member function वह function है जो class का हिस्सा होता है। Binary operator overloading में left-hand operand calling object बन जाता है।

Q3. What is a friend function in operator overloading?

A friend function is a non-member function declared inside a class with the friend keyword. It can access the class's private and protected members.

Operator Overloading में Friend Function क्या है?

Friend function एक non-member function होता है जिसे class के अंदर friend keyword से declare किया जाता है। यह class के private और protected members को access कर सकता है।

Q4. Is a friend function a member function?

No. A friend function is not a member of the class, even though it is declared inside the class.

क्या Friend Function एक Member Function होता है?

नहीं। Friend function class के अंदर declare होने के बावजूद class का member नहीं होता।

Q5. Does a friend function have a this pointer?

No. The this pointer is available in non-static member functions, not in friend functions.

क्या Friend Function में this pointer होता है?

नहीं। this pointer non-static member functions में उपलब्ध होता है, friend functions में नहीं।

Q6. How many arguments are required for binary operator overloading?

A binary operator overloaded as a member function normally takes one explicit argument. When overloaded as a non-member function, it normally takes two explicit arguments.

Binary Operator Overloading में कितने arguments की आवश्यकता होती है?

Member function के रूप में overload किए गए binary operator में सामान्यतः एक explicit argument होता है। Non-member function के रूप में overload किए जाने पर सामान्यतः दो explicit arguments होते हैं।

Q7. Can we overload the addition operator using a friend function?

Yes. The + operator can be overloaded using a friend function, allowing it to access private members of the class.

क्या Friend Function की सहायता से Addition Operator को overload कर सकते हैं?

हाँ। + operator को friend function की सहायता से overload किया जा सकता है, जिससे function class के private members को access कर सकता है।

Q8. Which method is used for overloading stream insertion and extraction operators?

Stream insertion (<<) and stream extraction (>>) operators are commonly overloaded as non-member functions, often declared as friends of the class.

Stream Insertion और Extraction Operators को overload करने के लिए कौन-सा तरीका उपयोग किया जाता है?

Stream insertion (<<) और stream extraction (>>) operators को सामान्यतः non-member functions के रूप में overload किया जाता है। इन्हें अक्सर class के friend functions के रूप में declare किया जाता है।


Operator overloading using member and friend functions allows programmers to customize operators for user-defined data types. Member functions use the left-hand object as the calling object, whereas friend functions receive operands explicitly as arguments. Understanding these two methods is essential for implementing unary, binary, arithmetic and relational operator overloading in C++.

Member और friend functions की सहायता से operator overloading programmers को user-defined data types के लिए operators customize करने की सुविधा देता है। Member functions में left-hand object calling object होता है, जबकि friend functions operands को explicitly arguments के रूप में receive करते हैं। C++ में unary, binary, arithmetic और relational operator overloading implement करने के लिए इन दोनों methods को समझना आवश्यक है।

In the next article: Unary Operator Overloading in C++ – Unary Minus, Increment and Decrement Operators.

अगले article में: C++ में Unary Operator Overloading – Unary Minus, Increment और Decrement Operators।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)