Operator overloading is a powerful feature in C++ that allows you to redefine the behavior of operators for user-defined types. By overloading operators, you can make your custom classes behave like built-in types, providing intuitive and natural syntax for operations.
Unary operators operate on a single operand. Examples include increment (++), decrement (–), logical NOT (!), and unary minus (-).
Unary operators can be overloaded as member functions or as standalone functions. Let’s explore an example of overloading the unary minus (-) operator for a custom class Vector
.
#include
class Vector {
private:
int x, y;
public:
// Constructor to initialize the object's value
Vector(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
// Overloading the unary minus operator
Vector operator-() const {
return Vector(-x, -y);
}
// Display function to print the vector
void display() const {
std::cout << "(" << x << ", " << y << ")" << std::endl;
}
};
int main() {
Vector v1(3, 4);
// Using the overloaded unary minus operator
Vector v2 = -v1;
// Displaying the result
std::cout << "Original Vector: ";
v1.display(); // Output: (3, 4)
std::cout << "Negated Vector: ";
v2.display(); // Output: (-3, -4)
return 0;
}
Vector
representing a 2D vector with x
and y
components.Vector
class.x
and y
components of the vector.main()
function, we create a Vector
object v1
with components (3, 4).v1
, resulting in a new Vector
object v2
.Binary operators operate on two operands. Examples include addition (+), subtraction (–), multiplication (*), and division (/).
Binary operators can also be overloaded as member functions or as standalone functions. Let’s explore an example of overloading the addition (+) operator for the Vector
class.
#include
class Vector {
private:
int x, y;
public:
// Default constructor
Vector() {
x = 0;
y = 0;
}
// Parameterized constructor
Vector(int x, int y) {
this->x = x;
this->y = y;
}
// Overloading the addition operator
Vector operator+(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}
// Display function to print the vector
void display() const {
std::cout << "(" << x << ", " << y << ")" << std::endl;
}
};
int main() {
Vector v1(3, 4);
Vector v2(2, 5);
// Using the overloaded addition operator
Vector v3 = v1 + v2;
// Displaying the result
std::cout << "Vector 1: ";
v1.display(); // Output: (3, 4)
std::cout << "Vector 2: ";
v2.display(); // Output: (2, 5)
std::cout << "Sum: ";
v3.display(); // Output: (5, 9)
return 0;
}
Vector
class representing a 2D vector.Vector
class.main()
function, we create two Vector
objects v1
and v2
with components (3, 4) and (2, 5) respectively.v1
and v2
, resulting in a new Vector
object v3
.In this chapter, we explored the concept of overloading unary and binary operators in C++. We learned how to redefine the behavior of operators for user-defined types, enhancing the expressiveness and readability of our code. By overloading operators, we can create more intuitive and natural syntax for operations on custom classes, making our code more powerful and flexible.Happy coding !❤️