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.
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.
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>
enumclass 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()
{
}