Binary Operator Overloading in C++ – Addition + , Subtraction - and Multiplication *

Binary operator overloading is a feature of C++ that allows programmers to define how binary operators work with user-defined data types such as classes and objects. A binary operator performs an operation on two operands. Common binary operators in C++ include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), equality (==) and relational operators.

C++ में Binary Operator Overloading एक ऐसा feature है जिसकी सहायता से programmer यह define कर सकता है कि binary operators user-defined data types जैसे classes और objects के साथ कैसे कार्य करेंगे। Binary operator दो operands पर operation perform करता है। C++ में सामान्य binary operators में addition (+), subtraction (-), multiplication (*), division (/), modulus (%), equality (==) और relational operators शामिल हैं।

What Is a Binary Operator?

A binary operator is an operator that operates on two operands. It performs operations such as addition, subtraction, multiplication and comparison.

Binary operator वह operator होता है जो दो operands पर कार्य करता है। इसका उपयोग addition, subtraction, multiplication और comparison जैसे operations के लिए किया जाता है।

For example:

int a = 20, b = 10;
int sum = a + b;
int difference = a - b;
int product = a * b;

In these expressions, +, - and * are binary operators because each operates on two operands.

इन expressions में +, - और * binary operators हैं क्योंकि प्रत्येक operator दो operands पर कार्य करता है।

Binary Operator Overloading in C++

In C++, binary operators can be overloaded to perform specific operations on objects. For example, we can overload the + operator to add two complex number objects or overload the * operator to multiply two matrix objects.

C++ में binary operators को objects पर specific operations perform करने के लिए overload किया जा सकता है। उदाहरण के लिए, दो complex number objects को जोड़ने के लिए + operator या दो matrix objects को multiply करने के लिए * operator overload किया जा सकता है।

When a binary operator is overloaded using a member function, the left-hand operand becomes the calling object and the right-hand operand is passed as an explicit argument.

जब किसी binary operator को member function की सहायता से overload किया जाता है, तब left-hand operand calling object बन जाता है और right-hand operand को explicit argument के रूप में pass किया जाता है।

Syntax of Binary Operator Overloading Using a Member Function

return_type operator symbol(const ClassName& obj) const {
// Function body
}

Here, operator is a keyword, symbol represents the operator being overloaded, and the parameter receives the right-hand operand.

यहाँ operator एक keyword है, symbol उस operator को represent करता है जिसे overload किया जा रहा है और parameter right-hand operand को receive करता है।

Overloading the Addition (+) Operator

The addition operator (+) is used to add two numeric values. In a user-defined class, it can be overloaded to add the corresponding data members of two objects.

Addition operator (+) का उपयोग दो numeric values को जोड़ने के लिए किया जाता है। किसी user-defined class में इसे दो objects के corresponding data members को जोड़ने के लिए overload किया जा सकता है।

For example, if two complex numbers are represented as a + bi and c + di, their sum is (a + c) + (b + d)i.

उदाहरण के लिए, यदि दो complex numbers को a + bi और c + di के रूप में represent किया जाता है, तो उनका sum (a + c) + (b + d)i होगा।

Program: Addition of Two Complex Numbers Using Operator Overloading

#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 {
return Complex(real + obj.real,
imag + obj.imag);
}
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 << "Addition: ";
c3.display();
return 0;
}

Output:

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

Explanation

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

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

The constructor initializes these data members with the supplied values. Default arguments allow the creation of a complex object with zero values.

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

The function operator+() overloads the addition operator. It receives the second complex number as an argument and adds its real and imaginary parts to those of the calling object.

operator+() function addition operator को overload करता है। यह दूसरे complex number को argument के रूप में receive करता है और उसके real तथा imaginary parts को calling object के corresponding parts के साथ जोड़ता है।

The expression:

Complex c3 = c1 + c2;

is interpreted as:

Complex c3 = c1.operator+(c2);

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

The overloaded function returns a new complex number containing the addition result. The original objects remain unchanged.

Overloaded function addition result वाला एक नया complex number return करता है। Original objects unchanged रहते हैं।

