Question: Complete traffic sim program in c + + including a way to demonstrate: #include #include #include #include #include #include using namespace std; / * VEHICLEQUEUE
Complete traffic sim program in c including a way to demonstrate: #include
#include
#include
#include
#include
#include
using namespace std;
VEHICLEQUEUE LOGIC:
use in multiple different places
allocate memory for vehicle, make it a pointer
set as pointer, to point to car typecast back to car, so you can access info from car & prevent data slicing error tp point to address of car, truck, motorcycle, bus
runtype ID most straightforward approach
VehicleQueue object for each direction of travel
queue q;
Member Functions:
qempty return bool
qsize return int # of elements
qpushv add vehicle to end of the queue
qpop removes vehicle from end of queue, does NOT tell you value, just removes it
qfront returns vehicle pointer of the front of the queue
qback returns vehicle pointer at the back of the queue
qclear removes all the elements, rather than going through length, gets rid of everything at once, releases all elements stored in queue
class VehicleQueue
private:
queue vehq;
public:
Constructor
VehicleQueue
Destructor
~VehicleQueue
clear;
Check if the queue is empty
bool empty const
return vehqempty;
Get the number of elements in the queue
int size const
return vehqsize;
Add a vehicle to the end of the queue
void pushVehicle v
vehqpushv;
Remove and return the first vehicle from the queue
Vehicle pop
if empty
Vehicle vehicle vehqfront;
vehqpop;
return vehicle;
else
return nullptr; or throw an exception
Get the vehicle pointer at the front of the queue
Vehicle front const
if empty
return vehqfront;
else
return nullptr;
Get the vehicle pointer at the back of the queue
Vehicle back const
if empty
return vehqback;
else
return nullptr;
Remove all vehicles from the queue
void clear
while empty
delete pop;
;
Vehicle Class
The vehicle class contains the location, speed, weight information of each of its derived vehicle
type car truck, bus motorcycle
class Vehicle
private:
char direction; N S E W
pair loc;
vector places;
float speed, weight;
public:
Vehiclechar dir, pair location, float spd float wgt;
char getDirection const;
pair getLocation const;
void setLocationpair newLocation;
float getSpeed const;
float getWeight const;
void move;
void erase;
void display;
;
Vehicle::Vehiclechar dir, std::pair location, float spd float wgt
: directiondir loclocation speedspd weightwgt
char Vehicle::getDirection const
return direction;
pair Vehicle::getLocation const
return loc;
void Vehicle::setLocationpair newLocation
loc newLocation;
float Vehicle::getSpeed const
return speed;
float Vehicle::getWeight const
return weight;
void Vehicle::move
void Vehicle::erase
void Vehicle::display
PLACE LOGIC:
grid intersection, put it smack in the middle
st coordinate nd coordinate
left lane of vertical portion of intersection road coordinates: to
right lane of vertical portion of intersection road coordinates: to
upper lane of horizontal portion of intersection road coordinates: to
lower lane of horizontal portion of intersection road coordinates: to
build lane st
lane going W to E: coordinate to
represent each grid location as a place object
each place can go N S E or W
if west, have place go east, and have north, south, or west, point to null
if a car is stitting in places, have place point to object that occupies it
pi & pi both point to vehicle and pi also points to pi
a mechanism is needed to record whether a given place on the road is free to enter or is
blocked
A place can be blocked if it is occupied by a car or if the light ahead is not green
The Place object contains a function FreeToMovedirection that indicates whether there is a link
to another Place object in that direction and whether this object is currently marked as free. It also
has a member function Nextdirection that returns a pointer to the next Place object. It is used
by the car's Move function.
We can only delete Place objects that are not parts of any other Road object.
class Place
private:
Represents the location of the place one patch of asph
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
