cpp
examples
examples.cpp⚙️cpp
/**
* Operator Overloading - Examples
* Compile: g++ -std=c++17 -Wall examples.cpp -o examples
*/
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
// ============================================================
// SECTION 1: ARITHMETIC OPERATORS
// ============================================================
class Vector2D {
double x, y;
public:
Vector2D(double x = 0, double y = 0) : x(x), y(y) {}
Vector2D operator+(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); }
Vector2D operator-(const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
Vector2D operator*(double s) const { return Vector2D(x * s, y * s); }
Vector2D operator-() const { return Vector2D(-x, -y); }
Vector2D& operator+=(const Vector2D& v) { x += v.x; y += v.y; return *this; }
friend ostream& operator<<(ostream& os, const Vector2D& v) {
return os << "(" << v.x << ", " << v.y << ")";
}
};
void demoArithmetic() {
cout << "--- Arithmetic Operators ---\n" << endl;
Vector2D a(3, 4), b(1, 2);
cout << "a = " << a << ", b = " << b << endl;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * 2 = " << (a * 2) << endl;
cout << "-a = " << (-a) << endl;
a += b;
cout << "a += b: " << a << endl;
}
// ============================================================
// SECTION 2: COMPARISON OPERATORS
// ============================================================
class Date {
int year, month, day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
bool operator==(const Date& d) const {
return year == d.year && month == d.month && day == d.day;
}
bool operator!=(const Date& d) const { return !(*this == d); }
bool operator<(const Date& d) const {
if (year != d.year) return year < d.year;
if (month != d.month) return month < d.month;
return day < d.day;
}
friend ostream& operator<<(ostream& os, const Date& d) {
return os << d.year << "-" << d.month << "-" << d.day;
}
};
void demoComparison() {
cout << "\n--- Comparison Operators ---\n" << endl;
Date d1(2024, 1, 15), d2(2024, 6, 20), d3(2024, 1, 15);
cout << d1 << " == " << d3 << ": " << (d1 == d3 ? "true" : "false") << endl;
cout << d1 << " < " << d2 << ": " << (d1 < d2 ? "true" : "false") << endl;
cout << d1 << " != " << d2 << ": " << (d1 != d2 ? "true" : "false") << endl;
}
// ============================================================
// SECTION 3: INCREMENT/DECREMENT
// ============================================================
class Counter {
int value;
public:
Counter(int v = 0) : value(v) {}
Counter& operator++() { ++value; return *this; } // Pre
Counter operator++(int) { Counter t = *this; ++value; return t; } // Post
Counter& operator--() { --value; return *this; }
friend ostream& operator<<(ostream& os, const Counter& c) {
return os << c.value;
}
};
void demoIncrement() {
cout << "\n--- Increment/Decrement ---\n" << endl;
Counter c(5);
cout << "c = " << c << endl;
cout << "++c = " << (++c) << endl;
cout << "c++ = " << (c++) << endl;
cout << "c = " << c << endl;
}
// ============================================================
// SECTION 4: STREAM OPERATORS
// ============================================================
class Point {
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
friend ostream& operator<<(ostream& os, const Point& p) {
return os << "Point(" << p.x << ", " << p.y << ")";
}
friend istream& operator>>(istream& is, Point& p) {
return is >> p.x >> p.y;
}
};
void demoStream() {
cout << "\n--- Stream Operators ---\n" << endl;
Point p(10, 20);
cout << "p = " << p << endl;
// cout << "Enter x y: "; cin >> p; cout << "You entered: " << p;
}
// ============================================================
// SECTION 5: ASSIGNMENT OPERATOR
// ============================================================
class MyString {
char* data;
size_t len;
public:
MyString(const char* s = "") : len(strlen(s)) {
data = new char[len + 1];
strcpy(data, s);
}
~MyString() { delete[] data; }
// Copy constructor
MyString(const MyString& other) : len(other.len) {
data = new char[len + 1];
strcpy(data, other.data);
}
// Copy assignment
MyString& operator=(const MyString& other) {
if (this != &other) {
delete[] data;
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
return *this;
}
// Move assignment
MyString& operator=(MyString&& other) noexcept {
if (this != &other) {
delete[] data;
data = other.data;
len = other.len;
other.data = nullptr;
other.len = 0;
}
return *this;
}
friend ostream& operator<<(ostream& os, const MyString& s) {
return os << (s.data ? s.data : "");
}
};
void demoAssignment() {
cout << "\n--- Assignment Operator ---\n" << endl;
MyString s1("Hello");
MyString s2("World");
cout << "s1 = " << s1 << ", s2 = " << s2 << endl;
s2 = s1; // Copy assignment
cout << "After s2 = s1: s2 = " << s2 << endl;
s2 = MyString("New"); // Move assignment
cout << "After move: s2 = " << s2 << endl;
}
// ============================================================
// SECTION 6: SUBSCRIPT OPERATOR
// ============================================================
class IntArray {
int* data;
size_t size;
public:
IntArray(size_t n) : data(new int[n]()), size(n) {}
~IntArray() { delete[] data; }
int& operator[](size_t i) { return data[i]; }
const int& operator[](size_t i) const { return data[i]; }
size_t getSize() const { return size; }
};
void demoSubscript() {
cout << "\n--- Subscript Operator ---\n" << endl;
IntArray arr(5);
for (size_t i = 0; i < arr.getSize(); ++i) arr[i] = i * 10;
cout << "Array: ";
for (size_t i = 0; i < arr.getSize(); ++i) cout << arr[i] << " ";
cout << endl;
}
// ============================================================
// SECTION 7: FUNCTION CALL OPERATOR
// ============================================================
class Multiplier {
int factor;
public:
Multiplier(int f) : factor(f) {}
int operator()(int x) const { return x * factor; }
};
class Adder {
int value;
public:
Adder(int v) : value(v) {}
int operator()(int x) const { return x + value; }
int operator()(int a, int b) const { return a + b + value; }
};
void demoFunctionCall() {
cout << "\n--- Function Call Operator ---\n" << endl;
Multiplier times3(3);
Adder add10(10);
cout << "times3(5) = " << times3(5) << endl;
cout << "add10(7) = " << add10(7) << endl;
cout << "add10(2, 3) = " << add10(2, 3) << endl;
}
// ============================================================
// SECTION 8: FRIEND FOR COMMUTATIVE OPS
// ============================================================
class Complex {
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator*(double d) const { return Complex(real * d, imag * d); }
// For: double * Complex
friend Complex operator*(double d, const Complex& c) {
return Complex(d * c.real, d * c.imag);
}
friend ostream& operator<<(ostream& os, const Complex& c) {
return os << c.real << "+" << c.imag << "i";
}
};
void demoFriend() {
cout << "\n--- Friend for Commutative ---\n" << endl;
Complex c(3, 4);
cout << "c = " << c << endl;
cout << "c * 2 = " << (c * 2) << endl;
cout << "2 * c = " << (2 * c) << endl;
}
// ============================================================
// MAIN
// ============================================================
int main() {
cout << "╔══════════════════════════════════════╗" << endl;
cout << "║ OPERATOR OVERLOADING - EXAMPLES ║" << endl;
cout << "╚══════════════════════════════════════╝" << endl;
demoArithmetic();
demoComparison();
demoIncrement();
demoStream();
demoAssignment();
demoSubscript();
demoFunctionCall();
demoFriend();
cout << "\n=== Complete ===" << endl;
return 0;
}