NH> NH> const button::status out = button::out;
NH> It just seems to me as though there is an excess of
NH> verbiage there. Had I first declared panic to be an
NH> instance of class button it seems as though the above
NH> line would be totally unnecessary for use as:
NH> if (panic.status) {
NH> would assume an "in" position for status (or whatever I
NH> had enumerated as not zero).
If you are going to work with classes, enumerations are not
really necessary for simple matters, since you can use your
class functions to handle all of that.
class PushButton {
public:
PushButton(int);
int Out() { return !button; }
int In() { return button; }
int Push(){ return button = !button; }
private:
int button;
};
PushButton::PushButton(int In = 0) { button = !!In; }
Now you can do a simple query using In() or Out().
PushButton On_Off;
cout << "The button is " << (On_Off.Out() ? "not" : "")
<<" pushed." << endl;
cout << "The button is " << (On_Off.In() ? "" : "not")
<<" pushed." << endl;
Of course, you would likely simplify it to your ctor, On(),
and Push(). This can be reused easily in nearly any class
requiring a toggle. You could also change it to incorporate
On(), Off(), Status(), and Toggle() in one function.
/*_|_| PBUTTN.CPP PUBLIC DOMAIN Kurt Kuzba 10/1/1997
_|_|_| An example of using a simple button class.
_|_|*/
class PushButton {
public:
PushButton(int);
int Button(int iB = 0)
{
if(iB)
{
button = (iB < 0)
? 0
: (iB > 1)
? !button
: 1
;
}
return button;
}
private:
int button;
};
PushButton::PushButton(int In = 0) { button = !!In; }
#include
enum { BTN_OFF = -1, BTN_STATUS, BTN_ON, BTN_TOGGLE };
void IsPushed(int status)
{
cout << "Button is " << (status ? "" : "not")
<< " pressed.\n" << flush;
}
int main(void)
{
char a;
PushButton Button;
cout << "\n Begin Button Test\n";
IsPushed(Button.Button(BTN_STATUS));
cout << " Push ON button\n";
IsPushed(Button.Button(BTN_ON));
cout << " Push OFF Button\n";
IsPushed(Button.Button(BTN_OFF));
cout << " Push TOGGLE Button\n";
IsPushed(Button.Button(BTN_TOGGLE));
cout << " Push TOGGLE Button\n";
IsPushed(Button.Button(BTN_TOGGLE));
cout << " Press alpha key and [ENTER] to end test.\n" << flush;
cin >> a;
return 0;
}
/*_|_| end PBUTTN.CPP _|_|*/
I know it looks pretty bloated, but in reality it is only two
simple functions and one integer, and actually takes very
little space for either code or variables. Enumerations are
actually more useful outside of a class, since you have near
total control of the variables inside a class anyway.
In this case, you might enumerate or define BTN_OFF, BTN_ON,
BTN_TOGGLE, and BTN_STATUS to assist the programmer in using
your button class. Naturally, this would be a case of
complicating the simple in an effort to simplify in the given
example, but in more complex situations, it might serve.
You even have the option of using if(Button.Button()==BTN_ON).
> ] I am Solo of Borg, and I've got a bad feeling about this....
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)
|