C++ operator Keyword
The operator
keyword in C++ is used to define or overload operators for user-defined data types. Operator overloading allows you to redefine the behavior of operators (such as +
, -
, *
, etc.) when they are applied to objects of a class or structure.
By overloading operators, you can make user-defined types behave like built-in types, improving code readability and usability.
Syntax
</>
Copy
return_type operator symbol(parameters) {
// Operator functionality
}
- return_type
- The type of value returned by the overloaded operator.
- operator
- The keyword used to define or overload an operator.
- symbol
- The operator to be overloaded (e.g.,
+
,-
,*
, etc.). - parameters
- The operands for the operator. The number of parameters depends on the operator being overloaded.
Examples
Example 1: Overloading the +
Operator
In this example, you will learn how to overload the +
operator for a class.
</>
Copy
#include <iostream>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overload the + operator
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2.5, 3.5), c2(1.5, 2.5);
Complex c3 = c1 + c2;
cout << "Result: ";
c3.display();
return 0;
}
Output:
Result: 4 + 6i
Explanation:
- The
Complex
class has two private members:real
andimag
, representing the real and imaginary parts of a complex number. - The
operator+
function is overloaded to add two complex numbers by summing their real and imaginary parts separately. - In the
main
function, two complex numbersc1
andc2
are added using the overloaded+
operator, and the result is displayed.
Example 2: Overloading the ==
Operator
In this example, you will learn how to overload the ==
operator to compare two objects.
</>
Copy
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
// Overload the == operator
bool operator==(const Point& other) const {
return (x == other.x && y == other.y);
}
void display() const {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(3, 4), p2(3, 4), p3(5, 6);
if (p1 == p2) {
cout << "p1 and p2 are equal." << endl;
} else {
cout << "p1 and p2 are not equal." << endl;
}
if (p1 == p3) {
cout << "p1 and p3 are equal." << endl;
} else {
cout << "p1 and p3 are not equal." << endl;
}
return 0;
}
Output:
p1 and p2 are equal.
p1 and p3 are not equal.
Explanation:
- The
Point
class represents a point in 2D space with coordinatesx
andy
. - The
operator==
function is overloaded to compare two points by checking if theirx
andy
coordinates are equal. - The
if
conditions inmain
test the equality of points and print appropriate messages.
Key Points about operator
Keyword
- The
operator
keyword is used to overload operators for user-defined data types. - Not all operators can be overloaded (e.g.,
::
,sizeof
, and?:
cannot be overloaded). - Overloading allows custom behavior for operators when applied to objects of a class or structure.
- When overloading an operator, at least one operand must be a user-defined type.