ऑपरेटर ओवरलोडिंग क्या है operator overloading in c++ in hindi

4 Deepak
इस लेख में हम यह जानेंगे कि ऑपरेटर ओवरलोडिंग (Operator Overloading in C++ in Hindi) की परिभाषा क्या है और साथ ही साथ C++ में इसकी क्या विशेषता है और इसका क्या role है।


Table of contents (toc)


ऑपरेटर ओवरलोडिंग क्या है ?

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
Operator overloading in hindi



Definition

"Operator Overloading एक ऐसा process है जो एक data type के लिए C++ Operator को एक extra memory प्रदान करती है।"

Rules for operator overloading in c++ in Hindi

  1. यूजर, operator templates को नहीं बदल सकता।
  2. C++ Compiler में पहले से निर्दिष्ट Operator का ही use किया जाता है, यूजर नए operators को निर्मित नहीं कर सकता।
  3. किसी भी operator की overloading उसके वास्तविक अर्थ से है।
  4. 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 int
    space s;
    s.get_data(10, -20, 30);

    cout << "Before overloading:" << endl;
    s.display();

    s();  // Calls the overloaded function call operator

    cout << "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 name
30 the name
After overloading:
-10 the name
20 the name
-30 the name(code-box)

कैसे काम करता है

  1. s.get_data(10, -20, 30); से x = 10, y = -20, z = 30 सेट होता है।
  2. s.display(); इसे प्रिंट करता है।
  3. s(); लाइन operator() को कॉल करती है, जिससे तीनों Variables के साइन पलट जाते हैं।
  4. फिर से s.display(); बदली हुई वैल्यू प्रिंट करता है।

लेख पसंद आया हो तो इसे जरूर शेयर करें।

Post a Comment

4 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Hi please, do not spam in comments

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Out
Ok, Go it!