*IN C++ LANGUAGE* Overview For this assignment, implement and use the methods fo
ID: 3716431 • Letter: #
Question
*IN C++ LANGUAGE*
Overview
For this assignment, implement and use the methods for a class called Elevator.
Elevator class
This class will simulate very basic operations of an elevator.
Data Members
The data members for the class are:
an integer that holds the current floor where the elevator is located
an integer symbolic constant that holds the number of the bottom floor in a building. Use a value of 1.
an integer symbolic constant that holds the number of the top floor in a building. Use a value of 15.
The symbolic constants need to be handled in a slightly different manner in this program than in previous programs. The issue with this program is that the symbolic constant is a member of the class, but a class definition does not take up any memory since it is just a pattern for what the class should contain. This means that there is no memory to store the values 1 and 15. To solve this problem:
static const int TOP_FLOOR;
const int Elevator::TOP_FLOOR = 15;
Note the inclusion of "Elevator::" in front of the constant name. This lets the compiler know that the constant belongs to the Elevator class.
Define the symbolic constants with the keywords "static const" as part of the definition. For example:
Initialize the constants value outside of the class definition.
Constructors
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the current floor to the bottom floor of the building.
The other constructor for the class should initialize the data member using the passed in argument. It takes 1 argument: an integer that holds the floor where the elevator is located. If the argument is invalid (ie. it's value is less than the bottom floor value or greater than the top floor value, then the current floor data member should be initialized to the bottom floor. Otherwise, initialize the current floor data member using the passed in argument.
Methods
void request( int newFloor )
This is a public method that simulates the movement of the elevator to a new floor. It takes 1 argument: an integer that represents the floor number that the elevator should be moved to. It returns nothing.
If the passed in argument is invalid, display an error message that the floor number is invalid and do not move the elevator. If the elevator should move up, call the goUp method that is described below. If the elevator should move down, call the goDown method that is described below. If the elevator is already on the requested floor, display a message to the user and do not move the elevator.
int getCurrentFloor()
This is a public method that returns the current floor where the elevator is located. It takes no argument. It returns an integer, which is the current floor data member.
void goDown( int lowerFloor )
This is a private method that simulates the movement of the elevator to a lower floor. It takes 1 argument: an integer that represents the new lower floor number that the elevator should be moved to. It returns nothing.
Start by displaying the current floor where the elevator is located. After that, display a message for each floor that the elevator passes as it's "moving" to the desired lower floor. Once the elevator "arrives" at the desired lower floor, display a message to reflect that. For example, if the elevator is currently on floor #10 and the requested floor is #3, then the output should resemble:
void goUp( int higherFloor )
This is a private method that simulates the movement of the elevator to a higher floor. It takes 1 argument: an integer that represents the new higher floor number that the elevator should be moved to. It returns nothing.
Start by displaying the current floor where the elevator is located. After that, display a message for each floor that the elevator passes as it's "moving" to the desired higher floor. Once the elevator "arrives" at the desired higher floor, display a message to reflect that. For example, if the elevator is currently on floor #7 and the requested floor is #11, then the output should resemble:
main()
In main(), create 2 Elevator objects. They should contain the values:
The first Elevator object should be created using the default constructor (the one that doesn't take any arguments)
The second Elevator object should be created so that the elevator starts on floor number 6.
The rest of main() will include using the various methods on each of the Elevator objects. Display a label similar to "The first Elevator object" before anything is outputted for each of the objects.
For the first elevator, move to the 10th floor, move to the 5th floor, try to move to the 5th floor again, move to the 15th floor, and finally, move to the 1st floor.
For the second elevator, move to the 1st floor, move to the 13th floor, try to move to the -4th floor, and finally, display the current floor where the elevator is located.
Programming Notes
Each method must have a documentation box like a function.
Hand in a copy of your source code using Blackboard.
Output
Explanation / Answer
#include <iostream>
using namespace std;
// Elevator class defination
class Elevator
{ private:
int curFloor; //having current floor value
static const int BOTTOM_FLOOR;
static const int TOP_FLOOR;
//prototype of private functions
void goDown(int);
void goUp(int);
//prototype of public functions
public:
Elevator();
Elevator(int);
void request(int);
int getCurrentFloor();
}; // end of class defination
const int Elevator::BOTTOM_FLOOR = 1;
const int Elevator::TOP_FLOOR = 15;
//default constructor
Elevator::Elevator()
{ curFloor=BOTTOM_FLOOR;
}
//Parameterized constructor
Elevator::Elevator(int c)
{
if(c>=BOTTOM_FLOOR && c<=TOP_FLOOR)
curFloor=c;
else
curFloor=BOTTOM_FLOOR;
}
//simulates the movement of the elevator to a new floor
void Elevator::request( int newFloor )
{
if(newFloor<BOTTOM_FLOOR || newFloor>TOP_FLOOR)
cout<<"Error: invalid floor number"<<endl<<endl;
else
if(curFloor==newFloor)
cout<<"You're already on the floor"<<endl<<endl;
else
if(newFloor>curFloor)
goUp(newFloor);
else
if(newFloor<curFloor)
goDown(newFloor);
}
//function returns the current floor value where the elevator is located.
int Elevator::getCurrentFloor()
{ return(curFloor);}
//private function to elevator move down
void Elevator::goDown( int lowerFloor )
{int i;
cout<<"Starting at floor #"<<curFloor<<endl;
for(i=curFloor-1;i>lowerFloor;i--) {
cout<<"Going down -- now at floor #"<<i<<endl;
}
curFloor=lowerFloor;
cout<<"Welcome to floor #"<<lowerFloor<<endl<<endl;
}
//private function to elevator move up
void Elevator::goUp( int higherFloor )
{int i;
cout<<"Starting at floor #"<<curFloor<<endl;
for(i=curFloor+1;i<higherFloor;i++) {
cout<<"Going up -- now at floor #"<<i<<endl;
}
curFloor=higherFloor;
cout<<"Welcome to floor #"<<higherFloor<<endl<<endl;
}
//main() function using the class Elevator
int main()
{
Elevator Eobj1,Eobj2(6); //defining objects of Eleavator class
cout<<"The first Elevator object"<<endl<<endl;
Eobj1.request(10);
Eobj1.request(5);
Eobj1.request(5);
Eobj1.request(15);
Eobj1.request(1);
cout<<endl<<"The second Elevator object"<<endl;
Eobj2.request(1);
Eobj2.request(13);
Eobj2.request(-4);
cout<<"The elevator is currently on floor "<<Eobj2.getCurrentFloor();
} //end of main() function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.