#include "car.h" Car::Car() { make = "N/A"; // the make is unknown model = "N/A"; // the model is unknown year = 0; running = false; gear = 'P'; speed = 0; } Car::Car(int _year, string _make, string _model) { make = _make; model = _model; year = _year; running = false; gear = 'P'; speed = 0; } int Car::getSpeed() const { return speed; } char Car::getGear() const { return gear; } string Car::getMake() const { return make; } string Car::getModel() const { return model; } int Car::getYear() const { return year; } void Car::printStatus() const { cout << "year:\t" << year << endl; cout << "make:\t" << make << endl; cout << "model:\t" << model << endl; cout << "engine:\t"; if(running) { cout << "running" << endl; } else { cout << "off" << endl; } cout << "gear:\t" << gear << endl; cout << "speed:\t" << speed << " MPH" << endl; } bool Car::isRunning() const { return running; } void Car::start() { running = true; } bool Car::changeGear(char _gear) { char valid_gears[] = "PDRN"; // automatic transmission car with 4 gears bool valid = false; // set it to valid until we check whether _gear is valid or not // step through the 4 different entries in the valid_gears array for(int i = 0; i < 4; i++) { // If the value of _gear matches any of the valid gears in the // valid_gears array, then go ahead and change the gear. // The gear will not be changed otherwise, because we can't change gears // to some invalid gear like 'Z' for example if(_gear == valid_gears[i]) { valid = true; gear = _gear; break; } } return valid; } bool Car::accelerate(int x) { // acceleration must be greater than the current speed, // otherwise it wouldn't be called accelerate if(x > 0 && x > speed) { speed = x; return true; } return false; } bool Car::decelerate(int x) { // deceleration must be between the current speed and 0, // because you can't have a negative speed, and slowing // down means going less than your current speed if(x >= 0 && x < speed) { speed = x; return true; } return false; } void Car::turnOff() { running = false; // turn of the engine now }