Learn C++ - C++ Operator Overloading






To overload an operator, you use a special function called an operator function.

An operator function has the following form, where op is the symbol for the operator being overloaded:

operator op (argument-list)

For example, operator+() overloads the + operator and operator*() overloads the * operator.

The op has to be a valid C++ operator.

The following table lists Operators That Can Be Overloaded.

+              -              *              /                   %               ^ 
&              |              ~              !                   =               < 
>              +=             -=             *=                  /=              %= 
^=             &=             |=             <<                  >>              >>= 
<<=            ==             !=             <=                  >=              && 
||             ++             --             ,                   ->*             -> 
()             []             new            delete              new []          delete [] 





Adding an Addition Operator

The following code converts the Time class to using an overloaded addition operator.


#include <iostream> 
class Time { /*from   w  w w  . j  a va2 s . co  m*/
private: 
     int hours; 
     int minutes; 
public: 
     Time(); 
     Time(int h, int m = 0); 
     void AddMin(int m); 
     void AddHr(int h); 
     void Reset(int h = 0, int m = 0); 
     Time operator+(const Time & t) const; 
     void Show() const; 
}; 
Time::Time() 
{ 
   hours = minutes = 0; 
} 
Time::Time(int h, int m ) 
{ 
    hours = h; 
    minutes = m; 
} 

void Time::AddMin(int m) 
{ 
    minutes += m; 
    hours += minutes / 60; 
    minutes %= 60; 
} 

void Time::AddHr(int h) 
{ 
    hours += h; 
} 

void Time::Reset(int h, int m) 
{ 
    hours = h; 
    minutes = m; 
} 

Time Time::operator+(const Time & t) const 
{ 
    Time sum; 
    sum.minutes = minutes + t.minutes; 
    sum.hours = hours + t.hours + sum.minutes / 60; 
    sum.minutes %= 60; 
    return sum; 
} 

void Time::Show() const 
{ 
    std::cout << hours << " hours, " << minutes << " minutes"; 
} 

int main() 
{ 
     using std::cout; 
     using std::endl; 
     Time planning; 
     Time coding(2, 40); 
     Time fixing(5, 55); 
     Time total; 

     cout << "planning time = "; 
     planning.Show(); 
     cout << endl; 

     cout << "coding time = "; 
     coding.Show(); 
     cout << endl; 

     cout << "fixing time = "; 
     fixing.Show(); 
     cout << endl; 

     total = coding + fixing; 
     // operator notation 
     cout << "coding + fixing = "; 
     total.Show(); 
     cout << endl; 

     Time morefixing(3, 28); 
     cout << "more fixing time = "; 
     morefixing.Show(); 
     cout << endl; 
     total = morefixing.operator+(total); 
               // function notation 
     cout << "morefixing.operator+(total) = "; 
     total.Show(); 
     cout << endl; 

     return 0; 
} 

The code above generates the following result.





More Overloaded Operators

The following code shows how to overload the subtraction and multiplication operators.


#include <iostream> 
class Time { /*from   ww w. j  a v a2s  .c o  m*/
private: 
     int hours; 
     int minutes; 
public: 
     Time(); 
     Time(int h, int m = 0); 
     void AddMin(int m); 
     void AddHr(int h); 
     void Reset(int h = 0, int m = 0); 
     Time operator+(const Time & t) const; 
     Time operator-(const Time & t) const; 
     Time operator*(double n) const; 
     void Show() const; 
}; 
Time::Time() { 
     hours = minutes = 0; 
} 

Time::Time(int h, int m ) 
{ 
     hours = h; 
     minutes = m; 
} 

void Time::AddMin(int m) 
{ 
    minutes += m; 
    hours += minutes / 60; 
    minutes %= 60; 
} 
void Time::AddHr(int h) 
{ 
    hours += h; 
} 

void Time::Reset(int h, int m) 
{ 
    hours = h; 
    minutes = m; 
} 

Time Time::operator+(const Time & t) const 
{ 
    Time sum; 
    sum.minutes = minutes + t.minutes; 
    sum.hours = hours + t.hours + sum.minutes / 60; 
    sum.minutes %= 60; 
    return sum; 
} 

Time Time::operator-(const Time & t) const 
{ 
    Time diff; 
    int tot1, tot2; 
    tot1 = t.minutes + 60 * t.hours; 
    tot2 = minutes + 60 * hours; 
    diff.minutes = (tot2 - tot1) % 60; 
    diff.hours = (tot2 - tot1) / 60; 
    return diff; 
} 

Time Time::operator*(double mult) const 
{ 
    Time result; 
     long totalminutes = hours * mult * 60 + minutes * mult; 
    result.hours = totalminutes / 60; 
    result.minutes = totalminutes % 60; 
    return result; 
} 

void Time::Show() const 
{ 
    std::cout << hours << " hours, " << minutes << " minutes"; 
} 

int main() 
{ 
     using std::cout; 
     using std::endl; 
     Time movie_time(4, 35); 
     Time dinner_time(2, 47); 
     Time total; 
     Time diff; 
     Time adjusted; 

     cout << "movie_time time = "; 
     movie_time.Show(); 
     cout << endl; 

     cout << "dinner_time time = "; 
     dinner_time.Show(); 
     cout << endl; 

     cout << "total work time = "; 
     total = movie_time + dinner_time;   // use operator+() 
     total.Show(); 
     cout << endl; 

     diff = movie_time - dinner_time;    // use operator-() 
     cout << "movie_time time - dinner_time time = "; 
     diff.Show(); 
     cout << endl; 

     adjusted = total * 1.5;          // use operator+() 
     cout << "adjusted work time = "; 
     adjusted.Show(); 
     cout << endl; 

     return 0; 
} 

The code above generates the following result.