<< Chapter < Page Chapter >> Page >

Focus on problem solving

Problem: Constructing an Elevator Object

In this application, you are required to simulate the operation of an elevator. Output is required that describes the current floor on which the elevator is stationed or is passing. Additionally, an internal elevator button that is pushed as a request to move to another floor should be provided. The elevator can travel between the first and fifteenth floors of the building in which it is situated.

Analyze the problem

For this application, we have one object, an elevator. The only attribute of interest is its location. A rider can request a change in the elevator’s position (state). Additionally, we must be able to establish the initial floor position when a new elevator is put in service. Figure below illustrates an object diagram that includes both the required attributes and operations.

Object diagram

Figure 7.3 An Elevator Class Diagram

Develop a solution

For this application, the location of the elevator, which corresponds to its current floor position, can be represented by an integer member variable whose value ranges between 1 and 15. The variable is named currentFloor. The value of currentFloor effectively represents the current state of the elevator. The services that we provide for changing the state of the elevator are an initialization function to set the initial floor position when a new elevator is put in service and a request function to change the elevator’s position (state) to a new floor. Putting an elevator in service is accomplished by declaring a single class instance and requesting a new floor position is equivalent to pushing an elevator button.

The response to the elevator button should be as follows:

If a request is made for either a nonexistent floor or the current floor,

Do nothing

Else if the request is for a floor above the current floor,

Display the current floor number

While not at the designated floor

Increment the floor number

Display the new floor number

End while

Display the ending floor number

Else

Display the current floor number

While not at the designated floor

Decrement the floor number

Display the new floor number

End while

Display the ending floor number

Endif

Coding the solution

From the design, a suitable class declaration is:

//class declaration

class Elevator

{

private:

int currentFloor;

public:

Elevator(int = 1); //constructor

void request(int);

};

The two declared public member functions, Elevator() and request(), are used to define the external services provided by each Elevator object. The Elevator() function becomes a constructor function that is automatically called when an object of type Elevator is created. We use this function to initialize the starting floor position of the elevator. The request() function is used to alter its position. To accomplish these services, a suitable class implementation section is:

// class implementation section

Elevator::Elevator(int cfloor)

{

currentFloor = cfloor;

}

void Elevator::request(int newfloor)

{

if (newfloor<1 || newfloor>MAXFLOOR || newfloor == currentFloor)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask