🟣 OOP · Lesson 50
Operator Overloading
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.
Operator overloading gives special meaning to operators for user-defined classes. It makes object operations natural and readable.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
operator-overloading.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| operator function | Operator Overloading programming में Operator function use होता है। |
| overload | Operator Overloading programming में Overload use होता है। |
| user-defined type | User-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
- Overload + for Distance class.
- 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.
Operator overloading gives special meaning to operators for user-defined classes. It makes object operations natural and readable.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
operator-overloading.cppमुख्य फोकस
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
इसे क्यों सीखें?
- यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
- यह school practicals, projects और competitive programming में बार-बार use होता है।
- यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।
ज़रूरी Terms
| Term | अर्थ / उपयोग |
|---|---|
| operator function | Operator function Operator Overloading programming में use होता है। |
| overload | Overload Operator Overloading programming में use होता है। |
| user-defined type | User-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)
- Overload + for Distance class.
- Explain why operator overloading improves readability.
सारांश
Operator Overloading एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.