To be sure, let us understand the concept of operator overloading with the help of function overloading
Operator overloading means we can use an operator symbol instead of a function name to do a particular task
Function overloading means we can have multiple functions with the same name but operator overloading is a little bit different
But, How?
until now, okay,
in reality, the truth is, operators are designed to do a particular operation on an operand
to begin with, let’s deconstruct an example
for instance, well imagine this
this time, suppose we have a + operator
a + operator can have a unary + and binary +
the unary +(operator) can act on a single operand like +,-,++,–,!
in general, ex: +4 (one operand)
the binary +( operator) can act on two operands like +,-,! ,*
such as, ex: 8+4 (two operands)
That is to say, the barbaric problem with operators
All the operators are designed to work with predefined data types such as(int, float, char, and double)
Without doubt, with the help of the + operator, you can add two integers
such as int a=8, int b=9 ,total;
total= a+b;
But the eye-opening problem is that we can’t add objects like how we added the integers
but Why?
because objects are instances of a class and a class is a user-defined data type
that is, Obj1+obj2 ——->ERROR
Until now, clear as mud?
So, to avoid this problem, C++ has introduced the concept of operator overloading and with the help of operator overloading we can add two objects of user-defined data type
in reality, the + operator has to work with a predefined data type[int, float, char] only
But by using the operating overloading concept we can use the + operator to add two user-defined objects
such as, obj1+obj1
in sum, Pretty effective right?
We can overload all the C++ operators except some operators such as
- :: scope resolution operator
- . , .*dot , dot star(means member access operator of a class)
- Conditional operator(?:)
- Sizeof()
This time, let’s jump in to see the syntax
The syntax for function definition
Return_type function_name(argument_list) { }
The syntax for operator overloading
Return_type operator_symbol(argument_list) { }
To be sure, Can you identify the difference?
in short, like it or not, in place of the function name we exchanged it with the operator symbol(where the operator is a keyword and the symbol is an operator symbol)
until now, inspiring right?
with this in mind, let us, first, see a simple example to understand == operator overloading with the help of a function
in fact, here’s an example
this time, we will use a function called compare (test t2)
until now, okay,
so, to compare the value we need two values one is implicit(inside obj)[test class], and the other one is explicit(outside obj)[t2 object]
#include <iostream> using namespace std; class test { private: int a; // data item public: void getdata() //Member function to get a value { cin>>a; } void compare(test t2)//To compare the two values we need two arguments one is implicit (i.e( test) class name) and another one is explicit (i.e t2 obj) { if(a==t2.a) cout<<"object are equal"; else cout<<"object are not equal"; } }; int main() { test t1, t2; cout << "enter t1 object a value" << endl; t1.getdata(); cout << "enter t2 object a value" << endl; t2.getdata(); t1.compare(t2); return 0; }
Output
enter t1 object a value 5 enter t2 object a value 5 object are equal
Here’s another one
At this instant, we will replace the function compare name with the operator symbol ==
Rule: in fact, to overload the operator the left side value should be the object of the same class
For example, consider the below expression from below program
that is, t1==t2
to conclude, here, t1 is a left-side value(object of the same class test) and == is an equality operator
#include <iostream> using namespace std; class test { private: int a; // data item public: void getdata() //Member function to get a value { cin>>a; } void operator ==(test t2)//To compare the two values we need two arguments one is implicit (i.e( test) class name) and another one is explicit (i.e t2 obj) { if(a==t2.a) cout<<"object are equal"; else cout<<"object are not equal"; } }; int main() { test t1, t2; cout << "enter t1 object a value" << endl; t1.getdata(); cout << "enter t2 object a value" << endl; t2.getdata(); t1==t2; return 0; }
Output
enter t1 object a value 5 enter t2 object a value 5 object are equal
Overloading the unary operators
As we know, a unary operator will act on a single operand whereas the binary operator will act on two operands
When the binary operator is overloaded it requires two objects one is implicit(inside) and the other is explicit(outside) but for unary operator overloading we require only an implicit(inside) object
#include <iostream> using namespace std; class demo { private: int a; public: demo()//default constructor { a=0;// data item } void operator ++()//Increment operator overloading { ++a; } void operator --()//Decrement operator overloading { --a; } void showdata()//function { cout<<"a="<<a<<endl;; } }; int main() { //obj d is created so, that it will automatically be initialized with 0 because the constructor says so demo d; ++d; d.showdata(); --d; d.showdata(); }
Output
a=1 a=0
Leave a Reply