About the constructor. You are right about most of it.
(bool state = false) <- is creating a boolean variable called state |
A default value of false is also being defined here. This way the constructor will also work with no argument supplied.
Eg:
toggle t1(true);
creates t1 with x = true.
toggle t1;
default value of state = false will be used. x = false will result.
About the panel concept. What aspects of a panel do you wish to model?
If a panel is no more than a set of toggles then an array of toggles may do just fine to represent the panel.
toggle panel1[5];
panel1
is an array of 5 toggles.
I went a bit further and added a connected load data member to the toggle class myself:
double conn_load;// load served by switch in amps
I also added a member to the device class for the device load:
double I;// current drawn by device in amps
The device constructor sets everything up:
1 2 3 4
|
device(double current, toggle* pS): I(current), pSw(pS)// this is initialization list style
{
pSw->conn_load += I;// the toggles connected load is increased
}
|
Then my panel class has a
double load;
member = sum of connected loads for the switches that are in the "on" state. Doing this makes a panel function necessary so that the panel load gets adjusted when a toggle is switched on or off:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// This function is used to change the switch settings in the panel
bool panel::set_sw_state(int space, bool turn_on)// space = switch # in panel
{
if(space>=0 && space <N)// validate the space #
{
if( turn_on && !sw[space].on )// turn on a switch which was off
{
sw[space].on = true;
load += sw[space].conn_load;// adjust the panel load
}
else if( !turn_on && sw[space].on )// turn off a switch which was on.
{
sw[space].on = false;
load -= sw[space].conn_load;// adjust the panel load
}
return sw[space].on;
}
return false;// invalid space # was given
}
|
Example:
panel1.set_sw_state(2, true);// turns switch #2 on
I'm thinking of adding fuse protection for the switches in the panel. Each toggle would have a member:
double fuse_rating;
. A switch would "trip" (turn off) if the connected load exceeds the fuse rating.
You could make it as complicated as you wish!
EDIT: Details added.