car.cpp

// Programmer: Jennifer Leopold Date: November 7, 2015

// File: car.cpp

// Purpose: This file contains the definitions of the functions for the

// Car class.

#include "car.h"

#include <iostream>

using namespace std;

void Car::incrBattery(const int weight)

{

if (weight > 0)

{

m_battery = m_battery +

static_cast<float>(weight)/BATTERY_RATE;

if (m_battery > BATTERY_MAX)

m_battery = BATTERY_MAX;

}

return;

}

void Car::incrDamage(const int weight)

{

if (weight > 0)

{

m_damage = m_damage +

static_cast<float>(weight)/DAMAGE_RATE;

if (m_damage > DAMAGE_MAX)

m_damage = DAMAGE_MAX;

}

return;

}

void Car::enterRoad(Road &r,

const int leftmostPosition) const

{

if ((leftmostPosition >= 0) &&

(leftmostPosition <= r.getWidth() - m_width))

r.placeCar(m_width, leftmostPosition, m_symbol);

else cout << "Error: Car:enterRoad cannot place a"

<< " car of width " << m_width

<< " at position " << leftmostPosition

<< " of a road of width "

<< r.getWidth() << endl;

return;

}

ostream& operator << (ostream& outs, const Car& c)

{

outs << "Car width = " << c.m_width << ", damage = "

<< c.m_damage << ", battery = " << c.m_battery

<< ", symbol = " << c.m_symbol << endl;

return(outs);

}