*URGENT* HELP WITH CLASSES!

My assignment is written as follows:
"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() method. When a new Light object is created, its initial color should be Red."

I am using c++ and terminal on mac, if that means anything.

Here is what i have set up already, but am stuck as to what to do after.

#include <iostream>
using namespace std;

Class Light
{
private:


public:


}

Light::Light()
{

}

Light::chgColor()
{

}

int main()
{

}

Somebody please help or give tips!!! College homework assignment and self taught.
Last edited on
is this someone from Dr. Gullien's class?
Haha, yup
oh haha who?
I've got some stuff but i can't get it to work XD Too many errors T.T
I'm in her Monday Wednesday class at 2:30. I'm not sure how she expects us to go this when the lab teacher was even pretty confused when she read the program...
so am I. XD
And yeah I'm confused. The lab prof said it's just like the circle lab program. But i still haven't figured it out.
Did you do the circle one?
I did most of it but its a little difficult and it doesn't really match up to the circle program I noticed. It's hard to even understand the question were supposed to answer
So you sort of have the right idea with implementing the functions, but you first need to declare them in the class. (The class keyword should be lowercase btw, that should be your first change)

I don't know what your teacher wants you to use for color so I'm assuming a string.

Your class should look something like this:
1
2
3
4
5
6
7
8
9
class Light
{
private:
   String color;

public:
	Light();
	void chgColor();
};


Your function implementation for the constructor is correct, you just need to initialize the color to red inside of it.

For your chgColor function you need to throw a void infront of it for the return type so:
1
2
3
4
void Light::chgColor()
{

}


And then from there you need to fill in the chgColor() method to change the string color to the other colors mentioned.

You would create an instance of your class like this:
1
2
3
4
5
6
int main()
{
     Light light;
     light.chgColor();   // To change color
     return 0;
}
hmmm... do we still need to use the constructor method?
Let's look at what you have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

Class Light
{
private:


public:


}

Light::Light()
{

}

Light::chgColor()
{

}

int main()
{

}


I'm assuming that this was what either you were given, or what you came up with so there isn't much to work with. Since I don't know the knowledge you have so far in this class, it's a little hard to say exactly what you know or the best route, but I'll go the simplest route and use booleans.

You have your constructor Light::Light() which is good and you already have a function for your class too Light::chgColor() which is also good. The problems that I see is that you're not using any variables in your class and therefore aren't storing an information in any objects. To get started, let's make some:
1
2
// We need variables to hold our colors
bool mRed, mYellow, mGreen;


Those will be true if the light is that color, false otherwise. They should be private. Now, instructions say to make the constructor make a red light, which is simple, we just make all of the colors false, except for red.
1
2
3
4
Light::Light() :
   mRed(true), mYellow(false), mGreen(false) {
      // We don't need to do anything else in here, so it's left blank
   }


Now, to change color:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
   }
}


That was simple, messy, but simple. Now, let's have a way to have the object tell you which color it is:
1
2
3
4
5
6
7
8
string Light::getColor() {
   if (mGreen)
      return "Green";
   else if (mYellow)
      return "Yellow";
   else
      return "Red";
}


And that should be all you need.

Now, a word of caution. Your teacher already knows about this site and I'm sure she checks it regularly for students asking for help so she will know that you used someone else's code. Meaning, do not use this code and turn it in as your own because you will get a zero on it. There are plenty of things that can be changed to make this your own including using ints instead of booleans (you should only need one int), using an enum (again, only need one) or whatever else you seem fit.

There are header files missing and I didn't give you a complete class example so that you have to work to get it to work together nicely.

Note: Only constructors (ctors) and destructors (dtors) don't have data types. All other class functions require a data type (including those that return nothing) in order to be valid code. Any questions, and I'll surely help.
Thanks everybody for your input!!
Topic archived. No new replies allowed.