Overloading the Subtraction (-) Operator

The subtraction operator (-) is used to find the difference between two values. It can be overloaded to subtract the corresponding data members of two objects.

Subtraction operator (-) का उपयोग दो values के बीच difference निकालने के लिए किया जाता है। इसे दो objects के corresponding data members को subtract करने के लिए overload किया जा सकता है।

For complex numbers, the subtraction rule is:

[
(a+bi)-(c+di)=(a-c)+(b-d)i
]

इसमें दोनों complex numbers के real parts को आपस में subtract किया जाता है और imaginary parts को भी आपस में subtract किया जाता है।

Program: Subtraction of Two Complex Numbers Using Operator Overloading

#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 {
return Complex(real - obj.real,
imag - obj.imag);
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(30, 15);
Complex c2(10, 5);
Complex c3 = c1 - c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Subtraction: ";
c3.display();
return 0;
}

Output:

First Complex Number: 30 + 15i
Second Complex Number: 10 + 5i
Subtraction: 20 + 10i

Explanation

The class Complex represents a complex number using the private data members real and imag.

Complex class private data members real और imag की सहायता से complex number को represent करती है।

The member function operator-() overloads the subtraction operator. It subtracts the real part of the second object from the real part of the calling object. It performs the same operation on the imaginary parts.

Member function operator-() subtraction operator को overload करता है। यह दूसरे object के real part को calling object के real part से subtract करता है। यही operation imaginary parts पर भी perform किया जाता है।

The expression c1 - c2 invokes the overloaded subtraction function and returns a new Complex object containing the difference.

Expression c1 - c2 overloaded subtraction function को invoke करता है और difference वाला एक नया Complex object return करता है।

The original objects c1 and c2 are not modified because the function returns a new object instead of changing the operands.

Original objects c1 और c2 modify नहीं होते क्योंकि function operands को change करने के स्थान पर एक नया object return करता है।

Overloading the Multiplication (*) Operator

The multiplication operator (*) is used to multiply two values. It can also be overloaded to perform multiplication between objects of a user-defined class.

Multiplication operator (*) का उपयोग दो values को multiply करने के लिए किया जाता है। इसे user-defined class के objects के बीच multiplication perform करने के लिए भी overload किया जा सकता है।

For complex numbers, the multiplication rule is:

[
(a+bi)(c+di)=(ac-bd)+(ad+bc)i
]

इस formula में real part ac - bd और imaginary part ad + bc होता है।

Program: Multiplication of Two Complex Numbers Using Operator Overloading

#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 {
int r = real * obj.real - imag * obj.imag;
int i = real * obj.imag + imag * obj.real;
return Complex(r, i);
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(4, 3);
Complex c2(2, 1);
Complex c3 = c1 * c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Multiplication: ";
c3.display();
return 0;
}

Output:

First Complex Number: 4 + 3i
Second Complex Number: 2 + 1i
Multiplication: 5 + 10i

Explanation

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

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

The member function operator*() overloads the multiplication operator. It calculates the real and imaginary parts using the complex number multiplication formula.

Member function operator*() multiplication operator को overload करता है। यह complex number multiplication formula की सहायता से real और imaginary parts calculate करता है।

For the given values, the calculation is:

[
(4+3i)(2+i)
]

[
=(4\times2-3\times1)+(4\times1+3\times2)i
]

[
=5+10i
]

इस उदाहरण में दोनों complex numbers को multiply करने पर real part 5 और imaginary part 10 प्राप्त होता है।

The function returns a new Complex object containing the multiplication result. The original operands remain unchanged.

Function multiplication result वाला एक नया Complex object return करता है। Original operands unchanged रहते हैं।

Binary Operator Overloading Using Friend Functions

Binary operators can also be overloaded using non-member functions, including friend functions. In this approach, both operands are passed explicitly as function arguments.

Binary operators को non-member functions, जिनमें friend functions भी शामिल हैं, की सहायता से भी overload किया जा सकता है। इस approach में दोनों operands को explicitly function arguments के रूप में pass किया जाता है।

