I don't understand what this problem is trying to make me, can anyone explain it to me?


Can anyone explain specifically what this problem is trying to make me do?


"Construct a class named Light that simulates a traffic light.

The class’s color attribute should change from Green to Yellow to Red and then back to Green by using the class’s change ( ) function.

When new Light object is created, its initial color should be Red."


Some of the parts of the question which confuses me is:

"Class's color attribute" and "class's change () function." what do these two mean? Can you give me an example?
Last edited on
The class needs to have an "attribute". This is also sometimes known as a member variable.

1
2
3
4
class car
{
  int number_of_wheels;
}


This class, "car", has an attribute named "number_of_wheels". You need to make an attribute in your class that you will use to represent the current colour of the light.


The class needs to have a function named "change".

1
2
3
4
class car
{
  void change(int);
}


This class, "car", has a function named "change". The function here returns nothing, and accepts an int.
Class's color attribute

Think about a traffic light (what your class is supposed to represent). What are the attributes of a traffic light? Your class needs to keep the state (color) of the traffic light. You can represent the three colors by using an enum.
 
  enum {RED,YELLOW,GREEN} TL_COLOR.


class's change () function

How is the traffic light supposed to change from one color to another? That is what the change() function is for. If the light is RED, calling change() should change the color to GREEN. If the color is GREEN, calling change() should change the color to YELLOW, and again YELLOW to RED.

This really isn't a difficult problem.

Last edited on
Can you give me an example?

an interpretation of how to get started, sure there are many others:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include <iostream>

enum class Color{RED, YELLOW, GREEN};

struct Light
{
    Color m_signal = Color::RED;
    //"When new Light object is created, its initial color should be Red."

    void change()
    {
        //...
    }
};

int main()
{

}
Topic archived. No new replies allowed.