hey guys please help me :(
the code is not working
here's the question
Construct a class named Light that simulates a traffic light. The class color attribute should change from green to yellow to red and then back to green by using the class change () function. When a new light is created its initial color should be red Thanks for helping ! :)
code: i've just seen this code here please explain it to me please cause i can't understand thanks
#include <iostream>
#include <string>
using namespace std;
class Light
{
private:
string color;
public:
Light();
void chgColor();
};
void Light::chgColor()
{
}
int main()
{
int mRed, mYellow, mGreen;
Light light;
light.chgColor(); // To change color
return 0;
}
Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
// We don't need to do anything else in here, so it's left blank
}
void Light::chgColor()
{
// See if the light is red
if (mGreen) {
// Set it to false
mGreen = false;
// And make it yellow
mYellow = true;
}
// We need else ifs or the light will be green each time
else if (mYellow) {
mYellow = false;
mRed = true;
}
// Assume it's red
else {
mRed = false;
mGreen = true;
}
}
string Light::getColor()
{
if (mGreen)
return "Green";
else if (mYellow)
return "Yellow";
else
return "Red";
}
#include <iostream>
#include <string>
usingnamespace std;
class Light
{
private:
string color;
public:
Light();
void chgColor();
};
void Light::chgColor()
{
}
int main()
{
int mRed, mYellow, mGreen;
Light light;
light.chgColor(); // To change color
return 0;
}
Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
// We don't need to do anything else in here, so it's left blank
}
void Light::chgColor()
{
// See if the light is red
if (mGreen) {
// Set it to false
mGreen = false;
// And make it yellow
mYellow = true;
}
// We need else ifs or the light will be green each time
elseif (mYellow) {
mYellow = false;
mRed = true;
}
// Assume it's red
else {
mRed = false;
mGreen = true;
}
}
string Light::getColor()
{
if (mGreen)
return"Green";
elseif (mYellow)
return"Yellow";
elsereturn"Red";
}
#include <iostream>
#include <string>
usingnamespace std;
class Light
{
private:
string color;
bool mRed,mGreen,mYellow;
public:
Light();
void chgColor();
string getColor();
};
Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
// We don't need to do anything else in here, so it's left blank
}
void Light::chgColor()
{
// See if the light is red
if (mGreen) {
// Set it to false
mGreen = false;
// And make it yellow
mYellow = true;
}
// We need else ifs or the light will be green each time
elseif (mYellow) {
mYellow = false;
mRed = true;
}
// Assume it's red
else {
mRed = false;
mGreen = true;
}
}
string Light::getColor()
{
if (mGreen)
return"Green";
elseif (mYellow)
return"Yellow";
elsereturn"Red";
}
int main()
{
int mRed, mYellow, mGreen;
Light light;
cout << light.getColor() << endl;
light.chgColor(); // To change color
cout << light.getColor() << endl;
light.chgColor();
cout << light.getColor() << endl;
return 0;
}