← Back to C++ Course | Chapter 8: OOP Core | Lesson 11 of 14

C++ Operator Overloading

Overloading Unary Operators

C++ lets you redefine how unary operators like '-' or '!' behave when applied to your own class's objects. A unary operator acts on a single operand, so overloading it lets expressions like -myVector or !isEmpty read naturally for user-defined types too.

Example: Overloading Unary Operators

cpp
#include <iostream>

class Vector2D {
public:
	int x, y;
	Vector2D operator-() {
		return Vector2D{-x, -y};
	}
};

int main() {
	Vector2D v{3, 4};
	Vector2D neg = -v;
	std::cout << neg.x << "," << neg.y << std::endl;
	return 0;
}

Overloading Binary Operators

Binary operators act on two operands, and overloading them — such as '+' or '-' — lets you write expressions like point1 + point2 that combine two objects of your class the same way you'd combine two built-in numbers, instead of calling an awkwardly-named method.

Example: Overloading Binary Operators

cpp
#include <iostream>

class Point {
public:
	int x, y;
	Point operator+(const Point &other) {
		return Point{x + other.x, y + other.y};
	}
};

int main() {
	Point p1{1, 2}, p2{3, 4};
	Point sum = p1 + p2;
	std::cout << sum.x << "," << sum.y << std::endl;
	return 0;
}

Overloading Relational Operators

Overloading relational operators like '==' or '<' lets you compare your own objects directly inside if-statements or use them with STL algorithms like sort, which rely on '<' to determine ordering between elements.

Example: Overloading Relational Operators

cpp
#include <iostream>

class Point {
public:
	int x;
	bool operator==(const Point &other) {
		return x == other.x;
	}
};

int main() {
	Point p1{5}, p2{5};
	std::cout << (p1 == p2) << std::endl;
	return 0;
}

Overloading with Friend Functions

Some operator overloads are written as non-member friend functions rather than member functions — this is required whenever the left-hand operand isn't an object of your class, such as when overloading '<<' for std::cout or when you want symmetric behavior on both sides of '+'.

Example: Overloading with Friend Functions

cpp
#include <iostream>

class Point {
public:
	int x;
	Point(int x) : x(x) {}
	friend Point operator+(int value, const Point &p);
};

Point operator+(int value, const Point &p) {
	return Point(value + p.x);
}

int main() {
	Point p(5);
	Point result = 10 + p;
	std::cout << result.x << std::endl;
	return 0;
}

Special Overloaded Operators

C++ also allows overloading special operators with unusual roles: the assignment operator '=' controls what happens on copy-assignment, and the function-call operator '()' lets an object be called like a function — the mechanism behind functors used throughout the STL.

Example: Special Overloaded Operators

cpp
#include <iostream>

class Multiplier {
public:
	int factor;
	int operator()(int x) {
		return x * factor;
	}
};

int main() {
	Multiplier triple{3};
	std::cout << triple(5) << std::endl;
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.