CSCI 201 Lab 10 -- Classes in C++

Getting ready

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

Download a ZIP'ed copy of the Lab 10 project and store it in the C:\FILES directory.

Go into the MS-DOS prompt and execute the following commands:

This should create a new directory called stoplite.

Classes

Open and build c:\files\Lab10\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, 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 internal variables. 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 and constructors of StopLight. There is a single constructor with no arguments which should initialize the three private variable of StopLight.

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 to supposed be called once a second from an outside time source. This method should, when appropriate, update internal variables of StopLight so that traffic is controlled in an orderly fashion.

The way in which StopLight is supposed to be used in that once a second a call is made to the Tick method followed by calls to the six bulb methods. However, because once a second would be boring, our lab driver presently calls 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. 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