Ok. let's see if I can help here. First I notice a few things wrong with what you have thus far.
Let's make certain you understand this. You are making a traffic light class, from which you will make 4 traffic lights.
You need to understand that the class is for 1 traffic light. It's a blue print, from which the 4 traffic lights will be built. But each traffic light is only interested in itself, not the other three, so the blue print needs to be designed around just 1. I bring this up, because I see:
1 2 3 4 5
|
void TrafficSignal::printState(){
cout << "Signal " << Id << " is GREEN." << endl;
cout << "Signal " << Id << " is Yellow." << endl;
cout << "Signal " << Id << " is Red." << endl;
}
|
which is rather confusing.
Next step, I also see this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void TrafficSignal::cycle(){
if (Red == true){
Red = false;
Green = true;
}
if (Green == true){
Green = false;
Yellow = true;
}
if (Yellow == true){
Yellow = false;
Red = true;
}
}
|
This is going to fail miserably, because the logic behind it is self defeating.
If red is true, then we set red to false, and set green to true.
If green is true, well it is, cause we just set it, so let's set green to false and yellow to true
If yellow is true, well, we just set it, so yes it will be, so we set it to false and set red to true
The outcome of that code is that it will always have red = true.
You need to fix that by using else if like thus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void TrafficSignal::cycle() {
if (Red == true) {
Red = false;
Green = true;
}
else if (Green == true) {
Green = false;
Yellow = true;
}
else if (Yellow == true) {
Yellow = false;
Red = true;
}
}
|
However, I would recommend scrapping the whole bool for each color anyway. I see nothing in the instructions that say they have to be bools. Since a traffic light can be one color at a time, e.g. it can be red, or green, or yellow, but never red and green, or yellow and red, then probably the better bet is to use a single variable to hold it's current state.
1 2
|
enum color = { Red, Green, Yellow };
color light;
|
Or you can use an integer which may score you an extra point for originality if used like thus:
1 2 3 4 5 6 7 8
|
int light; // 0 = Red, 1 = Green, 2 = Yellow
...
void TrafficSignal::cycle() {
light++; // add 1 to light
light%= 3; // and modulo 3 to keep it from getting too high
}
|
So, as is, your current class needs a bit more work. You're gonna need one variable to hold current state (which light is lit up), three variables (or an array of 3 ints could easily be accessed using the light variable as index) to hold the duration for each light. Another variable to hold elapsed time from last cycle. (the seconds since light first started I don't see as being needed, but can be included if you want it.)
Then for the tick function, you need to increase your elapsed time variable, get current state, use that to get duration of current state, and compare that against elapsed time to see if a cycle is needed. There is a need to be able to set the durations, but i see no reason to retrieve them. You need to set an ID (another variable you will need), but can probably live without retrieving it. You can use the elapsed time from within the tick function, should be no need to get at it from outside the class, or to set it from outside either.
You really need to re-think what the traffic light is supposed to do, and what you need to make available to the outside world (the remainder of the program). Also, remember, to design the class based around 1 traffic light. the class is a blue print, from which you will be making 4 exact copies (objects) based on the 1 blueprint. That will be done in the main function.
There's lots more, but I don't want to overwhealm you in a single post. So see if you can figgure out what i've said thus far and then we'll move on.