🟣 OOP  ·  Lesson 50

Operator Overloading

What is Operator Overloading?

Operator Overloading
Operator overloading gives special meaning to operators for user-defined classes. It makes object operations natural and readable.
Level
🟣 Object-Oriented Programming
Example File
operator-overloading.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
operator functionOperator Overloading programming में Operator function use होता है।
overloadOperator Overloading programming में Overload use होता है।
user-defined typeUser-Operator Overloading programming में defined type use होता है।
+ operator+ Operator Overloading programming में operator use होता है।

Syntax / Pattern

C++
ReturnType operator+(const ClassName &obj) { }

Example Program

main.cpp
#include <iostream>
using namespace std;
class Number{
public:
    int x;
    Number(int v): x(v) {}
    Number operator+(Number n){ return Number(x + n.x); }
};
int main(){ Number a(5), b(7); Number c = a + b; cout << c.x; }

Expected Output

12

Program Explanation

  • operator+ adds values inside two Number objects.
  • a + b becomes a.operator+(b).
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • complex numbers
  • matrix addition
  • custom classes

Common Mistakes

  • Overloading operators in confusing ways.
  • Trying to overload operators that cannot be overloaded.

Practice Tasks

  1. Overload + for Distance class.
  2. Explain why operator overloading improves readability.

Summary

Operator Overloading एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

Operator Overloading क्या है?

Operator Overloading
Operator overloading gives special meaning to operators for user-defined classes. It makes object operations natural and readable.
Level
🟣 Object-Oriented Programming
Example File
operator-overloading.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
operator functionOperator function Operator Overloading programming में use होता है।
overloadOverload Operator Overloading programming में use होता है।
user-defined typeUser-defined type Operator Overloading programming में use होता है।
+ operator+ operator Operator Overloading programming में use होता है।

Syntax / Pattern

C++
ReturnType operator+(const ClassName &obj) { }

Example Program

main.cpp
#include <iostream>
using namespace std;
class Number{
public:
    int x;
    Number(int v): x(v) {}
    Number operator+(Number n){ return Number(x + n.x); }
};
int main(){ Number a(5), b(7); Number c = a + b; cout << c.x; }

Expected Output

12

Program Explanation

  • operator+ adds values inside two Number objects.
  • a + b becomes a.operator+(b).
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • complex numbers
  • matrix addition
  • custom classes

आम गलतियाँ (Common Mistakes)

  • Overloading operators in confusing ways.
  • Trying to overload operators that cannot be overloaded.

अभ्यास (Practice Tasks)

  1. Overload + for Distance class.
  2. Explain why operator overloading improves readability.

सारांश

Operator Overloading एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.