Operator Overloading is generalization of function overloading. It increases the readability of our code. Further, it is straightforward and natural way to extend C++.
General form of operator overloading:
Return type may be whatever the operator returns including a reference to the object of the operand. Operator symbol may be any overloadable operator from the list.
Overloading Unary Operators
Unary Operators can overload as
- Non-static member function with no arguments.
- As a global function with one argument. Argument must be class object or reference to class object.
Why non-static?
- static functions only access static data
- Not what is needed for operator functions
Overloading ++ and –
- Pre/post-incrementing/decrementing operators can be overloaded
- How does the compiler distinguish between the two?
Prefix versions overloaded same as any other prefix unary operator would be. i.e. d1.operator++(); for ++d1;
Postfix versions can be overloaded with integer argument. Prototype: Date::operator++( int ); For d1++;
The following program is example for unary operator overloading.
//example //overloading unary operator for prefix and postfix #include<iostream> using namespace std; class Counter { private: unsigned int count; public: Counter() // constructor { count = 0; } int get_count() const { return count; } Counter operator ++ () // increment (prefix) { ++count; return *this; } Counter operator ++ (int) //increment(postfix) { Counter tmp=*this; //save old value ++count; //update value r return tmp; //return original } }; int main() { Counter c1, c2,c3; // define and initialize ++c1; // increment c1 ++c2; // increment c2 c3=c2++; // increment c2 cout << "\n c1=" << c1.get_count(); // displays 1 cout << "\n c2=" << c2.get_count(); // displays 2 cout << "\n c3=" << c3.get_count(); //displays 1; assigned before increase }
The output of the program is:
c1 = 1 c2 = 2 c3 = 1
Overloading Binary Operator
Binary operators can be overloaded just like unary operators. Arithmetic operators, comparison operators, and arithmetic assignment operators can be overloaded.
If binary operator is overloaded with a non-static member function, it needs one argument.
Syntax:
class class_name { public: return_type operator operator_symbol (class_name args); //… }; return_type class_name::operator operator_symbol(class_name args) { //… }
Here is example program that adds two complex numbers.
//overloading binary operator #include<iostream> using namespace std; class complex { private: float real, imag; public: complex () {} complex (float r, float i) { real=r; imag=i; } void display() { cout<<"("<<real<<","<<imag<<")"; } complex operator + (complex); }; complex complex:: operator + (complex c) { complex temp; temp.real= real +c.real; temp.imag=imag +c. imag; return temp; } int main() { complex c1(4.4,3.6), c2(1.8,1.06), c3; c3=c1+c2; c1.display(); cout<<" + "; c2.display(); cout<< " = "; c3.display(); }
The output of the program is:
(4.4, 3.6) + (1.8, 1.06) = ( 6.2, 4.66)
If binary operator is overloaded with a global function, then it requires with two arguments: One argument must be class object or reference to a class object.
Syntax:
class class_name { public: return_type operator operator_symbol (class_name args); //… }; return_type class_name::operator operator_symbol(class_name obj1,class_name obj2) { //… }