increment counter variable with ++ operator : member variable « Class « C++ Tutorial






#include <iostream>  
  using namespace std;  

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








9.5.member variable
9.5.1.Create a getter for member variable
9.5.2.Initialize member variable in constructor
9.5.3.Share static variable among different class instances
9.5.4.Output variable initialization message
9.5.5.Assign value to member variable directly from assignment
9.5.6.increment counter variable with ++ operator
9.5.7.Initialize member variable