Operator Overloading in c++

Operator overloading is an important theme in c++. It's a very useful mechanism that makes the code of the program be more understandable. To see the basic examples of the overloading we will create our own class, called MyIntDemo. Initially, the class is very simple(I'm not separating the code into header and source file just for clarity):
  1. #ifndef MYINTDEMO_H
  2. #define MYINTDEMO_H
  3.  
  4.  
  5. class MyIntDemo
  6. {
  7.     public:
  8.         MyIntDemo()
  9.         {
  10.         }
  11.         MyIntDemo(int val)
  12.         {
  13.             myIntValue = val;
  14.         }
  15.  
  16.     private:
  17.         int myIntValue;
  18. };
  19.  
  20. #endif // MYINTDEMO_H
The first step will be the overloading of the unary prefix operators such as ++ and -- :
  1. friend const MyIntDemo& operator++(MyIntDemo& obj)
  2.     {
  3.         obj.myIntValue++;
  4.         return obj.myIntValue;
  5.     }
  6.     friend const MyIntDemo& operator--(MyIntDemo& obj)
  7.     {
  8.         obj.myIntValue--;
  9.         return obj.myIntValue;
  10.     }
These operators should be overridden as friend functions. The next step is to override the binary operations. I'll show you the example with overriding operators +, += and ==. The other binary operators can be easily overridden in the same way:
  1.  friend const MyIntDemo operator+(const MyIntDemo& left, const MyIntDemo& right)
  2.     {
  3.         return MyIntDemo(left.myIntValue + right.myIntValue);
  4.     }
  5.     friend MyIntDemo& operator+=(MyIntDemo& left, const MyIntDemo& right)
  6.     {
  7.         left.myIntValue += right.myIntValue;
  8.         return left;
  9.     }
  10.     friend bool operator==(const MyIntDemo& left, const MyIntDemo& right)
  11.     {
  12.         return left.myIntValue == right.myIntValue;
  13.     }
Now, you can see, that the basic arithmetical operators are overridden. All of the are overridden as friend functions. Using this sample code you can override subtraction, multiplication and division operators. After this, we can overload input and output operators. For this scope we need to include iostream library in the class file:
  1. #include <iostream>
  2. using namespace std;
The next line after include statement is used to specify the used namespace, called "std". The overridden input and output operators look in this way:
  1. friend ostream& operator<< (ostream& os, const MyIntDemo& val)
  2.     {
  3.         os << val.myIntValue;
  4.         return os;
  5.     }
  6. friend istream& operator>> (istream& is, MyIntDemo& val)
  7.     {
  8.         is >> val.myIntValue;
  9.         return is;
  10. }
An important note is that you need to use const arguments n all cases, when your operator doesn't modify the object. This operators are tested in a simple program, that work with the custom created class as with the simple int type:
  1. #include "MyIntDemo.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     MyIntDemo someNumber;
  7.     cout << "Please, enter an integer value" << endl;
  8.     cin >> someNumber;
  9.     cout << "You entered " << someNumber << endl;
  10.     ++someNumber;
  11.     cout << "Incremented value " << someNumber << endl;
  12.     MyIntDemo five = 5;
  13.     MyIntDemo res = five + someNumber;
  14.     cout << five << " + " << someNumber << " = " << res << endl;
  15.     bool eq = five == someNumber;
  16.  
  17.  
  18.     cout << five << " = " << someNumber << " ? " << eq << endl;
  19.     five += someNumber;
  20.     cout << "Now five is " << five<< endl;
  21.     return 0;
  22. }
The output of this code is next: Please, enter an integer value 10 You entered 10 Incremented value 11 5 + 11 = 16 5 = 11 ? 0 Now five is 16 Process returned 0 (0x0) execution time : 2.725 s Press any key to continue. In conclusion, I would like to say, that this example is really simple, but the overriding mechanism looks similar to this example even for a complex example. In this tutorial you can find the sample codeblocks project with source code. In case of any questions, you can ask me in comments and you will get a response.

Add new comment