Question: #include #include using namespace std; / * * A simulated traffic light. * / class TrafficLight { public: / * * Constructs a green traffic
#include
#include
using namespace std;
A simulated traffic light.
class TrafficLight
public:
Constructs a green traffic light.
TrafficLight;
Constructs a traffic light.
@param initialcolor the initial color "green", "yellow", or "red"
TrafficLightstring initialcolor;
Moves this traffic light to the next color.
void next;
Returns the current color of this traffic light.
@return the current color
string getcolor const;
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
int getreds const;
private:
int steps;
;
TrafficLight::TrafficLight
Your code goes here
TrafficLight::TrafficLightstring initialcolor
Your code goes here
void TrafficLight::next
Your code goes here
string TrafficLight::getcolor const
Your code goes here
int TrafficLight::getreds const
Your code goes here
int main
TrafficLight tl;
cout tlgetcolor endl;
cout "Expected: green" endl;
cout tlgetreds endl;
cout "Expected:
endl;
tlnext;
cout tlgetcolor endl;
cout "Expected: yellow" endl;
cout tlgetreds endl;
cout "Expected:
endl;
tlnext;
cout tlgetcolor endl;
cout "Expected: red" endl;
cout tlgetreds endl;
cout "Expected:
endl;
tlnext;
cout tlgetcolor endl;
cout "Expected: green" endl;
cout tlgetreds endl;
cout "Expected:
endl;
TrafficLight tlred;
cout tlgetcolor endl;
cout "Expected: red" endl;
cout tlgetreds endl;
cout "Expected:
endl;
tlnext;
cout tlgetcolor endl;
cout "Expected: green" endl;
cout tlgetreds endl;
cout "Expected:
endl;
tlnext;
tlnext;
cout tlgetcolor endl;
cout "Expected: red" endl;
cout tlgetreds endl;
cout "Expected: endl;
return ;
Reimplement the TrafficLight class using a simple counter that is advanced in each call to next. If the traffic light was
initially green, the counter has values dots. If the traffic light was initially red, the counter has values
Compute the current color and the number of reds, using integer division and remainder.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
