Operator overloading is one of the important feature in c++. For operator overloading there are certain restriction and limitations such as:
- Only existing operator can be overloaded new operator can not be created.
- The overloaded operator have at least one operand that is of user define type.
- We can not change the basic meaning of operator.
- Overloaded operator follow the syntax rule of original operator that can not be violated.
- Unary operator overloaded by means of member function, take no explicit argument and return no explicit value.But those operator overloaded by means of friend function take one reference argument i.e the object of relevant class.
- Binary operator overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit argument.
- When using binary operator overloaded through a member function the left hand operand must be an object of relevant class.
- Binary arithmetic operator such as '+' , '-' , '*' , '/' must explicitly to change their own arguments.
- There are some operator that can not be overloaded such as
- size of operator ( sizeof )
- Membership operator ( . )
- Pointer to membership operator ( * )
- Scope resolution operator (: :)
- Conditional Operator ( ? : )
- We can not use friend function to overload certain operator.
- Assignment operator ( = )
- Function call operator ( )
- Sub scripting operator ( [ ] )
- Class member access operator ( -> )
Unary Operator Overloading
Before understanding unary operator overloading let we discuss about unary operators.
Unary operators operates on a single operand to produce new value.
Unary operators operates on a single operand to produce new value.
Type Unary Operator
- Increment (+ +)
- Decrement (- -)
- Not ( ! )
- Unary minus ( - )
- Sizeof operator (sizeof( ) )
- Addressof operator ( & )
Example
#include <iostream>
using namespace std;
class unary
{
int a;
public:
unary operator -- ()
{
a = - a;
}
void read()
{
cout<<"Enter Number:";
cin>>a;
}
void print()
{
cout<<"a="<<a<<"\n";
}
};
int main()
{
unary u1;
u1.read();
--u1;
u1.print();
return 0;
}
Output:
Enter Number:5
a=-5
Binary Operator Overloading
Before understanding binary operator overloading let we discuss about binary operators.
Binary operators operates on two operand to produce new value.
Binary operators operates on two operand to produce new value.
Type Binary Operator
- Equal (= =)
- Not equal to (! =)
- Less than ( < )
- Greater than ( > )
- Less than equal to (< =)
- Greater than equal to (> =)
- Logical AND ( && )
- Logical OR (| |)
- Plus ( + )
- Minus ( - )
- Multiplication ( * )
- Division ( / )
Example
#include <iostream>
using namespace std;
class binary
{
int a,b;
public:
binary operator + (binary ob)
{
a=a+ob.a;
b=b+ob.b;
}
void read()
{
cout<<"Enter real:";
cin>>a;
cout<<"Enter imagenary:";
cin>>b;
}
void print()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
int main()
{
binary b1,b2,b3;
b1.read();
b1.print();
b2.read();
b2.print();
b3=b1-b2;
b1.print();
return 0;
}
Output:
Enter real:2
Enter imagenary:3
2+3i
Enter real:5
Enter imagenary:6
5+6i
7+9i