// -*- mode: c++ -*-
// $Id: methcall.g++,v 1.7 2001/06/28 14:25:35 doug Exp $
// http://www.bagley.org/~doug/shootout/

// with some help from Bill Lear

// [dada] 2001-09-19 had to add 'int i' at the beggining of main for VC++

#include <stdlib.h>
#include <iostream>

using namespace std;

class Toggle {
public:
    Toggle(bool start_state) : state(start_state) { }
    virtual bool value() {
    return(state);
    }
    Toggle& activate() {
    state = !state;
    return(*this);
    }
    bool state;
};

class NthToggle : public Toggle {
public:
    NthToggle(bool start_state, int max_counter) :
    Toggle(start_state), count_max(max_counter), counter(0) {
    }
    NthToggle& activate() {
    if (++this->counter >= this->count_max) {
        state = !state;
        counter = 0;
    }
    return(*this);
    }
private:
    int count_max;
    int counter;
};

int
main(int argc, char *argv[]) {
    int i;
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    bool val = true;
    Toggle *toggle = new Toggle(val);
    for (i=0; i<n; i++) {
    val = toggle->activate().value();
    }
    cout << ((val) ? "true" : "false") << endl;
    delete toggle;

    val = true;
    NthToggle *ntoggle = new NthToggle(val, 3);
    for (i=0; i<n; i++) {
    val = ntoggle->activate().value();
    }
    cout << ((val) ? "true" : "false") << endl;
    delete ntoggle;

    return 0;
}