C++ Class Inheritance with Counter class

Description

C++ Class Inheritance with Counter class

#include <iostream>
using namespace std;
class Counter                            //base class
{
   private:                            //NOTE: not private
       unsigned int count;                //count
   public:/*from ww  w.ja v  a2 s. c o  m*/
       Counter() : count(0)               //no-arg constructor
       {  }
       Counter(int c) : count(c)          //1-arg constructor
       {  }
       unsigned int get_count() const     //return count
       { return count; }
       Counter operator ++ ()             //incr count (prefix)
       { return Counter(++count); }
};
class CountDn : public Counter
{
   public:
   Counter operator -- ()             //decr count (prefix)
   { return Counter(--count); }
};
int main()
{
   CountDn c1;                           //c1 of class CountDn
   cout << "\nc1=" << c1.get_count();
   ++c1; ++c1; ++c1;                     //increment c1, 3 times
   cout << "\nc1=" << c1.get_count();
   --c1; --c1;                           //decrement c1, twice
   cout << "\nc1=" << c1.get_count();
   cout << endl;
   return 0;
}



PreviousNext

Related