Some of the most commonly used operators in C++ are the arithmetic operators -- that is, the plus operator (+), minus operator (-), multiplication operator (*), and division operator (/). Note that all of the arithmetic operators are binary operators -- meaning they take two operands -- one on each side of the operator. All four of these operators are overloaded in the exact same way.
Overloading operators using friend functions
When the operator does not modify its operands, the best way to overload the operator is via friend function. None of the arithmetic operators modify their operands (they just produce and return a result), so we will utilize the friend function overloaded operator method here.
The following example shows how to overload operator plus (+) in order to add two “Cents” objects together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Cents { private: int m_nCents; public: Cents(int nCents) { m_nCents = nCents; } // Add Cents + Cents friend Cents operator+(const Cents &c1, const Cents &c2); int GetCents() { return m_nCents; } }; // note: this function is not a member function! Cents operator+(const Cents &c1, const Cents &c2) { // use the Cents constructor and operator+(int, int) return Cents(c1.m_nCents + c2.m_nCents); } int main() { Cents cCents1(6); Cents cCents2(8); Cents cCentsSum = cCents1 + cCents2; std::cout << "I have " << cCentsSum .GetCents() << " cents." << std::endl; return 0; } |
This produces the result:
I have 14 cents.
Overloading the plus operator (+) is as simple as declaring a function named operator+, giving it two parameters of the type of the operands we want to add, picking an appropriate return type, and then writing the function.
In the case of our Cents object, implementing our operator+() function is very simple. First, the parameter types: in this version of operator+, we are going to add two Cents objects together, so our function will take two objects of type Cents. Second, the return type: our operator+ is going to return a result of type Cents, so that’s our return type.
Finally, implementation: to add two Cents objects together, we really need to add the m_nCents member from each Cents object. Because our overloaded operator+() function is a friend of the class, we can access the m_nCents member of our parameters directly. Also, because m_nCents is an integer, and C++ knows how to add integers together using the built-in version of the plus operator that works with integer operands, we can simply use the + operator to do the adding.
Overloading the subtraction operator (-) is simple as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Cents { private: int m_nCents; public: Cents(int nCents) { m_nCents = nCents; } // overload Cents + Cents friend Cents operator+(const Cents &c1, const Cents &c2); // overload Cents - Cents friend Cents operator-(const Cents &c1, const Cents &c2); int GetCents() { return m_nCents; } }; // note: this function is not a member function! Cents operator+(const Cents &c1, const Cents &c2) { // use the Cents constructor and operator+(int, int) return Cents(c1.m_nCents + c2.m_nCents); } // note: this function is not a member function! Cents operator-(const Cents &c1, const Cents &c2) { // use the Cents constructor and operator-(int, int) return Cents(c1.m_nCents - c2.m_nCents); } |
Overloading the multiplication operator (*) and division operator (/) are as easy as defining functions for operator* and operator/.
Overloading operators for operands of different types
Often it is the case that you want your overloaded operators to work with operands that are different types. For example, if we have Cents(4), we may want to add the integer 6 to this to produce the result Cents(10).
When C++ evaluates the expression x + y
, x becomes the first parameter, and y becomes the second parameter. When x and y have the same type, it does not matter if you add x + y or y + x -- either way, the same version of operator+ gets called. However, when the operands have different types, x + y is not the same as y + x.
For example, Cents(4) + 6
would call operator+(Cents, int), and 6 + Cents(4)
would call operator+(int, Cents). Consequently, whenever we overload binary operators for operands of different types, we actually need to write two functions -- one for each case. Here is an example of that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class Cents { private: int m_nCents; public: Cents(int nCents) { m_nCents = nCents; } // Overload cCents + int friend Cents operator+(const Cents &cCents, int nCents); // Overload int + cCents friend Cents operator+(int nCents, const Cents &cCents); int GetCents() { return m_nCents; } }; // note: this function is not a member function! Cents operator+(const Cents &cCents, int nCents) { return Cents(cCents.m_nCents + nCents); } // note: this function is not a member function! Cents operator+(int nCents, const Cents &cCents) { return Cents(cCents.m_nCents + nCents); } int main() { Cents c1 = Cents(4) + 6; Cents c2 = 6 + Cents(4); std::cout << "I have " << c1.GetCents() << " cents." << std::endl; std::cout << "I have " << c2.GetCents() << " cents." << std::endl; return 0; } |
Note that both overloaded functions have the same implementation -- that’s because they do the same thing, they just take their parameters in a different order.
Another example
Let’s take a look at another example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
class MinMax { private: int m_nMin; // The min value seen so far int m_nMax; // The max value seen so far public: MinMax(int nMin, int nMax) { m_nMin = nMin; m_nMax = nMax; } int GetMin() { return m_nMin; } int GetMax() { return m_nMax; } friend MinMax operator+(const MinMax &cM1, const MinMax &cM2); friend MinMax operator+(const MinMax &cM, int nValue); friend MinMax operator+(int nValue, const MinMax &cM); }; MinMax operator+(const MinMax &cM1, const MinMax &cM2) { // Get the minimum value seen in cM1 and cM2 int nMin = cM1.m_nMin < cM2.m_nMin ? cM1.m_nMin : cM2.m_nMin; // Get the maximum value seen in cM1 and cM2 int nMax = cM1.m_nMax > cM2.m_nMax ? cM1.m_nMax : cM2.m_nMax; return MinMax(nMin, nMax); } MinMax operator+(const MinMax &cM, int nValue) { // Get the minimum value seen in cM and nValue int nMin = cM.m_nMin < nValue ? cM.m_nMin : nValue; // Get the maximum value seen in cM and nValue int nMax = cM.m_nMax > nValue ? cM.m_nMax : nValue; return MinMax(nMin, nMax); } MinMax operator+(int nValue, const MinMax &cM) { // call operator+(MinMax, nValue) return (cM + nValue); } int main() { MinMax cM1(10, 15); MinMax cM2(8, 11); MinMax cM3(3, 12); MinMax cMFinal = cM1 + cM2 + 5 + 8 + cM3 + 16; std::cout << "Result: (" << cMFinal.GetMin() << ", " << cMFinal.GetMax() << ")" << std::endl; return 0; } |
The MinMax class keeps track of the minimum and maximum values that it has seen so far. We have overloaded the + operator 3 times, so that we can add two MinMax objects together, or add integers to MinMax objects.
This example produces the result:
Result: (3, 16)
which you will note is the minimum and maximum values that we added to cMFinal.
One other interesting thing to note is that we defined operator+(int, MinMax) by calling operator+(MinMax, int). This is slightly less efficient than implementing it directly (due to the extra function call), but keeps our code shorter and easier to maintain (because it reduces duplicate code). It is often possible to define overloaded operators by calling other overloaded operators -- when possible, do so!
9.3 -- Overloading the I/O operators ^{[1]} |
Index ^{[2]} |
9.1 -- Introduction to operator overloading ^{[3]} |