For example, the expression:

c1 + c2;

is handled by a non-member overloaded function in the form:

operator+(c1, c2);

इस approach में left-hand operand किसी member function का calling object नहीं होता। दोनों objects arguments के रूप में function को pass किए जाते हैं।

Example: Overloading the Addition Operator Using a Friend Function

#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int v = 0) {
value = v;
}
friend Number operator+(
const Number& n1,
const Number& n2
);
void display() const {
cout << "Value = " << value << endl;
}
};
Number operator+(
const Number& n1,
const Number& n2
) {
return Number(n1.value + n2.value);
}
int main() {
Number n1(100);
Number n2(200);
Number n3 = n1 + n2;
cout << "First Number: ";
n1.display();
cout << "Second Number: ";
n2.display();
cout << "Sum: ";
n3.display();
return 0;
}

Output:

First Number: Value = 100
Second Number: Value = 200
Sum: Value = 300

Explanation

The class Number contains a private data member named value. The friend function operator+() is declared inside the class.

Number class में value नाम का एक private data member है। Friend function operator+() को class के अंदर declare किया गया है।

The function is defined outside the class and receives two objects as arguments. Since it is a friend function, it can access the private data members of both objects.

Function की definition class के बाहर लिखी गई है और यह दो objects को arguments के रूप में receive करता है। Friend function होने के कारण यह दोनों objects के private data members को access कर सकता है।

The function adds the values of both objects and returns a new Number object containing the result.

Function दोनों objects की values को जोड़ता है और result वाला एक नया Number object return करता है।

Note: A friend function is not a member function. It has no this pointer. The + operator can also be overloaded using a suitable non-member function that does not require friendship, provided it can access the necessary data through the class's public interface.

नोट: Friend function, member function नहीं होता और इसमें this pointer नहीं होता। + operator को ऐसे suitable non-member function से भी overload किया जा सकता है जिसे friendship की आवश्यकता न हो, बशर्ते वह class के public interface के माध्यम से आवश्यक data access कर सके।

Difference Between Unary and Binary Operator Overloading

Unary and binary operator overloading differ in the number of operands and the parameters required by their overloaded functions.

Unary और binary operator overloading में operands की संख्या और overloaded functions में आवश्यक parameters के आधार पर अंतर होता है।

Unary Operator OverloadingBinary Operator Overloading
Operates on one operand.Operates on two operands.
Examples: -obj, ++obj, --objExamples: obj1 + obj2, obj1 - obj2
A member function normally takes no explicit parameter.A member function normally takes one explicit parameter.
A non-member function normally takes one explicit parameter.A non-member function normally takes two explicit parameters.
Used for operations such as changing signs or incrementing values.Used for operations such as addition, subtraction and multiplication.

Hindi Explanation: Unary operator केवल एक operand पर कार्य करता है, जबकि binary operator दो operands पर कार्य करता है। Member function के रूप में unary operator को overload करने पर सामान्यतः कोई explicit parameter नहीं होता, जबकि binary operator के लिए एक explicit parameter होता है।

Difference Between Member Function and Friend Function in Binary Operator Overloading

Binary operators can be overloaded using member functions or non-member functions. A friend function is one type of non-member function that can access private and protected members.

Binary operators को member functions या non-member functions की सहायता से overload किया जा सकता है। Friend function एक प्रकार का non-member function है जो private और protected members को access कर सकता है।

Member FunctionFriend Function
It is a member of the class.It is a non-member function.
Left-hand operand is the calling object.Both operands are passed as arguments.
Normally takes one explicit parameter for a binary operator.Normally takes two explicit parameters for a binary operator.
Has access to private and protected members of its class.Gets access through the friend declaration.
Called through an object.Called like an ordinary function; operator syntax can invoke it.

Hindi Explanation: Member function में left-hand object calling object होता है और केवल right-hand operand को parameter के रूप में pass किया जाता है। Friend function में दोनों operands arguments के रूप में pass किए जाते हैं।

