इस लेख में हम यह जानेंगे कि ऑपरेटर ओवरलोडिंग (Operator Overloading in C++ in Hindi) की परिभाषा क्या है और साथ ही साथ C++ में इसकी क्या विशेषता है और इसका क्या role है।
ऑपरेटर ओवरलोडिंग क्या है ?
Introduction
यह C++ की सबसे महत्वपूर्ण विशेषता है जो C++ में operator को दूसरा define करने का अवसर प्रदान करती है जिससे user के द्वारा declare किये गए data type, built in data type की तरह operator व्यवहार करते हैं। जैसे - C++ data type integer या float के साथ काम (work) करता है तब compiler, error program में आता है लेकिन operator overloading का use करके इसे ठीक किया जा सकता है।![]() |
Operator overloading in hindi |
Definition
"Operator Overloading एक ऐसा process है जो एक data type के लिए C++ Operator को एक extra memory प्रदान करती है।"Rules for operator overloading in c++ in Hindi
- यूजर, operator templates को नहीं बदल सकता।
- C++ Compiler में पहले से निर्दिष्ट Operator का ही use किया जाता है, यूजर नए operators को निर्मित नहीं कर सकता।
- किसी भी operator की overloading उसके वास्तविक अर्थ से है।
- Unary operators को इसलिए overload किया जाता है कि member function कोई स्पष्ट argument नहीं लेते हैं।
Syntax
return_type operator to be overloaded (parameters);Program
#include <iostream> // Correct header for cout, endl#include <conio.h> // For getch(), used in some compilers like Turbo C++using namespace std; // Needed for cout, endl without std::class space {int x, y, z;public:void get_data(int a, int b, int c);void display();void operator()(); // Overloaded function call operator};void space::get_data(int a, int b, int c) {x = a;y = b;z = c;}void space::display() {cout << x << " the name" << endl;cout << y << " the name" << endl;cout << z << " the name" << endl;}void space::operator()() {x = -x;y = -y;z = -z;}int main() { // Correct return type for main is intspace s;s.get_data(10, -20, 30);cout << "Before overloading:" << endl;s.display();s(); // Calls the overloaded function call operatorcout << "After overloading:" << endl;s.display();getch(); // To pause the output screen (only works in some IDEs)return 0; // main must return an int}(code-box)
Output
Before overloading:10 the name-20 the name30 the nameAfter overloading:-10 the name20 the name-30 the name(code-box)
कैसे काम करता है
- s.get_data(10, -20, 30); से x = 10, y = -20, z = 30 सेट होता है।
- s.display(); इसे प्रिंट करता है।
- s(); लाइन operator() को कॉल करती है, जिससे तीनों Variables के साइन पलट जाते हैं।
- फिर से s.display(); बदली हुई वैल्यू प्रिंट करता है।
लेख पसंद आया हो तो इसे जरूर शेयर करें।
Nice short note ����
ReplyDeleteThank you so much
DeleteProgram ka output kya hai ....?
ReplyDeleteJald hi ise update kr diya jaiga
Delete