You are here:

C/C++ Traffic Light

Advertisement


Question
hi,
I'm new for cplusplus. I've to do a source code which can function as traffic light. I'm still cannot come out with the source code. here is the metaphor: there got 4 traffic lights. when 1 of a traffic light show green the other three should be in red. these four traffic light should change the color every 15 sec. if 2 traffic lights show green color at the same time, it should give "warning". other than that, if 1 of the traffic light took more time than 15 sec it also should output as warning to the user.
i would appreciate your help, sir..!

Answer
Hello ganapathyrajan

Your program can be made quite simple by creating a traffic light class, and array of 4 traffic light objects. Each traffic light object is an instance of the the traffic light class. A single function could be used to control all 4 traffic light objects. By using a single function to control all traffic lights, it would be impossible for errors conditions, like 2 green at the same time.

The fact that your assignment requires checking for 2 green makes me think that your instructor wants a more complicated solution, and it is not clear to me what your instructor wants. Perhaps if you gave me the exact text of the assignment I could give you a better answer.

Let's consider the simple solution. If you wanted to define a simple traffic light class, what types of data would you need to store in the class? I suppose you would need some way to store if it is red or green. What types of functions would the class need? I suppose it would need functions to set the colour, and some function to get the colour.

You can represent state (red or green) with an enumeration, like this:

   enum State
   {
       Red = 0,
       Green
   };

Inside your traffic light class, you can have a variable like this:
   State mState;

to represent the state.

It would be good to have a constructor to set the state when a traffic light instance is created. Setting the light in a safe state is a good idea, so I would set it to Red. Having a default constructor will allow you create an array of lights.

Here is a first attempt at a traffic light class.

#include <string>

class TrafficLight
{
public:

   /*
   Default constructor sets the light to a safe state
   and allows for arrays of lights.
   */
   TrafficLight()
   {
       mState = Red;
   }

   /*
   functions to get and set the sate
   */
   void SetRed()
   {
       mState = Red;
   }

   void SetGreen()
   {
       mState = Green;
   }

   string GetStateName()
   {
       if (mState == Red) return "red";
       return "green";
   }

private:
   /*
   Constants to represent the state of the light
   */
   enum State
   {
       Red = 0,
       Green
   };
   /*
   The state data
   */
   State mState;
};


Now you try to write a function to control 4 lights. The function should create an array of 4 lights. The lights will start all being red, because of the default constructor. The function should choose one light and set it green. Then the function should print out the state of all 4 lights, wait some time, and then choose a different light to set green.

Make an attempt at writing such a function, show me the attempt, and I will help you more.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.