Frequently Asked Questions (FAQs)

Q1. What is binary operator overloading in C++?

Binary operator overloading allows programmers to define how binary operators perform operations on objects of user-defined classes.

C++ में Binary Operator Overloading क्या है?

Binary operator overloading programmers को user-defined classes के objects पर binary operators के operations define करने की सुविधा देता है।

Q2. Which operators can be overloaded as binary operators?

Common examples include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), equality (==) and relational operators.

कौन-कौन से operators को Binary Operators के रूप में overload किया जा सकता है?

सामान्य उदाहरणों में addition (+), subtraction (-), multiplication (*), division (/), modulus (%), equality (==) और relational operators शामिल हैं।

Q3. How many arguments are required for a binary operator overloaded as a member function?

A binary operator overloaded as a member function normally requires one explicit argument because the left-hand operand is the calling object.

Member Function के रूप में Binary Operator Overload करने के लिए कितने arguments की आवश्यकता होती है?

Member function के रूप में overload किए गए binary operator में सामान्यतः एक explicit argument की आवश्यकता होती है क्योंकि left-hand operand calling object होता है।

Q4. How many arguments are required for a binary operator overloaded as a friend function?

A binary operator overloaded as a non-member friend function normally requires two explicit arguments, one for each operand.

Friend Function के रूप में Binary Operator Overload करने के लिए कितने arguments की आवश्यकता होती है?

Non-member friend function के रूप में overload किए गए binary operator में सामान्यतः दो explicit arguments की आवश्यकता होती है, प्रत्येक operand के लिए एक।

Q5. Can we overload the + operator in C++?

Yes. The + operator can be overloaded to perform addition between objects of user-defined classes.

क्या C++ में + Operator को Overload कर सकते हैं?

हाँ। User-defined classes के objects के बीच addition perform करने के लिए + operator को overload किया जा सकता है।

Q6. Can we overload the multiplication operator (*)?

Yes. The multiplication operator can be overloaded to perform multiplication between objects, such as complex numbers or matrices, according to the class's defined behavior.

क्या Multiplication Operator (*) को Overload कर सकते हैं?

हाँ। Multiplication operator को complex numbers या matrices जैसे objects के बीच multiplication perform करने के लिए overload किया जा सकता है।

Q7. Does operator overloading change the original meaning of an operator?

No. Operator overloading defines additional behavior for user-defined types. Its original behavior with built-in data types remains unchanged.

क्या Operator Overloading से operator का original meaning बदल जाता है?

नहीं। Operator overloading user-defined types के लिए additional behavior define करता है। Built-in data types के साथ उसका original behavior unchanged रहता है।

Q8. Can binary operators be overloaded using friend functions?

Yes. Many binary operators can be overloaded using friend functions. Both operands are passed explicitly as arguments.

क्या Binary Operators को Friend Functions से overload कर सकते हैं?

हाँ। कई binary operators को friend functions की सहायता से overload किया जा सकता है। दोनों operands को explicitly arguments के रूप में pass किया जाता है।

Binary operator overloading allows programmers to perform operations such as addition, subtraction and multiplication on objects of user-defined classes. It makes expressions more natural and readable when the overloaded behavior follows the expected meaning of the operator. In C++, binary operators can be overloaded using member functions or non-member functions, including friend functions. Understanding these techniques is useful for implementing mathematical and object-oriented applications.

Binary operator overloading programmers को user-defined classes के objects पर addition, subtraction और multiplication जैसे operations perform करने की सुविधा देता है। जब overloaded behavior operator के expected meaning के अनुसार होता है, तब expressions अधिक natural और readable बनते हैं। C++ में binary operators को member functions या non-member functions, जिनमें friend functions भी शामिल हैं, की सहायता से overload किया जा सकता है। इन techniques को समझना mathematical और object-oriented applications implement करने के लिए उपयोगी है।

In the next article: Relational Operator Overloading in C++ – Equality, Inequality and Comparison Operators.

अगले article में: C++ में Relational Operator Overloading – Equality, Inequality और Comparison 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)