← Back to C++ Course | Chapter 8: OOP Core | Lesson 11 of 14

C++ में Operator Overloading

Operator overloading + या == जैसे symbols को आपके अपने objects के साथ काम करना सिखाता है, एक game को नए rules सिखाने जैसा। यह आपके objects को naturally इस्तेमाल होने देता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

Relational Operators Overload करना

'==' या '<' जैसे relational operators overload करना आपको if-statements के अंदर सीधे अपने objects compare करने देता है या उन्हें sort जैसे STL algorithms के साथ इस्तेमाल करने देता है, जो elements के बीच ordering determine करने के लिए '<' पर depend करते हैं।

उदाहरण: Overloading Relational Operators

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक operator को कुछ unexpected करने के लिए overload करना।
  2. :: या . जैसे operators overload करने की कोशिश करना, जिन्हें overload नहीं किया जा सकता।
  3. जहाँ एक reference चाहिए वहाँ value से return करना, जैसे operator= में।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.