//************************************************************** // // Class: Train class (Labtest2, Tuesday, 2:30) // // Authors: Art Miller amiller@mta.ca // Bob Rosebrugh rrosebrugh@mta.ca // // Description: A class for Train objects // // METHODS: // Train a constructor // moveForward move in easterly direction (forward) // moveBackward move in westerly direction (backward) // hasArrived has the train reached final destination ? // displayLocation display the current location of the train // // Date: November 16, 1999 // // Course Info: CS 1711, Lab test2 Tuesday, 2:30 - 4:00 // // Filename: train.h // the Train class class Train { private: int tripDistance; // distance to travel to destination int location; // current distance public: // constructor Train(); // modify distance (adding to or subtracting from current location) void moveForward(int distance); void moveBackward(int distance); // test if reached final destination bool hasArrived(); // display the current loction void displayPosition(); }; // *********************************************************** // Train class implementation // *********************************************************** // constructor Train::Train() { location = 0; tripDistance = 100; } void Train::moveForward(int distance) // move east from start point { location = location + distance; if (location >= tripDistance) location = tripDistance; } void Train::moveBackward(int distance) // move west from start point { location = location - distance; } bool Train::hasArrived() { return (location >= tripDistance); } void Train::displayPosition() { if (location >= tripDistance) cout << "The train has arrived!" << endl; else cout << "Location = " << location << endl; }