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
run-type ID most straightforward approach
1 VehicleQueue object for each direction of travel
queue q;
Member Functions:
1. q.empty()- return bool
2. q.size()- return int # of elements
3. q.push(v)- add vehicle to end of the queue
4. q.pop()- removes vehicle from end of queue, does NOT tell you value, just removes it
5. q.front()- returns vehicle pointer of the front of the queue
6. q.back()- returns vehicle pointer at the back of the queue
7. q.clear()- 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 veh_q;
public:
// Constructor
VehicleQueue(){}
// Destructor
~VehicleQueue()
{
clear();
}
// Check if the queue is empty
bool empty() const
{
return veh_q.empty();
}
// Get the number of elements in the queue
int size() const
{
return veh_q.size();
}
// Add a vehicle to the end of the queue
void push(Vehicle* v)
{
veh_q.push(v);
}
// Remove and return the first vehicle from the queue
Vehicle* pop()
{
if (!empty())
{
Vehicle* vehicle = veh_q.front();
veh_q.pop();
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 veh_q.front();
}
else
{
return nullptr;
}
}
// Get the vehicle pointer at the back of the queue
Vehicle* back() const
{
if (!empty())
{
return veh_q.back();
}
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:
Vehicle(char dir, pair location, float spd, float wgt);
char getDirection() const;
pair getLocation() const;
void setLocation(pair newLocation);
float getSpeed() const;
float getWeight() const;
void move();
void erase();
void display();
};
Vehicle::Vehicle(char dir, std::pair location, float spd, float wgt)
: direction(dir), loc(location), speed(spd), weight(wgt){}
char Vehicle::getDirection() const
{
return direction;
}
pair Vehicle::getLocation() const
{
return loc;
}
void Vehicle::setLocation(pair 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
1st coordinate 499,2nd coordinate 500
left lane of vertical portion of intersection road coordinates: (499,0 to 1000)
right lane of vertical portion of intersection road coordinates: (500,0 to 1000)
upper lane of horizontal portion of intersection road coordinates: (0 to 1000,500)
lower lane of horizontal portion of intersection road coordinates: (0 to 1000,499)
build lane 1st
lane going W to E: coordinate (0 to 1000,499)
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 2 places, have place point to object that occupies it
p[i] & p[i+2] both point to vehicle and p[i+1] also points to p[i]
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 FreeToMove(direction) 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 Next(direction) 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!