Operator Overloading in c++
Submitted by pavel7_7_7 on Friday, August 29, 2014 - 04:09.
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):
The first step will be the overloading of the unary prefix operators such as ++ and -- :
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
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:
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:
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
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.
- #ifndef MYINTDEMO_H
- #define MYINTDEMO_H
- class MyIntDemo
- {
- public:
- MyIntDemo()
- {
- }
- MyIntDemo(int val)
- {
- myIntValue = val;
- }
- private:
- int myIntValue;
- };
- #endif // MYINTDEMO_H
- friend const MyIntDemo& operator++(MyIntDemo& obj)
- {
- obj.myIntValue++;
- return obj.myIntValue;
- }
- friend const MyIntDemo& operator--(MyIntDemo& obj)
- {
- obj.myIntValue--;
- return obj.myIntValue;
- }
+
, +=
and ==
. The other binary operators can be easily overridden in the same way:
- friend const MyIntDemo operator+(const MyIntDemo& left, const MyIntDemo& right)
- {
- return MyIntDemo(left.myIntValue + right.myIntValue);
- }
- friend MyIntDemo& operator+=(MyIntDemo& left, const MyIntDemo& right)
- {
- left.myIntValue += right.myIntValue;
- return left;
- }
- friend bool operator==(const MyIntDemo& left, const MyIntDemo& right)
- {
- return left.myIntValue == right.myIntValue;
- }
- #include <iostream>
- using namespace std;
- friend ostream& operator<< (ostream& os, const MyIntDemo& val)
- {
- os << val.myIntValue;
- return os;
- }
- friend istream& operator>> (istream& is, MyIntDemo& val)
- {
- is >> val.myIntValue;
- return is;
- }
int
type:
- #include "MyIntDemo.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- MyIntDemo someNumber;
- cout << "Please, enter an integer value" << endl;
- cin >> someNumber;
- cout << "You entered " << someNumber << endl;
- ++someNumber;
- cout << "Incremented value " << someNumber << endl;
- MyIntDemo five = 5;
- MyIntDemo res = five + someNumber;
- cout << five << " + " << someNumber << " = " << res << endl;
- bool eq = five == someNumber;
- cout << five << " = " << someNumber << " ? " << eq << endl;
- five += someNumber;
- cout << "Now five is " << five<< endl;
- return 0;
- }
Add new comment
- 4 views