| amyx (6) | |||
|
Essentially, in my program, I need to make a bulb change brightness according to user input. There are 3 states: off, dim, bright. Should I have: enum {off, dim, bright};? However, I am wondering say, the bulb is already bright, how do I change the brightness back to 0? So:
Would that be a valid declaration? Is there a more efficient method? e.g. should I do bright++; Thank you in advance! Amy x | |||
|
Last edited on
|
|||
| ajh32 (163) | |||
How about this, it makes it easier to increment/decrement a bulb:
| |||
|
Last edited on
|
|||
| amyx (6) | |
|
Hiya, it looks rather complex!!! I have only learnt a little bit about classes, so could you comment some parts of the code pls? Also, in main(), if I chose to turn bulb 2 to dim, when it is off, how would I do that ? Thank you very much for your help! | |
|
|
|
| ajh32 (163) | |
|
I have updated, code to add comments. HTH It is really quite simple, we have a struct that has a default constructor and a custom constructor. The struct has a single member variable 'level', and a ++ and -- operator so that each instance of a Bulb can be incremented/decremented. You previously had a if | else if | else construct to do the same as the ++ operator for a Bulb now does. Calling the increment operator ++ increments the Bulb::level through off | 0 to dim | 1 to bright | 2 then back through to off | 0 etc... this is what happens when you increment an integer through its entire range when you get to the upper bounds of an integer it will loop round ... Does that help you understand? | |
|
Last edited on
|
|
| ajh32 (163) | ||||
| ||||
|
Last edited on
|
||||
| amyx (6) | |
|
Great. I think I am starting to get the hang of it now. so bulb 2 is bright and i want to dim it: bulbs[1]-- | |
|
|
|
| jimas13 (16) | |
|
ajh32 overloaded the ++ and -- operators so that when you use them with an object of struct Bulb other operations are performed... overloading is a way of adding operations to an operator... | |
|
|
|
| ajh32 (163) | |
| Yes, that's correct. | |
|
|
|