C++ Implementing a Queue of cars in OOP

897 views Asked by At

I would like to know any suggestions about the implementation of a queue of cars. For now i have just one car that moves correctly in the screen, we are using Programming oriented object. What I need is to display more than one car in a road, for this i designed 3 classes Queue, Iterator and Node.

1.Queue:

public: 
    Queue();
    ~Queue();    
    bool add(Car car);
    Car get();
    Car& getFirst() const;
    Car& getFinal() const;
    bool isEmpty()  const;
    Iterador getStart() const;

private:
    Node* m_first;
    Node* m_final;

2.Iterator:

public: 
    Iterator();
    ~Iterator();
    Iterator(Node* position);    
    void next();
    Car& getCar() const;
    bool isNull() const;

private:
    Node* m_position;

3.Node:

public: 
    Node();
    ~Node();
    Node(Car car, Node* next);    

    void setCar(Car car);
    void setNext(Node* next);
    Car getCar() const;
    NodeVehicle* getNext() const;

private:
    Car m_car;
    Node* m_next;

There is a main project where i call all the classes, the Car class allows me to draw and move the car in the x-axis... The mentioned classes have been implemented but the problem is how to use them, I know that i can't paste the whole source code here, but it would be helpful if there's a topic that explains this very well.

1

There are 1 answers

0
Alexandre Severino On
std::queue<Car*> carQueue; // creates the queue.

// insertion loop.
for (int i = 0; i < 3; ++i)
{
    Car *car = new Car();
    carQueue.push(car); // inserts the car into the end of the queue.
}

size_type queueInitialSize = carQueue.size();
// popping loop.
for (int = 0; i < queueInitialSize; ++i)
{
    Car *car = carQueue.front(); // gets the car in the front of the queue.
    std::cout << car->getName() << std::endl;
    carQueue.pop(); // removes the car from the front of the queue.
    delete car; // don't forget to release the allocated memory.
}