Pejman Moghadam / C-programming

C++ - Two implementations of a simple counter class

Public domain


-- 1 --

#include <stdio.h>

class counter
{
        private:
                unsigned int value;
        public:
                counter() { value = 0; };
                void increment() { if (value < 65535) value++; }
                void decrement() { if (value >0) value--; }
                unsigned int access_value() { return value; }
};

main()
{
        counter c1;

        for ( int i=1; i <=15; i++ ) {
                c1.increment();
                printf("\n c1 = %u", c1.access_value());
        }
}

-- 2 --

#include <stdio.h>

class counter
{
        private:
                unsigned int value;
        public:
                counter();
                void increment();
                void decrement();
                unsigned int access_value();
};

counter::counter() { value = 0; };
void counter::increment() { if (value < 65535) value++; }
void counter::decrement() { if (value >0) value--; }
unsigned int counter::access_value() { return value; }

main()
{
        counter c1;

        for ( int i=1; i <=15; i++ ) {
                c1.increment();
                printf("\n c1 = %u", c1.access_value());
        }
}

BY: Pejman Moghadam
TAG: cpp
DATE: 2011-02-19 00:12:00


Pejman Moghadam / C-programming [ TXT ]