C++ में Operator Overloading
In this page:
class ClassName {
public:
ClassName operator-(); // unary
ClassName operator+(const ClassName &other); // binary
};
Unary Operators Overload करना
C++ आपको यह redefine करने देता है कि '-' या '!' जैसे unary operators आपकी अपनी class के objects पर apply होने पर कैसे behave करते हैं।
एक unary operator एक single operand पर act करता है, इसलिए इसे overload करना -myVector या !isEmpty जैसे expressions को user-defined types के लिए भी naturally पढ़ने देता है।
उदाहरण: Overloading Unary Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Vector2D
class Vector2D {
// Members below can be accessed from outside the class
public:
int x, y;
Vector2D operator-() {
// Send Vector2D{-x, -y} back to the caller
return Vector2D{-x, -y};
}
};
// Program execution starts in main()
int main() {
Vector2D v{3, 4};
Vector2D neg = -v;
// Print to the console with cout
std::cout << neg.x << "," << neg.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Binary Operators Overload करना
Binary operators दो operands पर act करते हैं, और उन्हें overload करना — जैसे '+' या '-' — आपको point1 + point2 जैसे expressions लिखने देता है जो एक awkwardly-named method call करने की बजाय आपकी class के दो objects को उसी तरह combine करते हैं जैसे आप दो built-in numbers combine करते।
उदाहरण: Overloading Binary Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x, y;
Point operator+(const Point &other) {
return Point{x + other.x, y + other.y};
}
};
// Program execution starts in main()
int main() {
Point p1{1, 2}, p2{3, 4};
Point sum = p1 + p2;
// Print to the console with cout
std::cout << sum.x << "," << sum.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Relational Operators Overload करना
'==' या '<' जैसे relational operators overload करना आपको if-statements के अंदर सीधे अपने objects compare करने देता है या उन्हें sort जैसे STL algorithms के साथ इस्तेमाल करने देता है, जो elements के बीच ordering determine करने के लिए '<' पर depend करते हैं।
उदाहरण: Overloading Relational Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x;
bool operator==(const Point &other) {
// Send x == other.x back to the caller
return x == other.x;
}
};
// Program execution starts in main()
int main() {
Point p1{5}, p2{5};
// Print to the console with cout
std::cout << (p1 == p2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Friend Functions के साथ Overloading
कुछ operator overloads member functions की बजाय non-member friend functions की तरह लिखे जाते हैं — यह ज़रूरी है जब भी left-hand operand आपकी class का object न हो, जैसे std::cout के लिए '<<' overload करते समय या जब आपको '+' के दोनों sides पर symmetric behavior चाहिए हो।
उदाहरण: Overloading with Friend Functions
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x;
Point(int x) : x(x) {}
friend Point operator+(int value, const Point &p);
};
Point operator+(int value, const Point &p) {
// Send Point(value + p.x) back to the caller
return Point(value + p.x);
}
// Program execution starts in main()
int main() {
Point p(5);
Point result = 10 + p;
// Print to the console with cout
std::cout << result.x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Special Overloaded Operators
C++ unusual roles वाले special operators overload करने भी देता है: assignment operator '=' control करता है कि copy-assignment पर क्या होता है, और function-call operator '()' एक object को एक function की तरह called होने देता है — STL में हर जगह इस्तेमाल होने वाले functors के पीछे का mechanism।
उदाहरण: Special Overloaded Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Multiplier
class Multiplier {
// Members below can be accessed from outside the class
public:
int factor;
int operator()(int x) {
// Send x * factor back to the caller
return x * factor;
}
};
// Program execution starts in main()
int main() {
Multiplier triple{3};
// Print to the console with cout
std::cout << triple(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक operator को कुछ unexpected करने के लिए overload करना।
::या.जैसे operators overload करने की कोशिश करना, जिन्हें overload नहीं किया जा सकता।- जहाँ एक reference चाहिए वहाँ value से return करना, जैसे
operator=में।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: