overloaded ++ operator in both prefix and postfix : Plus Plus « Overload « C++






overloaded ++ operator in both prefix and postfix

   
#include <iostream>  
  using namespace std;  

  class Counter{  
     private:  
        unsigned int count; 
     public:  
        Counter() : count(0){  }  
        Counter(int c) : count(c) {  }  
        unsigned int get_count() const{ return count; }  
    
        Counter operator ++ () {
           return Counter(++count);
        }
    
        Counter operator ++ (int){
           return Counter(count++);
        }                       
  };  
  int main(){  
     Counter c1, c2; 
    
     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count();  
    
     ++c1;                                 
     c2 = ++c1;                            
    
     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count();  
    
     c2 = c1++;

     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count() << endl;  
     return 0;  
  }
  
    
    
  








Related examples in the same category

1.Overload ++ relative to MyClass class.Overload ++ relative to MyClass class.
2.Overload ++ and -- for three_d.
3.Overload a unary operator ++Overload a unary operator ++