Question: In C++ please: I am having trouble with my code on Queues with templates. Please fix it and show the output. Below i will attach

In C++ please: I am having trouble with my code on Queues with templates. Please fix it and show the output. Below i will attach the copy constructor function defintion as well and you have to add it to the program.

//copy constructor

template

Queue ::Queue(const Queue &obj)

{

//Allocate the queue array.

queueArray = new T[obj.queueSize];

//copy the other object's attributes

queueSize = obj.queueSize;

front = obj.front;

rear = obj.rear;

numItems = obj.numItems;

//copy the other objects queue array

for (int count = 0; count < obj.queueSize; count++)

queueArray[count] = obj.queueArray[count];

}

Code:

Header:

#ifndef QUEUE_H

#define QUEUE_H

using namespace std;

template< class T>

class Queue

{

private:

T * queueArray;

int queueSize;

int front;

int rear;

int numItems;

public:

//constructor

Queue(int);

//destructor

~Queue();

//Queue operartions

void enqueue(T);

void dequeue(T &);

bool isEmpty()const;

bool isFull() const;

void clear();

};

#endif //!

.cpp(Implementation file)

#include

#include

using namespace std;

#include "QUEUE.h"

template< class T>

Queue::Queue(int s)

{

queueArray = new T(s);

queueSize = s;

front = -1;

rear = -1;

numItems = 0;

}

template< class T>

void Queue::enqueue(T item)

{

if (isFull())

cout << "The queue is full. ";

if (front == -1 && rear == -1)

front = rear = 0;

else

rear = rear + 1;

queueArray[rear] = item;

//update item count

numItems++;

}

//function dequeue

template< class T>

void Queue::dequeue(T &item)

{

if (isEmpty())

cout << "The queue is empty. ";

else

{

item = queueArray[front];

//update item count

front++;

numItems--;

}

}

//returns true if the queue is empty, otherwise false

template< class T>

bool Queue::isEmpty()const

{

if (front == -1 || front > rear)

return true;

return false;

}

template< class T>

bool Queue::isFull() const

{

if (rear == queueSize - 1)

return true;

return false;

}

template< class T>

void Queue::clear()

{

front = queueSize - 1;

rear = queueSize - 1;

numItems = 0;

}

//destructor

template< class T>

Queue::~Queue()

{

delete[] queueArray;

}

Main(.cpp)

#include

#include

#include

#include "QUEUE.h"

using namespace std;

const int QUEUE_SIZE = 10;

int main()

{

string name;

//create a queue

Queuequeue(QUEUE_SIZE);

//Enqueue some names

for (int count = 0; count < QUEUE_SIZE; count++)

{

cout << "Enter a name: ";

getline(cin, name);

queue.enqueue(name);

}

//Dequeue the name and display them

cout << "Here are the names you entered: ";

for (int count = 0; count < QUEUE_SIZE; count++)

{

queue.dequeue(name);

cout << name << endl;

}

system("pause");

return 0;

}

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 Databases Questions!