CSCI 201 Lab 10 -- Classes in C++

Getting ready

If a directory called C:\FILES\Stoplite presently exists on your computer, delete it.

You get one project to start this lab. Download a ZIP'ed copy of the Lab 10 project and store it in the C:\FILES directory. Extract the files in this zip file create a new directory called Stoplite. Make sure that directory exists before proceeding.

Classes

Open and build c:\files\Stoplite\Stoplite.dsw. The build should fail with an error message about unresolved externals. That is expected.

The only files you'll modify in this lab are Stoplite.h and Stoplite.cpp which define a class called StopLight. StopLight is a representation of a traffic signal. The traffic signal controls the crossing of two roads, one running North-South and another running East-West. It has several methods (also called member functions), specified in Stoplite.h, that are used to modify and examine the traffic signal.

Start by looking at StopLight's include file Stoplite.h to see its data members. There are only three:

time
The number of seconds into the present cycle. In the present code cycles last 50 seconds.
stateNS
The state of the North-South lights.
stateEW
The state of the North-South lights.
The three possible values for stateNS and stateEW are Go, Caution, and Stop.

Now let's look at the methods (i.e. member functions) including the constructor for the StopLight class. The constructor takes no input (i.e. has no parameters) and is used to initialize the three data members of the StopLight class.

StopLight::StopLight() {
	// You need to initialize stateNS here
	stateEW = Go ;
	time = 0 ;
}
Unfortunately, due to a bug, one of these variables is not initialized. You'll need to fix that.

Six methods control the bulbs of the light. These methods have cryptic names that refer to the bulb they control. For example, method bulbNSR is used to determine if the red bulb in the North-South direction is turned on.

int StopLight::bulbNSR(void) {
	return stateNS == Stop ;
}
The definitions of a couple of the "bulb" methods are missing in Stoplite.cpp. You'll have to fill them in.

Instructor checkoff I

Fix the constructor and write the missing bulb methods. You should now be able to build and run the program.


The method Tick does all the interesting work. It is called once a second by the driver program which acts as an outside time clock. This method should, when appropriate, update the data members of StopLight so that traffic is controlled in an orderly fashion.

The Stoplight class is designed to be used as follows: Once every second, the driver program should call StopLight's method Tick after which it makes calls to the six bulb methods. However, because once a second would be boring, our lab driver presently calls these functions ten times a second.


Instructor checkoff II

Carefully study the buggy code for the Tick method. Modify it so that it behaves like a normal traffic signal. To help you, a compiled example of a program that solves this problem is available. You'll need to save it into the C:\FIlES directory and then run it.


Instructor checkoff III

Now turn the stop light into a signal that blinks red in one direction and yellow in another. Again a compiled example is available. You'll probably want to change the definition of the enumerated type Signal by adding a new value in order to solve this problem.


Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page