Relational Operator Overloading in C++ with Examples
Relational operators are used to compare two values and determine their relationship. In C++, the relational operators include ==, !=, <, >, <=, and >=. By default, these operators work with built-in data types such as int, float, and double. However, we can overload them to compare objects of a class.
C++ में Relational Operators का उपयोग दो values की तुलना करने और उनके बीच संबंध निर्धारित करने के लिए किया जाता है। C++ में ==, !=, <, >, <= और >= relational operators हैं। ये operators सामान्यतः int, float और double जैसे built-in data types पर कार्य करते हैं। Operator Overloading की सहायता से हम इन्हें किसी class के objects की तुलना करने के लिए भी उपयोग कर सकते हैं।
What is Relational Operator Overloading?
Relational operator overloading is a feature of C++ that allows us to define how relational operators behave when they are used with objects of a user-defined class.
Relational Operator Overloading C++ की एक सुविधा है, जिसकी सहायता से हम यह निर्धारित कर सकते हैं कि जब relational operators का उपयोग किसी user-defined class के objects के साथ किया जाए, तो वे किस प्रकार कार्य करेंगे।
For example, suppose we have two objects n1 and n2 of a class named Number. We can overload the relational operators to write comparisons such as:
उदाहरण के लिए, मान लीजिए हमारे पास Number नाम की class के दो objects n1 और n2 हैं। हम relational operators को overload करके निम्नलिखित comparisons कर सकते हैं:
n1 == n2n1 != n2n1 < n2n1 > n2n1 <= n2n1 >= n2
These expressions compare the values stored in the objects according to the definitions provided by the class.
ये expressions class में दी गई definitions के अनुसार objects में stored values की तुलना करते हैं।
Relational Operators in C++
The following table describes the six relational operators used in C++.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
< | Less than | a < b |
> | Greater than | a > b |
<= | Less than or equal to | a <= b |
>= | Greater than or equal to | a >= b |
Each relational operator produces a Boolean result: true or false.
प्रत्येक relational operator एक Boolean result देता है: true या false।
Syntax of Relational Operator Overloading
Relational operators can be overloaded using member functions or non-member functions. The following is the general syntax for overloading a relational operator using a member function.
Relational operators को member functions या non-member functions की सहायता से overload किया जा सकता है। Member function का उपयोग करके relational operator overload करने का सामान्य syntax निम्नलिखित है।
return_type operator symbol(const ClassName& obj) const {// Comparison logic}
Here:
operatoris the keyword used for operator overloading.symbolrepresents the relational operator, such as==,<, or>.objrepresents the second object being compared.constafter the parameter list indicates that the function does not modify the current object.
यहाँ:
operatoroperator overloading के लिए उपयोग किया जाने वाला keyword है।symbolउस relational operator को दर्शाता है जिसे overload किया जा रहा है, जैसे==,<या>।objदूसरे object को दर्शाता है जिसकी तुलना की जा रही है।Parameter list के बाद
constयह बताता है कि function current object में बदलाव नहीं करता है।
For relational operators, the return type is generally bool, because the result of a comparison is either true or false.
Relational operators के लिए सामान्यतः return type bool रखा जाता है, क्योंकि comparison का परिणाम या तो true होता है या false।
C++ Program to Overload Relational Operators
The following program demonstrates how to overload all six relational operators using member functions. A class named Number is created to compare the integer values stored in its objects.
निम्नलिखित program में member functions का उपयोग करके सभी छह relational operators को overload करना दिखाया गया है। इसमें Number नाम की class बनाई गई है, जिसके objects में stored integer values की तुलना की जाती है।
Program: Overloading ==, !=, <, >, <= and >= Operators
#include <iostream>using namespace std;class Number {private:int value;public:Number(int v) {value = v;}bool operator==(const Number& obj) const {return value == obj.value;}bool operator!=(const Number& obj) const {return value != obj.value;}bool operator<(const Number& obj) const {return value < obj.value;}bool operator>(const Number& obj) const {return value > obj.value;}bool operator<=(const Number& obj) const {return value <= obj.value;}bool operator>=(const Number& obj) const {return value >= obj.value;}};int main() {Number n1(10);Number n2(20);Number n3(10);cout << boolalpha;cout << "n1 == n3: " << (n1 == n3) << endl;cout << "n1 != n2: " << (n1 != n2) << endl;cout << "n1 < n2: " << (n1 < n2) << endl;cout << "n2 > n1: " << (n2 > n1) << endl;cout << "n1 <= n3: " << (n1 <= n3) << endl;cout << "n2 >= n1: " << (n2 >= n1) << endl;return 0;}
Output
n1 == n3: truen1 != n2: truen1 < n2: truen2 > n1: truen1 <= n3: truen2 >= n1: true
Explanation of the Program
The
Numberclass contains a private integer data member namedvalue.The constructor initializes the value of each object.
Six operator functions are defined to overload the six relational operators.
Each function compares the current object's
valuewith the value of the object passed as an argument.The
boolreturn type indicates whether the comparison is true or false.The
constqualifier ensures that these comparison functions do not modify the objects.The
boolalphamanipulator displays Boolean results astrueorfalseinstead of1or0.Numberclass मेंvalueनाम का एक private integer data member है।Constructor प्रत्येक object की value को initialize करता है।
छह operator functions बनाकर सभी relational operators को overload किया गया है।
प्रत्येक function current object की
valueकी तुलना argument के रूप में प्राप्त object की value से करता है।boolreturn type यह बताता है कि comparison का परिणाम true है या false।constqualifier यह सुनिश्चित करता है कि comparison functions objects में कोई बदलाव न करें।boolalphamanipulator Boolean results को1या0के स्थान परtrueयाfalseके रूप में प्रदर्शित करता है।
For example, the expression n1 < n2 calls the overloaded less-than operator function. Since n1 contains 10 and n2 contains 20, the expression returns true.
उदाहरण के लिए, expression n1 < n2 overloaded less-than operator function को call करता है। चूँकि n1 में 10 और n2 में 20 है, इसलिए यह expression true return करता है।
Relational Operator Overloading Using Friend Functions
Relational operators can also be overloaded using friend functions. A friend function is a non-member function that is allowed to access the private and protected members of a class.
Relational operators को friend functions की सहायता से भी overload किया जा सकता है। Friend function एक non-member function होता है, जिसे class के private और protected members को access करने की अनुमति दी जाती है।
The following example overloads the less-than (<) operator using a friend function.
निम्नलिखित उदाहरण में friend function का उपयोग करके less-than (<) operator को overload किया गया है।
Program: Overloading Less Than Operator Using Friend Function
#include <iostream>using namespace std;class Number {private:int value;public:Number(int v) {value = v;}friend bool operator<(const Number& a,const Number& b);};bool operator<(const Number& a, const Number& b) {return a.value < b.value;}int main() {Number n1(15);Number n2(25);if (n1 < n2) {cout << "First number is smaller.";} else {cout << "First number is not smaller.";}return 0;}
Output
First number is smaller.Explanation
The
operator<()function is declared as a friend inside the class.It is defined outside the class as a non-member function.
It receives both objects as arguments.
Since it is a friend function, it can access the private data member
value.The function compares the values of both objects and returns a Boolean result.
operator<()function को class के अंदर friend के रूप में declare किया गया है।इसे class के बाहर non-member function के रूप में define किया गया है।
यह दोनों objects को arguments के रूप में प्राप्त करता है।
Friend function होने के कारण यह private data member
valueको access कर सकता है।यह दोनों objects की values की तुलना करके Boolean result return करता है।
Member Function vs Friend Function for Relational Operator Overloading
Both member functions and friend functions can be used to overload relational operators. The choice depends on the requirements of the class and the desired operator behavior.
Relational operators को overload करने के लिए member functions और friend functions दोनों का उपयोग किया जा सकता है। इनका चुनाव class की आवश्यकताओं और operator के desired behavior पर निर्भर करता है।
| Member Function | Friend Function |
|---|---|
| It is a member of the class. | It is a non-member function. |
| The left operand is the calling object. | Both operands are passed as arguments. |
| It can directly access private members. | It can access private members through friendship. |
| It has one explicit parameter for a binary operator. | It has two explicit parameters for a binary operator. |
Member Function | Friend Function |
|---|---|
| यह class का member होता है। | यह non-member function होता है। |
| Left operand calling object होता है। | दोनों operands arguments के रूप में दिए जाते हैं। |
| यह private members को सीधे access कर सकता है। | यह friendship के कारण private members को access कर सकता है। |
| Binary operator के लिए इसमें एक explicit parameter होता है। | Binary operator के लिए इसमें दो explicit parameters होते हैं। |
Rules for Relational Operator Overloading
The following guidelines should be considered when overloading relational operators in C++.
C++ में relational operators को overload करते समय निम्नलिखित बातों का ध्यान रखना चाहिए।
Relational operator functions generally return
bool.The comparison should be meaningful for the objects being compared.
The comparison functions should generally not modify either operand.
Use
constmember functions when the comparison does not modify the current object.Use
constreferences for object parameters to avoid unnecessary copying.Related comparison operators should follow consistent logic. For example, if two objects are equal, neither should be considered smaller than the other.
Relational operator functions सामान्यतः
boolreturn करते हैं।Comparison objects के लिए meaningful होना चाहिए।
Comparison functions को सामान्यतः किसी भी operand में बदलाव नहीं करना चाहिए।
यदि comparison current object में बदलाव नहीं करता है, तो
constmember function का उपयोग करना चाहिए।Unnecessary copying से बचने के लिए object parameters में
constreferences का उपयोग करना चाहिए।संबंधित comparison operators का logic consistent होना चाहिए। उदाहरण के लिए, यदि दो objects equal हैं, तो उनमें से किसी एक को दूसरे से छोटा नहीं माना जाना चाहिए।
Applications of Relational Operator Overloading
Relational operator overloading makes object comparisons easier and improves the readability of C++ programs. It is useful in applications where objects need to be compared based on their data.
Relational Operator Overloading objects की तुलना को आसान बनाता है और C++ programs की readability को बेहतर करता है। इसका उपयोग उन applications में किया जाता है जहाँ objects की तुलना उनके data के आधार पर करनी होती है।
Some common applications include:
कुछ सामान्य applications निम्नलिखित हैं:
Comparing student records based on marks or roll numbers.
Comparing employee records based on salary or experience.
Comparing dates and times.
Comparing measurements such as distance, weight, or temperature.
Sorting user-defined objects based on specific properties.
Marks या roll numbers के आधार पर student records की तुलना करना।
Salary या experience के आधार पर employee records की तुलना करना।
Dates और times की तुलना करना।
Distance, weight या temperature जैसी measurements की तुलना करना।
किसी विशेष property के आधार पर user-defined objects को sort करना।
Frequently Asked Questions (FAQs)
1. What is relational operator overloading in C++?
Relational operator overloading allows us to define the behavior of comparison operators when they are used with objects of a user-defined class.
Relational Operator Overloading हमें यह define करने की सुविधा देता है कि comparison operators user-defined class के objects के साथ उपयोग किए जाने पर किस प्रकार कार्य करेंगे।
2. Which relational operators can be overloaded in C++?
The six relational operators ==, !=, <, >, <=, and >= can be overloaded in C++.
C++ में छह relational operators ==, !=, <, >, <= और >= को overload किया जा सकता है।
3. What is the return type of a relational operator function?
A relational operator function generally returns bool, because a comparison produces either true or false.
Relational operator function सामान्यतः bool return करता है, क्योंकि comparison का परिणाम या तो true होता है या false।
4. Can relational operators be overloaded using friend functions?
Yes. Relational operators can be overloaded using either member functions or non-member friend functions.
हाँ। Relational operators को member functions या non-member friend functions दोनों की सहायता से overload किया जा सकता है।
5. What is the difference between == and = operators?
The == operator checks whether two values are equal, whereas the = operator assigns a value to a variable or object.
== operator यह जाँचता है कि दो values समान हैं या नहीं, जबकि = operator किसी variable या object को value assign करता है।
6. Can we compare two objects without overloading relational operators?
Yes. We can compare their data members using ordinary member functions or other comparison functions. Operator overloading simply provides a more natural syntax.
हाँ। हम सामान्य member functions या अन्य comparison functions की सहायता से objects के data members की तुलना कर सकते हैं। Operator overloading केवल comparison के लिए अधिक natural syntax प्रदान करता है।
Relational operator overloading is an important feature of object-oriented programming in C++. It allows programmers to compare user-defined objects using familiar comparison symbols such as ==, !=, <, >, <=, and >=. By defining meaningful and consistent comparison logic, we can make C++ programs easier to understand and maintain.
Relational Operator Overloading C++ की Object-Oriented Programming का एक महत्वपूर्ण feature है। इसकी सहायता से programmers user-defined objects की तुलना ==, !=, <, >, <= और >= जैसे सामान्य comparison symbols का उपयोग करके कर सकते हैं। Meaningful और consistent comparison logic define करके हम C++ programs को अधिक सरल और maintainable बना सकते हैं।
Comments
Post a Comment