Examples of operator overloading
Arithmetic Operators
#include <iostream>
class Complex {
public:
double real, imag;
Complex(double r, double i) : real(r), imag(i) {}
// Overloading + operator
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2; // Using overloaded +
std::cout << c3.real << " + " << c3.imag << "i" << std::endl;
return 0;
}Assignment Operators
Comparison Operators
Increment/Decrement Operators
Logical Operators
Bitwise Operators
Member Access Operators
Subscript Operator
Function Call Operator
Comma Operator
New and Delete Operators
Last updated