🔹 What is Operator Overloading?
- Operator overloading allows us to redefine the way operators work with user-defined types (classes and structs).
- Example:
+
can be overloaded to add two objects.
📌 Example 1: Overloading Assignment Operator (=
)
🚀 Approach
- Define a
Box
class. - Overload the
=
(assignment) operator.
🔹 C++ Code
#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
// Constructor
Box(int l = 0) {
length = l;
}
// Overloading Assignment Operator
void operator=(const Box &b) {
length = b.length;
}
void display() {
cout << "Length: " << length << endl;
}
};
int main() {
Box box1(10), box2;
box2 = box1; // Uses overloaded '=' operator
box2.display(); // Output: Length: 10
return 0;
}
🔹 Output
Length: 10
📝 Explanation:
- The
=
operator assigns values frombox1
tobox2
.
📌 Example 2: Overloading Binary Operator (+
)
🚀 Approach
- Define a
Complex
class. - Overload
+
to add two complex numbers.
🔹 C++ Code
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
// Constructor
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overloading `+` Operator
Complex operator+(const Complex &c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 2), c2(1, 7);
Complex c3 = c1 + c2; // Uses overloaded `+` operator
c3.display(); // Output: 4 + 9i
return 0;
}
🔹 Output
4 + 9i
📝 Explanation:
- The
+
operator adds real and imaginary parts separately.
📌 Example 3: Overloading Increment (++
) and Decrement (--
) Operators
🚀 Approach
- Define a
Counter
class. - Overload both
++
(pre-increment) and--
(pre-decrement).
🔹 C++ Code
#include <iostream>
using namespace std;
class Counter {
private:
int value;
public:
Counter(int v = 0) {
value = v;
}
// Overloading Pre-Increment `++`
void operator++() {
++value;
}
// Overloading Pre-Decrement `--`
void operator--() {
--value;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Counter c(5);
++c; // Increments value
c.display(); // Output: 6
--c; // Decrements value
c.display(); // Output: 5
return 0;
}
🔹 Output
Value: 6
Value: 5
📝 Explanation:
- Pre-increment (
++c
) increases value by 1. - Pre-decrement (
--c
) decreases value by 1.
📌 Example 4: Overloading Input (>>
) and Output (<<
) Operators
🚀 Approach
- Define a
Student
class. - Overload
>>
for input and<<
for output.
🔹 C++ Code
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Overloading `>>` for Input
friend istream& operator>>(istream &in, Student &s) {
cout << "Enter Name & Age: ";
in >> s.name >> s.age;
return in;
}
// Overloading `<<` for Output
friend ostream& operator<<(ostream &out, const Student &s) {
out << "Student Name: " << s.name << ", Age: " << s.age;
return out;
}
};
int main() {
Student s;
cin >> s; // Input values
cout << s; // Output values
return 0;
}
🔹 Output
Enter Name & Age: Alex 21
Student Name: Alex, Age: 21
📝 Explanation:
>>
operator takes user input.<<
operator prints the object details.
📌 Example 5: Overloading Relational Operators (<
, >
)
🚀 Approach
- Define a
Distance
class. - Overload
<
and>
to compare distances.
🔹 C++ Code
#include <iostream>
using namespace std;
class Distance {
private:
int meters;
public:
Distance(int m = 0) {
meters = m;
}
// Overloading `<` Operator
bool operator<(const Distance &d) {
return meters < d.meters;
}
// Overloading `>` Operator
bool operator>(const Distance &d) {
return meters > d.meters;
}
void display() {
cout << "Distance: " << meters << " meters" << endl;
}
};
int main() {
Distance d1(10), d2(15);
if (d1 < d2)
cout << "d1 is smaller" << endl;
else
cout << "d1 is greater" << endl;
return 0;
}
🔹 Output
d1 is smaller
📝 Explanation:
d1 < d2
returns true since10 < 15
.
📌 Example 6: Overloading Unary Operator (-
)
🚀 Approach
- Define a
Measure
class. - Overload the
-
operator to negate the measurement.
🔹 C++ Code
#include <iostream>
using namespace std;
class Measure {
private:
int value;
public:
Measure(int v = 0) {
value = v;
}
// Overloading `-` Operator
Measure operator-() {
return Measure(-value);
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Measure m1(10);
Measure m2 = -m1; // Uses overloaded `-`
m2.display(); // Output: Value: -10
return 0;
}
🔹 Output
Value: -10
📝 Explanation:
- The
-
operator negates the object's value.
📌 Summary
Example | Operator | Description |
---|---|---|
Assignment Overloading | = |
Assign values between objects. |
Binary Operator Overloading | + |
Add two objects. |
Increment & Decrement Overloading | ++ , -- |
Modify object value. |
Input & Output Overloading | >> , << |
Handle input/output with objects. |
Relational Operator Overloading | < , > |
Compare two objects. |
Unary Operator Overloading | - |
Negate object values. |
🚀 Need more examples? Let me know! 🚀
0 Comments