#include <iostream>
using namespace std;
class Elevator
{
public: // Access specifier
int currentFloor; // Attribute (int variable) Current Floor number
bool upDirection; // Attribute (bool variable) flag for updirection
void move(int numFloors)
{ // Method/function to move between floors
if (upDirection == true)
{
currentFloor = currentFloor + numFloors;
}
else { currentFloor = currentFloor - numFloors; }
};
void stop() { // Method/function to stop
cout << "Elevator stoped" << endl;
};
string status()
{ // Method/function to move between floors
string str = to_string(currentFloor);
return str;
};
};
int main() { //Stack memory is set up by a simple variable declaration
Elevator stackObj; // Create an object of Elevator class and its stored in stack memory
// heap memory is set up through the use of the new keyword
Elevator* heapObj = new Elevator(); // Create an object of Elevator class and its stored in heap memory
cout << "Elevator move upward call using stack memory object " << endl;
stackObj.upDirection = true;
cout << "Move 3" << endl;
stackObj.move(3); // Call the method move by 3
cout << "set up direction downward" << endl;
stackObj.upDirection = false; cout << "Move 1" << endl;
stackObj.move(1);// Call the method move by 1
stackObj.stop(); cout << "checking status" << endl;
cout << "Elevator reached at " + stackObj.status() << endl;
cout << "Elevator move upward call using heap memory object " << endl;
heapObj->upDirection = true;
cout << "Move 3" << endl;
heapObj->move(3);; // Call the method move by 3
cout << "set up direction downward" << endl;
heapObj->upDirection = false;
cout << "Move 1" << endl;
heapObj->move(1); // Call the method move by 1
heapObj->stop();
cout << "checking status" << endl;
cout << "Elevator reached at " + heapObj->status() << endl;
}