In C + +, we can make operators work for user-defined classes. This means that C + + can provide operators with special meaning for data types, which is called operator overloading. For example, we can overload the operator "+" in a class like String, so that we can connect two strings by using +. Other example classes that may overload arithmetic operators are complex numbers, decimals, large integers, and so on.
A simple and complete example
#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0) {real = r; imag = i;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << '\n'; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); }
Output:
12 + i9
What is the difference between operator functions and ordinary functions?
Operator functions are the same as normal functions. The only difference is that the name of the operator function is always the operator keyword followed by the operator symbol, and the operator function is called when the corresponding operator is used.
The following is an example of a global operator function.
#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0) {real = r; imag = i;} void print() { cout << real << " + i" << imag << '\n'; } // The global operator function is made friend of this class so // that it can access private members friend Complex operator + (Complex const &, Complex const &); }; Complex operator + (Complex const &c1, Complex const &c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); return 0; }
output
12 + i9
Can we overload all operators?
Except for a few, almost all operators can be overloaded. The following is a list of operators that cannot be overloaded.
. (spot) :: ?: sizeof
Why not. (dot), ::, ?: And sizeof are overloaded?
see also this Get Stroustrup's own answer.
Key points about operator overloading
**1) * * for operator overloading to work, at least one of the operands must be a user-defined class object.
2) * * assignment operator: * * the compiler automatically creates a default assignment operator for each class. The default assignment operator does assign all members on the right to the left, and works in most cases (the same behavior as the copy constructor). For more details, see this Content.
3) * * conversion operators: * * we can also write conversion operators to convert one type to another.
#include <iostream> using namespace std; class Fraction { private: int num, den; public: Fraction(int n, int d) { num = n; den = d; } // Conversion operator: return float value of fraction operator float() const { return float(num) / float(den); } }; int main() { Fraction f(2, 5); float val = f; cout << val << '\n'; return 0; }
Output:
0.4
Overloaded conversion operator must be a member method. Other operators can be member methods or global methods.
**4) * * any constructor that can be called with a single parameter can be used as a conversion constructor, which means it can also be used to implicitly convert to the class being constructed.
- CPP
#include <iostream> using namespace std; class Point { private: int x, y; public: Point(int i = 0, int j = 0) { x = i; y = j; } void print() { cout << "x = " << x << ", y = " << y << '\n'; } }; int main() { Point t(20, 20); t.print(); t = 30; // Member x of t becomes 30 t.print(); return 0; }
Output:
x = 20, y = 20 x = 30, y = 0
We will soon discuss some important operator overloading, such as new, delete, comma, function call, arrow, etc